"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Edit=Edit;const block_templates_1=require("@woocommerce/block-templates"),data_1=require("@woocommerce/data"),core_data_1=require("@wordpress/core-data"),data_2=require("@wordpress/data"),element_1=require("@wordpress/element"),i18n_1=require("@wordpress/i18n"),shipping_dimensions_image_1=require("../../../components/shipping-dimensions-image"),validation_context_1=require("../../../contexts/validation-context"),number_control_1=require("../../../components/number-control"),SHIPPING_AND_WEIGHT_MIN_VALUE=0,SHIPPING_AND_WEIGHT_MAX_VALUE=1e14;function Edit({attributes:e,clientId:t,context:n}){var o,i,r;const l=(0,block_templates_1.useWooBlockProps)(e),[a,_]=(0,core_data_1.useEntityProp)("postType",n.postType,"dimensions"),[m,s]=(0,core_data_1.useEntityProp)("postType",n.postType,"weight"),[c]=(0,core_data_1.useEntityProp)("postType",n.postType,"virtual"),[d,u]=(0,element_1.useState)(),{dimensionUnit:h,weightUnit:p}=(0,data_2.useSelect)((e=>{const{getOption:t}=e(data_1.OPTIONS_STORE_NAME);return{dimensionUnit:t("woocommerce_dimension_unit"),weightUnit:t("woocommerce_weight_unit")}}),[]);function g(t,n){var o;return{name:`dimensions.${t}`,value:null!==(o=a&&a[t])&&void 0!==o?o:"",onChange:e=>_({...null!=a?a:{},[t]:e}),onFocus:()=>u(n),onBlur:()=>u(void 0),suffix:h,disabled:e.disabled||c,min:SHIPPING_AND_WEIGHT_MIN_VALUE,max:SHIPPING_AND_WEIGHT_MAX_VALUE}}const v=`dimensions_width-${t}`,{ref:E,error:w,validate:b}=(0,validation_context_1.useValidation)(v,(async function(){if((null==a?void 0:a.width)&&+a.width<=0)return{message:(0,i18n_1.__)("Width must be greater than zero.","woocommerce")}}),[null==a?void 0:a.width]),I=`dimensions_length-${t}`,{ref:N,error:f,validate:A}=(0,validation_context_1.useValidation)(I,(async function(){if((null==a?void 0:a.length)&&+a.length<=0)return{message:(0,i18n_1.__)("Length must be greater than zero.","woocommerce")}}),[null==a?void 0:a.length]),P=`dimensions_height-${t}`,{ref:S,error:x,validate:y}=(0,validation_context_1.useValidation)(P,(async function(){if((null==a?void 0:a.height)&&+a.height<=0)return{message:(0,i18n_1.__)("Height must be greater than zero.","woocommerce")}}),[null==a?void 0:a.height]),H=`weight-${t}`,{ref:T,error:G,validate:W}=(0,validation_context_1.useValidation)(H,(async function(){if(m&&+m<=0)return{message:(0,i18n_1.__)("Weight must be greater than zero.","woocommerce")}}),[m]),U={...g("width","A"),ref:E,onBlur:b,id:v},V={...g("length","B"),ref:N,onBlur:A,id:I},q={...g("height","C"),ref:S,onBlur:y,id:P},B={id:H,name:"weight",value:null!=m?m:"",onChange:s,suffix:p,ref:T,onBlur:W,disabled:e.disabled||c,min:SHIPPING_AND_WEIGHT_MIN_VALUE,max:SHIPPING_AND_WEIGHT_MAX_VALUE};return(0,element_1.createElement)("div",{...l},(0,element_1.createElement)("h4",null,(0,i18n_1.__)("Dimensions","woocommerce")),(0,element_1.createElement)("div",{className:"wp-block-columns"},(0,element_1.createElement)("div",{className:"wp-block-column"},(0,element_1.createElement)(number_control_1.NumberControl,{label:(0,element_1.createInterpolateElement)((0,i18n_1.__)("Width <Side />","woocommerce"),{Side:(0,element_1.createElement)("span",null,"A")}),error:w,...U}),(0,element_1.createElement)(number_control_1.NumberControl,{label:(0,element_1.createInterpolateElement)((0,i18n_1.__)("Length <Side />","woocommerce"),{Side:(0,element_1.createElement)("span",null,"B")}),error:f,...V}),(0,element_1.createElement)(number_control_1.NumberControl,{label:(0,element_1.createInterpolateElement)((0,i18n_1.__)("Height <Side />","woocommerce"),{Side:(0,element_1.createElement)("span",null,"C")}),error:x,...q}),(0,element_1.createElement)(number_control_1.NumberControl,{label:(0,i18n_1.__)("Weight","woocommerce"),error:G,...B})),(0,element_1.createElement)("div",{className:"wp-block-column"},(0,element_1.createElement)(shipping_dimensions_image_1.ShippingDimensionsImage,{highlight:d,className:"wp-block-woocommerce-product-shipping-dimensions-fields__dimensions-image",labels:{A:(null===(o=U.value)||void 0===o?void 0:o.length)?U.value:void 0,B:(null===(i=V.value)||void 0===i?void 0:i.length)?V.value:void 0,C:(null===(r=q.value)||void 0===r?void 0:r.length)?q.value:void 0}}))))}