"use strict";var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.Edit=void 0;const components_1=require("@woocommerce/components"),data_1=require("@woocommerce/data"),navigation_1=require("@woocommerce/navigation"),tracks_1=require("@woocommerce/tracks"),block_editor_1=require("@wordpress/block-editor"),components_2=require("@wordpress/components"),compose_1=require("@wordpress/compose"),core_data_1=require("@wordpress/core-data"),data_2=require("@wordpress/data"),element_1=require("@wordpress/element"),i18n_1=require("@wordpress/i18n"),classnames_1=__importDefault(require("classnames")),use_validation_1=require("../../hooks/use-validation"),radio_field_1=require("../../components/radio-field"),FOLLOW_CLASS_OPTION_VALUE="follow_class",FREE_SHIPPING_OPTION_VALUE="free_shipping",options=[{label:(0,i18n_1.__)("Follow class","woocommerce"),value:"follow_class"},{label:(0,i18n_1.__)("Free shipping","woocommerce"),value:"free_shipping"}];function Edit({attributes:e}){const{title:o}=e,t=(0,block_editor_1.useBlockProps)(),[s,n]=(0,element_1.useState)("free_shipping"),[l,i]=(0,core_data_1.useEntityProp)("postType","product","shipping_class"),{shippingClasses:a}=(0,data_2.useSelect)((e=>{var o;const{getProductShippingClasses:t}=e(data_1.EXPERIMENTAL_PRODUCT_SHIPPING_CLASSES_STORE_NAME);return{shippingClasses:null!==(o=t())&&void 0!==o?o:[]}}),[]),r=(0,compose_1.useInstanceId)(components_2.BaseControl),c=(0,use_validation_1.useValidation)("product/shipping_class",(function(){return!("follow_class"===s&&!l)}));return(0,element_1.useEffect)((()=>{""===l?n("free_shipping"):l&&n("follow_class")}),[l]),(0,element_1.createElement)("div",{...t},(0,element_1.createElement)("div",{className:"wp-block-columns"},(0,element_1.createElement)("div",{className:"wp-block-column"},(0,element_1.createElement)(radio_field_1.RadioField,{title:o,selected:s,options:options,onChange:function(e){var o;if(n(e),"follow_class"===e){const[e]=a;i(null!==(o=null==e?void 0:e.slug)&&void 0!==o?o:"")}else i("")}}))),"follow_class"===s&&(0,element_1.createElement)("div",{className:"wp-block-columns"},(0,element_1.createElement)("div",{className:(0,classnames_1.default)("wp-block-column",{"has-error":!c})},(0,element_1.createElement)(components_2.SelectControl,{id:r,name:"shipping_class",value:l,onChange:i,label:(0,i18n_1.__)("Shipping class","woocommerce"),help:c?(0,element_1.createInterpolateElement)((0,i18n_1.__)("Manage shipping classes and rates in <Link>global settings</Link>.","woocommerce"),{Link:(0,element_1.createElement)(components_1.Link,{href:(0,navigation_1.getNewPath)({tab:"shipping",section:"classes"},"",{},"wc-settings"),target:"_blank",type:"external",onClick:()=>{(0,tracks_1.recordEvent)("product_shipping_global_settings_link_click")}},(0,element_1.createElement)(element_1.Fragment,null))}):(0,i18n_1.__)("The shipping class is required.","woocommerce")},a.map((({slug:e,name:o})=>(0,element_1.createElement)("option",{key:e,value:e},o))))),(0,element_1.createElement)("div",{className:"wp-block-column"})))}exports.Edit=Edit;