"use strict";var __create=Object.create,__defProp=Object.defineProperty,__getOwnPropDesc=Object.getOwnPropertyDescriptor,__getOwnPropNames=Object.getOwnPropertyNames,__getProtoOf=Object.getPrototypeOf,__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 a of __getOwnPropNames(t))__hasOwnProp.call(e,a)||a===o||__defProp(e,a,{get:()=>t[a],enumerable:!(r=__getOwnPropDesc(t,a))||r.enumerable});return e},__toESM=(e,t,o)=>(o=null!=e?__create(__getProtoOf(e)):{},__copyProps(!t&&e&&e.__esModule?o:__defProp(o,"default",{value:e,enumerable:!0}),e)),__toCommonJS=e=>__copyProps(__defProp({},"__esModule",{value:!0}),e),edit_exports={};__export(edit_exports,{Edit:()=>Edit}),module.exports=__toCommonJS(edit_exports);var import_element=require("@wordpress/element"),import_settings=require("@woocommerce/settings"),import_block_templates=require("@woocommerce/block-templates"),import_components=require("@woocommerce/components"),import_compose=require("@wordpress/compose"),import_data=require("@wordpress/data"),import_create_taxonomy_modal=require("./create-taxonomy-modal"),import_use_taxonomy_search=__toESM(require("./use-taxonomy-search")),import_use_product_entity_prop=__toESM(require("../../../hooks/use-product-entity-prop")),import_label=require("../../../components/label/label");function Edit({attributes:e,context:{postType:t,isInSelectedTab:o}}){const r=(0,import_block_templates.useWooBlockProps)(e),{hierarchical:a}=(0,import_data.useSelect)(t=>t("core").getTaxonomy(e.slug)||{hierarchical:!1},[e.slug]),{label:l,help:n,slug:p,property:m,createTitle:i,dialogNameHelpText:_,parentTaxonomyText:s,disabled:c}=e,[u,d]=(0,import_element.useState)(""),[b,y]=(0,import_element.useState)([]),{searchEntity:g,isResolving:f}=(0,import_use_taxonomy_search.default)(p,{fetchParents:a}),x=(0,import_compose.useDebounce)((0,import_element.useCallback)(e=>{d(e),g(e||"").then(y)},[a]),150);(0,import_element.useEffect)(()=>{o&&x("")},[o]);const[P,h]=(0,import_use_product_entity_prop.default)(m,{postType:t,fallbackValue:[]}),w=(P||[]).map(e=>({value:String(e.id),label:e.name})),[S,O]=(0,import_element.useState)(!1),v=b.map(e=>({parent:a&&e.parent&&e.parent>0?String(e.parent):void 0,label:e.name,value:String(e.id)}));return(0,import_element.createElement)("div",{...r},(0,import_element.createElement)(import_element.Fragment,null,(0,import_element.createElement)(import_components.__experimentalSelectTreeControl,{id:(0,import_compose.useInstanceId)(import_components.__experimentalSelectTreeControl,"woocommerce-taxonomy-select"),label:(0,import_element.createElement)(import_label.Label,{label:l,tooltip:n}),isLoading:f,disabled:c,multiple:!0,createValue:u,onInputChange:x,shouldNotRecursivelySelect:!0,shouldShowCreateButton:e=>!e||-1===v.findIndex(t=>t.label.toLowerCase()===e.toLowerCase()),onCreateNew:()=>O(!0),items:v,selected:w,onSelect:e=>{Array.isArray(e)?h([...e.map(e=>({id:+e.value,name:e.label,parent:+(e.parent||0)})),...P||[]]):h([{id:+e.value,name:e.label,parent:+(e.parent||0)},...P||[]])},onRemove:e=>{Array.isArray(e)?h((P||[]).filter(t=>!e.find(e=>e.value===String(t.id)))):h((P||[]).filter(t=>String(t.id)!==e.value))},onClear:function(){h([])},isClearingAllowed:(P||[]).length>0}),S&&(0,import_element.createElement)(import_create_taxonomy_modal.CreateTaxonomyModal,{slug:p,hierarchical:a,title:i,dialogNameHelpText:_,parentTaxonomyText:s,onCancel:()=>O(!1),onCreate:e=>{O(!1),d(""),h([{id:e.id,name:e.name,parent:e.parent},...P||[]])},initialName:u})))}