"use strict";var __defProp=Object.defineProperty,__getOwnPropDesc=Object.getOwnPropertyDescriptor,__getOwnPropNames=Object.getOwnPropertyNames,__hasOwnProp=Object.prototype.hasOwnProperty,__export=(e,t)=>{for(var o in t)__defProp(e,o,{get:t[o],enumerable:!0})},__copyProps=(e,t,o,r)=>{if(t&&"object"==typeof t||"function"==typeof t)for(let s of __getOwnPropNames(t))__hasOwnProp.call(e,s)||s===o||__defProp(e,s,{get:()=>t[s],enumerable:!(r=__getOwnPropDesc(t,s))||r.enumerable});return e},__toCommonJS=e=>__copyProps(__defProp({},"__esModule",{value:!0}),e),edit_exports={};__export(edit_exports,{DEFAULT_SHIPPING_CLASS_OPTIONS:()=>DEFAULT_SHIPPING_CLASS_OPTIONS,Edit:()=>Edit}),module.exports=__toCommonJS(edit_exports);var import_block_templates=require("@woocommerce/block-templates"),import_components=require("@woocommerce/components"),import_data=require("@woocommerce/data"),import_navigation=require("@woocommerce/navigation"),import_tracks=require("@woocommerce/tracks"),import_components2=require("@wordpress/components"),import_compose=require("@wordpress/compose"),import_data2=require("@wordpress/data"),import_element=require("@wordpress/element"),import_i18n=require("@wordpress/i18n"),import_core_data=require("@wordpress/core-data"),import_components3=require("../../../components"),import_constants=require("../../../constants");const DEFAULT_SHIPPING_CLASS_OPTIONS=[{value:"",label:(0,import_i18n.__)("No shipping class","woocommerce")},{value:import_constants.ADD_NEW_SHIPPING_CLASS_OPTION_VALUE,label:(0,import_i18n.__)("Add new shipping class","woocommerce")}];function mapShippingClassToSelectOption(e){return e.map(({slug:e,name:t})=>({value:e,label:t}))}const shippingClassRequestQuery={};function extractDefaultShippingClassFromProduct(e,t){const o=e?.find(({slug:e})=>"uncategorized"!==e);if(o&&!t?.some(({slug:e})=>e===o.slug))return{name:o.name,slug:o.slug}}function Edit({attributes:e,context:{postType:t,isInSelectedTab:o}}){const[r,s]=(0,import_element.useState)(!1),i=(0,import_block_templates.useWooBlockProps)(e),{createProductShippingClass:p}=(0,import_data2.useDispatch)(import_data.experimentalProductShippingClassesStore),{createErrorNotice:n}=(0,import_data2.useDispatch)("core/notices"),[a]=(0,import_core_data.useEntityProp)("postType",t,"categories"),[c,m]=(0,import_core_data.useEntityProp)("postType",t,"shipping_class"),[_]=(0,import_core_data.useEntityProp)("postType",t,"virtual");function l(e){let t=(0,import_i18n.__)("We couldn’t add this shipping class. Try again in a few seconds.","woocommerce");throw"term_exists"===e.code&&(t=(0,import_i18n.__)("A shipping class with that slug already exists.","woocommerce")),n(t,{explicitDismiss:!0}),e}const{shippingClasses:u}=(0,import_data2.useSelect)(e=>{const{getProductShippingClasses:t}=e(import_data.experimentalProductShippingClassesStore);return{shippingClasses:o&&t(shippingClassRequestQuery)||[]}},[o]),d=(0,import_compose.useInstanceId)(import_components2.BaseControl,"wp-block-woocommerce-product-shipping-class-field");return(0,import_element.createElement)("div",{...i},(0,import_element.createElement)("div",{className:"wp-block-columns"},(0,import_element.createElement)("div",{className:"wp-block-column"},(0,import_element.createElement)(import_components2.SelectControl,{id:d,name:"shipping_class",value:c,onChange:e=>{e!==import_constants.ADD_NEW_SHIPPING_CLASS_OPTION_VALUE?m(e):s(!0)},label:(0,import_i18n.__)("Shipping class","woocommerce"),options:[...DEFAULT_SHIPPING_CLASS_OPTIONS,...mapShippingClassToSelectOption(u??[])],disabled:e.disabled||_,help:(0,import_element.createInterpolateElement)((0,import_i18n.__)("Manage shipping classes and rates in <Link>global settings</Link>.","woocommerce"),{Link:(0,import_element.createElement)(import_components.Link,{href:(0,import_navigation.getNewPath)({tab:"shipping",section:"classes"},"",{},"wc-settings"),target:"_blank",type:"external",onClick:()=>{(0,import_tracks.recordEvent)("product_shipping_global_settings_link_click")}},(0,import_element.createElement)(import_element.Fragment,null))})})),(0,import_element.createElement)("div",{className:"wp-block-column"})),r&&(0,import_element.createElement)(import_components3.AddNewShippingClassModal,{shippingClass:extractDefaultShippingClassFromProduct(a,u),onAdd:e=>p(e,{optimisticQueryUpdate:shippingClassRequestQuery}).then(e=>((0,import_tracks.recordEvent)("product_new_shipping_class_modal_add_button_click"),m(e.slug),e)).catch(l),onCancel:()=>s(!1)}))}