"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 block_editor_1=require("@wordpress/block-editor"),element_1=require("@wordpress/element");require("@woocommerce/settings");const components_1=require("@woocommerce/components"),core_data_1=require("@wordpress/core-data"),compose_1=require("@wordpress/compose"),data_1=require("@wordpress/data"),create_taxonomy_modal_1=require("./create-taxonomy-modal"),use_taxonomy_search_1=__importDefault(require("./use-taxonomy-search"));function Edit({attributes:e}){const t=(0,block_editor_1.useBlockProps)(),{hierarchical:r}=(0,data_1.useSelect)((t=>t("core").getTaxonomy(e.slug)||{hierarchical:!1})),{label:a,slug:o,property:n,createTitle:l,dialogNameHelpText:i,parentTaxonomyText:s}=e,[m,c]=(0,element_1.useState)(""),[u,_]=(0,element_1.useState)([]),{searchEntity:d,isResolving:p}=(0,use_taxonomy_search_1.default)(o,{fetchParents:r}),x=(0,compose_1.useDebounce)((0,element_1.useCallback)((e=>{c(e),d(e||"").then(_)}),[r]),150);(0,element_1.useEffect)((()=>{x("")}),[]);const[y,h]=(0,core_data_1.useEntityProp)("postType","product",n),g=y.map((e=>({value:String(e.id),label:e.name}))),[f,b]=(0,element_1.useState)(!1),v=u.map((e=>({parent:r&&e.parent&&e.parent>0?String(e.parent):void 0,label:e.name,value:String(e.id)})));return(0,element_1.createElement)("div",{...t},(0,element_1.createElement)(element_1.Fragment,null,(0,element_1.createElement)(components_1.__experimentalSelectTreeControl,{id:(0,compose_1.useInstanceId)(components_1.__experimentalSelectTreeControl,"woocommerce-taxonomy-select"),label:a,isLoading:p,multiple:!0,createValue:m,onInputChange:x,shouldNotRecursivelySelect:!0,shouldShowCreateButton:e=>!e||-1===v.findIndex((t=>t.label.toLowerCase()===e.toLowerCase())),onCreateNew:()=>b(!0),items:v,selected:g,onSelect:e=>{Array.isArray(e)?h([...e.map((e=>({id:+e.value,name:e.label,parent:+(e.parent||0)}))),...y]):h([{id:+e.value,name:e.label,parent:+(e.parent||0)},...y])},onRemove:e=>{Array.isArray(e)?h(y.filter((t=>!e.find((e=>e.value===String(t.id)))))):h(y.filter((t=>String(t.id)!==e.value)))}}),f&&(0,element_1.createElement)(create_taxonomy_modal_1.CreateTaxonomyModal,{slug:o,hierarchical:r,title:l,dialogNameHelpText:i,parentTaxonomyText:s,onCancel:()=>b(!1),onCreate:e=>{b(!1),c(""),h([{id:e.id,name:e.name,parent:e.parent},...y])},initialName:m})))}exports.Edit=Edit;