(()=>{"use strict";const e=window.wp.element,o=window.wp.components,t=window.wp.i18n,{restUrl:n,nonce:s}=window.cpaisData||{};const a=(e,o)=>async function(e,o={}){const t=n+e.replace(/^\//,""),a=await fetch(t,{...o,headers:{"Content-Type":"application/json","X-WP-Nonce":s,...o.headers||{}}}),i=await a.json().catch(()=>({}));if(!a.ok){const e=i?.message||`Request failed (${a.status})`,o=new Error(e);throw o.code=i?.code,o.status=a.status,o}return i}(e,{method:"POST",body:JSON.stringify(o||{})}),i=window.ReactJSXRuntime,{options:r={},adminUrl:c="#"}=window.cpaisData||{};function l({termId:n,taxonomy:s}){const[l,d]=(0,e.useState)("professional"),[m,u]=(0,e.useState)("medium"),[x,_]=(0,e.useState)("en"),[w,y]=(0,e.useState)(!1),[h,g]=(0,e.useState)(!1),[p,b]=(0,e.useState)(""),[j,S]=(0,e.useState)(null),[f,v]=(0,e.useState)(null);return n?(0,i.jsx)("div",{className:"cpais-taxonomy",children:(0,i.jsxs)(o.Card,{children:[(0,i.jsx)(o.CardHeader,{children:(0,i.jsx)("strong",{children:(0,t.__)("CatalogPilot AI Studio for WooCommerce — Description","catalogpilot-ai-studio-for-woocommerce")})}),(0,i.jsxs)(o.CardBody,{children:[(0,i.jsxs)(o.Flex,{gap:3,align:"flex-end",children:[(0,i.jsx)(o.SelectControl,{label:(0,t.__)("Tone","catalogpilot-ai-studio-for-woocommerce"),value:l,options:r.tones||[],onChange:d,__nextHasNoMarginBottom:!0}),(0,i.jsx)(o.SelectControl,{label:(0,t.__)("Length","catalogpilot-ai-studio-for-woocommerce"),value:m,options:r.lengths||[],onChange:u,__nextHasNoMarginBottom:!0}),(0,i.jsx)(o.SelectControl,{label:(0,t.__)("Language","catalogpilot-ai-studio-for-woocommerce"),value:x,options:r.languages||[],onChange:_,__nextHasNoMarginBottom:!0}),(0,i.jsx)(o.Button,{variant:"primary",onClick:async()=>{y(!0),S(null),v(null);try{const o=await(e={term_id:n,taxonomy:s,tone:l,length:m,language:x},a("generate-taxonomy",e));b(o.description||"")}catch(e){"cpais_missing_key"===e.code?S((0,i.jsxs)(i.Fragment,{children:[(0,t.__)("No API key configured.","catalogpilot-ai-studio-for-woocommerce")," ",(0,i.jsx)("a",{href:c,children:(0,t.__)("Open settings","catalogpilot-ai-studio-for-woocommerce")})]})):S(e.message)}finally{y(!1)}var e},isBusy:w,disabled:w,children:(0,t.__)("Generate","catalogpilot-ai-studio-for-woocommerce")})]}),j&&(0,i.jsx)("div",{className:"cpais-taxonomy__notice",children:(0,i.jsx)(o.Notice,{status:"error",isDismissible:!1,children:j})}),f&&(0,i.jsx)("div",{className:"cpais-taxonomy__notice",children:(0,i.jsx)(o.Notice,{status:f.type,onRemove:()=>v(null),children:f.text})}),p&&(0,i.jsxs)("div",{className:"cpais-taxonomy__draft",children:[(0,i.jsx)(o.TextareaControl,{label:(0,t.__)("Generated description (HTML)","catalogpilot-ai-studio-for-woocommerce"),value:p,onChange:b,rows:8,__next40pxDefaultSize:!0,__nextHasNoMarginBottom:!0}),(0,i.jsx)("div",{className:"cpais-taxonomy__save",children:(0,i.jsx)(o.Button,{variant:"primary",onClick:async()=>{g(!0);try{await(e={term_id:n,taxonomy:s,description:p},a("apply-taxonomy",e)),v({type:"success",text:(0,t.__)("Description saved. Reload the page to see it in the editor.","catalogpilot-ai-studio-for-woocommerce")})}catch(e){v({type:"error",text:e.message})}finally{g(!1)}var e},isBusy:h,disabled:h,children:(0,t.__)("Save description","catalogpilot-ai-studio-for-woocommerce")})})]})]})]})}):null}function d(){const o=document.getElementById("cpais-taxonomy-root");if(!o)return;const t=parseInt(o.dataset.termId,10),n=o.dataset.taxonomy,s=(0,i.jsx)(e.StrictMode,{children:(0,i.jsx)(l,{termId:t,taxonomy:n})});"function"==typeof e.createRoot?(0,e.createRoot)(o).render(s):(0,e.render)(s,o)}"loading"===document.readyState?document.addEventListener("DOMContentLoaded",d):d()})();