"use strict";(globalThis.webpackChunk_wcAdmin_webpackJsonp=globalThis.webpackChunk_wcAdmin_webpackJsonp||[]).push([[587],{3944:(e,t,a)=>{a.r(t),a.d(t,{default:()=>N});var r=a(69307),n=a(65736),c=a(95422),o=a(96483),l=a(94333),s=a(86020),i=a(74617),m=a(14599),d=a(86989),u=a.n(d);const _={};function p(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];for(const r of e){var a;const e={label:r.name,value:r.term_id.toString(),children:[]};_[r.term_id]=r,t.push(e),null!==(a=r.children)&&void 0!==a&&a.length&&p(r.children,e.children)}return t.sort(((e,t)=>{const a=e.label.toUpperCase(),r=t.label.toUpperCase();return a<r?-1:a>r?1:0})),t}async function g(e){const t=await u()({url:(0,o.addQueryArgs)(new URL("admin-ajax.php",(0,i.getSetting)("adminUrl")).toString(),{term:e,action:"woocommerce_json_search_categories_tree",security:wc_product_category_metabox_params.search_categories_nonce}),method:"GET"});return t?p(Object.values(t)):[]}const h=(0,r.forwardRef)(((e,t)=>{let{selectedCategoryTerms:a,onChange:o}=e;const[i,d]=(0,r.useState)(""),[u,p]=(0,r.useState)([]),h=(0,r.useCallback)((e=>{e&&e.length>0&&(0,m.recordEvent)("product_category_search",{page:"product",async:!0,search_string_length:e.length}),g(e).then((e=>{p(Object.values(e))}))}),[p]),y=(0,l.useDebounce)(h,250);return(0,r.useEffect)((()=>{y(i)}),[i]),(0,r.useImperativeHandle)(t,(()=>({resetInitialValues(){g("").then((e=>{p(Object.values(e))}))}})),[]),(0,r.createElement)(r.Fragment,null,(0,r.createElement)("div",{className:"product-add-category__tree-control"},(0,r.createElement)(s.TreeSelectControl,{alwaysShowPlaceholder:!0,options:u,value:a.map((e=>e.term_id.toString())),onChange:e=>{o(e.map((e=>_[e]))),(0,m.recordEvent)("product_category_update",{page:"product",async:!0,selected:e.length})},selectAllLabel:!1,onInputChange:d,placeholder:(0,n.__)("Add category","woocommerce"),includeParent:!0,minFilterQueryLength:2,clearOnSelect:!1,individuallySelectParent:!0})),(0,r.createElement)("ul",{className:"categorychecklist form-no-clear tagchecklist",id:c.z5+"checklist"},a.map((e=>(0,r.createElement)("li",{key:e.term_id},(0,r.createElement)("button",{type:"button",className:"ntdelbutton",onClick:()=>{const t=a.filter((t=>t.term_id!==e.term_id));o(t)}},(0,r.createElement)("span",{className:"remove-tag-icon","aria-hidden":"true"}),(0,r.createElement)("span",{className:"screen-reader-text"},(0,n.sprintf)((0,n.__)("Remove term: %s","woocommerce"),e.name))),e.name)))))})),y=e=>{let{selected:t,onChange:a}=e;const[n,l]=(0,r.useState)([]);(0,r.useEffect)((()=>{u()({url:(0,o.addQueryArgs)(new URL("admin-ajax.php",(0,i.getSetting)("adminUrl")).toString(),{action:"woocommerce_json_search_taxonomy_terms",taxonomy:c.z5,limit:10,orderby:"count",order:"DESC",security:wc_product_category_metabox_params.search_taxonomy_terms_nonce}),method:"GET"}).then((e=>{e&&l(e.filter((e=>e.count>0)))}))}),[]);const s=t.map((e=>e.term_id));return(0,r.createElement)("ul",{className:"categorychecklist form-no-clear",id:c.z5+"checklist-pop"},n.map((e=>{const n=`in-popular-${c.z5}-${e.term_id}`;return(0,r.createElement)("li",{key:e.term_id,className:"popular-category"},(0,r.createElement)("label",{className:"selectit",htmlFor:n},(0,r.createElement)("input",{type:"checkbox",id:n,checked:s.includes(e.term_id),onChange:()=>{s.includes(e.term_id)?a(t.filter((t=>t.term_id!==e.term_id))):a([...t,e])}}),e.name))})))};function w(){return w=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var a=arguments[t];for(var r in a)Object.prototype.hasOwnProperty.call(a,r)&&(e[r]=a[r])}return e},w.apply(this,arguments)}var b=a(67221);function v(e){return(null==e?void 0:e.name)||""}function E(e){return String(null==e?void 0:e.term_id)}const f=e=>{let{selectedCategoryTerms:t,onChange:a}=e;const[l,d]=(0,r.useState)(!1),[_,p]=(0,r.useState)(""),[g,h]=(0,r.useState)(""),[y,f]=(0,r.useState)(),[S,C]=(0,r.useState)([]),{currentUserCan:k}=(0,b.useUser)(),N=k("edit_product_terms"),x=(0,r.useCallback)((async function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return C([]),u()({url:(0,o.addQueryArgs)(new URL("admin-ajax.php",(0,i.getSetting)("adminUrl")).toString(),{term:e,action:"woocommerce_json_search_categories",security:wc_product_category_metabox_params.search_categories_nonce}),method:"GET"}).then((e=>(e&&C(Object.values(e)),[])))}),[]),{isFetching:z,...U}=(0,s.useAsyncFilter)({filter:x});return N?(0,r.createElement)("div",{id:c.z5+"-adder"},(0,r.createElement)("a",{id:"product_cat-add-toggle",href:"#taxonomy-"+c.z5,className:"taxonomy-add-new",onClick:()=>d(!l),"aria-label":(0,n.__)("Add new category","woocommerce")},(0,n.__)("+ Add new category","woocommerce")),l&&(0,r.createElement)("div",{id:"product_cat-add",className:"category-add"},(0,r.createElement)("label",{className:"screen-reader-text",htmlFor:"newproduct_cat"},(0,n.__)("Add new category","woocommerce")),(0,r.createElement)("input",{type:"text",name:"newproduct_cat",id:"newproduct_cat",className:"form-required",placeholder:(0,n.__)("New category name","woocommerce"),value:_,onChange:e=>p(e.target.value),"aria-required":"true"}),(0,r.createElement)("label",{className:"screen-reader-text",htmlFor:"newproduct_cat_parent"},(0,n.__)("Parent category:","woocommerce")),(0,r.createElement)(s.__experimentalSelectControl,w({},U,{label:(0,n.__)("Parent category:","woocommerce"),items:S,selected:y||null,placeholder:(0,n.__)("Find category","woocommerce"),onSelect:f,getItemLabel:v,getItemValue:E,onRemove:()=>f(void 0)})),g&&(0,r.createElement)("p",{className:"category-add__error"},g),(0,r.createElement)("input",{type:"button",id:"product_cat-add-submit",className:"button category-add-submit",value:(0,n.__)("Add new category","woocommerce"),disabled:!_.length,onClick:e=>{var r;if(e.preventDefault(),!_)return;const n={name:_,parent:null!==(r=null==y?void 0:y.term_id)&&void 0!==r?r:-1};h(""),u()({path:"/wc/v3/products/categories",data:n,method:"POST"}).then((e=>{e&&((0,m.recordEvent)("product_category_add",{category_id:e.id,parent_id:e.parent,parent_category:e.parent>0?"Other":"None",page:"product",async:!0}),a([...t,{term_id:e.id,name:e.name,count:e.count}]),p(""),f(void 0),d(!1))})).catch((e=>{e&&e.message&&h(e.message)}))}}))):null};let S="";window.getUserSetting&&(S=window.getUserSetting(c.z5+"_tab")||"");const C="pop",k="all",N=e=>{let{initialSelected:t}=e;const[a,o]=(0,r.useState)(t),l=(0,r.useRef)(null),[s,i]=(0,r.useState)(S===C?S:k);return(0,r.createElement)("div",{id:"taxonomy-"+c.z5,className:"categorydiv category-async-metabox"},(0,r.createElement)("ul",{className:"category-tabs"},(0,r.createElement)("li",{className:s===k?"tabs":""},(0,r.createElement)("a",{href:"#"+c.z5+"-"+k,onClick:e=>{e.preventDefault(),i(k),window.deleteUserSetting&&window.deleteUserSetting(c.z5+"_tab")}},(0,n.__)("All items","woocommerce"))),(0,r.createElement)("li",{className:s===C?"tabs":""},(0,r.createElement)("a",{href:"#"+c.z5+"-"+C,onClick:e=>{e.preventDefault(),i(C),window.setUserSetting&&window.setUserSetting(c.z5+"_tab",C)}},(0,n.__)("Most used","woocommerce")))),(0,r.createElement)("div",{className:"tabs-panel",id:c.z5+"-"+C,style:s!==C?{display:"none"}:{}},(0,r.createElement)("ul",{id:c.z5+"checklist-"+C,className:"categorychecklist form-no-clear"},(0,r.createElement)(y,{selected:a,onChange:o}))),(0,r.createElement)("div",{className:"tabs-panel",id:c.z5+"-"+k,style:s!==k?{display:"none"}:{}},(0,r.createElement)(h,{selectedCategoryTerms:a,onChange:o,ref:l})),(a||[]).map((e=>(0,r.createElement)("input",{key:e.term_id,type:"hidden",value:e.term_id,name:"tax_input["+c.z5+"][]"}))),0===a.length&&(0,r.createElement)("input",{type:"hidden",value:"",name:"tax_input["+c.z5+"][]"}),(0,r.createElement)(f,{selectedCategoryTerms:a,onChange:e=>{o(e),l.current&&l.current.resetInitialValues()}}))}}}]);