"use strict";var __create=Object.create,__defProp=Object.defineProperty,__getOwnPropDesc=Object.getOwnPropertyDescriptor,__getOwnPropNames=Object.getOwnPropertyNames,__getProtoOf=Object.getPrototypeOf,__hasOwnProp=Object.prototype.hasOwnProperty,__export=(e,o)=>{for(var t in o)__defProp(e,t,{get:o[t],enumerable:!0})},__copyProps=(e,o,t,r)=>{if(o&&"object"==typeof o||"function"==typeof o)for(let n of __getOwnPropNames(o))__hasOwnProp.call(e,n)||n===t||__defProp(e,n,{get:()=>o[n],enumerable:!(r=__getOwnPropDesc(o,n))||r.enumerable});return e},__toESM=(e,o,t)=>(t=null!=e?__create(__getProtoOf(e)):{},__copyProps(!o&&e&&e.__esModule?t:__defProp(t,"default",{value:e,enumerable:!0}),e)),__toCommonJS=e=>__copyProps(__defProp({},"__esModule",{value:!0}),e),create_taxonomy_modal_exports={};__export(create_taxonomy_modal_exports,{CreateTaxonomyModal:()=>CreateTaxonomyModal}),module.exports=__toCommonJS(create_taxonomy_modal_exports);var import_i18n=require("@wordpress/i18n"),import_components=require("@wordpress/components"),import_element=require("@wordpress/element"),import_data=require("@wordpress/data"),import_components2=require("@woocommerce/components"),import_compose=require("@wordpress/compose"),import_clsx=__toESM(require("clsx")),import_use_taxonomy_search=__toESM(require("./use-taxonomy-search"));const CreateTaxonomyModal=({onCancel:e,onCreate:o,initialName:t,slug:r,hierarchical:n,dialogNameHelpText:m,parentTaxonomyText:a,title:_})=>{const[l,p]=(0,import_element.useState)(""),[c,i]=(0,import_element.useState)([]),{searchEntity:s,isResolving:u}=(0,import_use_taxonomy_search.default)(r),d=(0,import_compose.useDebounce)((0,import_element.useCallback)(e=>s(e||"").then(i),[]),150);(0,import_element.useEffect)(()=>{d("")},[]);const{saveEntityRecord:y}=(0,import_data.useDispatch)("core"),[w,x]=(0,import_element.useState)(!1),[g,f]=(0,import_element.useState)(null),[P,b]=(0,import_element.useState)(t||""),[h,C]=(0,import_element.useState)(null),v=(0,import_compose.useInstanceId)(import_components.BaseControl,"taxonomy_name"),O=(0,import_compose.useInstanceId)(import_components2.__experimentalSelectTreeControl,"parent-taxonomy-select");return(0,import_element.createElement)(import_components.Modal,{title:_,onRequestClose:e,className:"woocommerce-create-new-taxonomy-modal"},(0,import_element.createElement)("div",{className:"woocommerce-create-new-taxonomy-modal__wrapper"},(0,import_element.createElement)(import_components.BaseControl,{id:v,label:(0,import_i18n.__)("Name","woocommerce"),help:g||m,className:(0,import_clsx.default)({"has-error":g})},(0,import_element.createElement)(import_components.TextControl,{id:v,value:P,onChange:b})),n&&(0,import_element.createElement)(import_components2.__experimentalSelectTreeControl,{isLoading:u,label:(0,import_element.createInterpolateElement)(`${a||(0,import_i18n.__)("Parent","woocommerce")} <optional/>`,{optional:(0,import_element.createElement)("span",{className:"woocommerce-create-new-taxonomy-modal__optional"},(0,import_i18n.__)("(optional)","woocommerce"))}),id:O,items:c.map(e=>({label:e.name,value:String(e.id),parent:e.parent>0?String(e.parent):void 0})),shouldNotRecursivelySelect:!0,selected:h?{value:String(h.id),label:h.name}:void 0,onSelect:e=>e&&C({id:+e.value,name:e.label,parent:e.parent?+e.parent:0}),onRemove:()=>C(null),onInputChange:e=>{d(e),p(e||"")},createValue:l}),(0,import_element.createElement)("div",{className:"woocommerce-create-new-taxonomy-modal__buttons"},(0,import_element.createElement)(import_components.Button,{variant:"tertiary",onClick:e,disabled:w},(0,import_i18n.__)("Cancel","woocommerce")),(0,import_element.createElement)(import_components.Button,{variant:"primary",disabled:0===P.length||w,isBusy:w,onClick:async()=>{f(null),x(!0);try{const e=await y("taxonomy",r,{name:P,parent:h?h.id:null},{throwOnError:!0});x(!1),o(e)}catch(e){if(x(!1),!e.message)throw f((0,import_i18n.__)("Failed to create taxonomy","woocommerce")),e;f(e.message)}}},(0,import_i18n.__)("Create","woocommerce")))))};