"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Edit=exports.DEFAULT_SHIPPING_CLASS_OPTIONS=void 0;const block_templates_1=require("@woocommerce/block-templates"),components_1=require("@woocommerce/components"),data_1=require("@woocommerce/data"),navigation_1=require("@woocommerce/navigation"),tracks_1=require("@woocommerce/tracks"),components_2=require("@wordpress/components"),compose_1=require("@wordpress/compose"),data_2=require("@wordpress/data"),element_1=require("@wordpress/element"),i18n_1=require("@wordpress/i18n"),core_data_1=require("@wordpress/core-data"),components_3=require("../../../components"),constants_1=require("../../../constants");function mapShippingClassToSelectOption(e){return e.map((({slug:e,name:t})=>({value:e,label:t})))}function extractDefaultShippingClassFromProduct(e,t){const s=null==e?void 0:e.find((({slug:e})=>"uncategorized"!==e));if(s&&!(null==t?void 0:t.some((({slug:e})=>e===s.slug))))return{name:s.name,slug:s.slug}}function Edit({attributes:e,context:t}){const[s,o]=(0,element_1.useState)(!1),n=(0,block_templates_1.useWooBlockProps)(e),{createProductShippingClass:a,invalidateResolution:c}=(0,data_2.useDispatch)(data_1.EXPERIMENTAL_PRODUCT_SHIPPING_CLASSES_STORE_NAME),{createErrorNotice:i}=(0,data_2.useDispatch)("core/notices"),[r]=(0,core_data_1.useEntityProp)("postType",t.postType,"categories"),[l,_]=(0,core_data_1.useEntityProp)("postType",t.postType,"shipping_class"),[p]=(0,core_data_1.useEntityProp)("postType",t.postType,"virtual");function m(e){let t=(0,i18n_1.__)("We couldn’t add this shipping class. Try again in a few seconds.","woocommerce");throw"term_exists"===e.code&&(t=(0,i18n_1.__)("A shipping class with that slug already exists.","woocommerce")),i(t,{explicitDismiss:!0}),e}const{shippingClasses:d}=(0,data_2.useSelect)((e=>{var t;const{getProductShippingClasses:s}=e(data_1.EXPERIMENTAL_PRODUCT_SHIPPING_CLASSES_STORE_NAME);return{shippingClasses:null!==(t=s())&&void 0!==t?t:[]}}),[]),u=(0,compose_1.useInstanceId)(components_2.BaseControl,"wp-block-woocommerce-product-shipping-class-field");return(0,element_1.createElement)("div",{...n},(0,element_1.createElement)("div",{className:"wp-block-columns"},(0,element_1.createElement)("div",{className:"wp-block-column"},(0,element_1.createElement)(components_2.SelectControl,{id:u,name:"shipping_class",value:l,onChange:e=>{e!==constants_1.ADD_NEW_SHIPPING_CLASS_OPTION_VALUE?_(e):o(!0)},label:(0,i18n_1.__)("Shipping class","woocommerce"),options:[...exports.DEFAULT_SHIPPING_CLASS_OPTIONS,...mapShippingClassToSelectOption(null!=d?d:[])],disabled:p,help:(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,element_1.createElement)("div",{className:"wp-block-column"})),s&&(0,element_1.createElement)(components_3.AddNewShippingClassModal,{shippingClass:extractDefaultShippingClassFromProduct(r,d),onAdd:e=>a(e).then((e=>((0,tracks_1.recordEvent)("product_new_shipping_class_modal_add_button_click"),c("getProductShippingClasses"),_(e.slug),e))).catch(m),onCancel:()=>o(!1)}))}exports.DEFAULT_SHIPPING_CLASS_OPTIONS=[{value:"",label:(0,i18n_1.__)("No shipping class","woocommerce")},{value:constants_1.ADD_NEW_SHIPPING_CLASS_OPTION_VALUE,label:(0,i18n_1.__)("Add new shipping class","woocommerce")}],exports.Edit=Edit;