(()=>{"use strict";var e={d:(t,s)=>{for(var a in s)e.o(s,a)&&!e.o(t,a)&&Object.defineProperty(t,a,{enumerable:!0,get:s[a]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{INITIAL_STATE:()=>l,QUADLAYERS_AICP_STORE_NAME:()=>R,STORE_NAME:()=>o,actions:()=>a,fetchRestApiTemplates:()=>u,reducer:()=>c,resolvers:()=>r,selectors:()=>s,useApiContentTemplates:()=>h});var s={};e.r(s),e.d(s,{getTemplates:()=>i});var a={};e.r(a),e.d(a,{createTemplate:()=>S,deleteTemplate:()=>y,editTemplate:()=>g,setTemplates:()=>_});var r={};e.r(r),e.d(r,{getTemplates:()=>E});var c={};e.r(c),e.d(c,{default:()=>w});const p=window.wp.data,o="aicp/templates/store",l={content:[]},i=e=>e.content,n=window.wp.i18n,d=window.wp.notices,m=window.aicp.helpers,{QUADLAYERS_AICP_API_CONTENT_TEMPLATES_REST_ROUTES:T}=aicpApiContentTemplates,u=({method:e,data:t}={})=>(0,m.apiFetch)({path:T.templates,method:e,data:t});function h(){const{createTemplate:e,editTemplate:t,deleteTemplate:s}=(0,p.useDispatch)(o),{templates:a,isResolvingTemplates:r,hasResolvedTemplates:c}=(0,p.useSelect)(e=>{const{isResolving:t,hasFinishedResolution:s,getTemplates:a}=e(o);return{templates:a(),isResolvingTemplates:t("getTemplates"),hasResolvedTemplates:s("getTemplates")}},[]),l=(a||[]).map(e=>({...e,template_origin:"user",model_temperature:parseFloat(e.model_temperature)}));return{templates:[...m.SYSTEM_CONTENT_TEMPLATES,...l],isResolvingTemplates:r,hasResolvedTemplates:c,hasTemplates:!(!c||!a?.length),createTemplate:e,editTemplate:t,deleteTemplate:s}}const _=e=>({type:"SET_TEMPLATES",payload:e}),S=e=>async({registry:t,dispatch:s,select:a})=>{const r=a.getTemplates(),c=await u({method:"POST",data:e});return c?.code||c?.message?(t.dispatch(d.store).createSuccessNotice((0,n.sprintf)("%s: %s",c.code,c.message),{type:"snackbar"}),!1):(r.push(c),s.setTemplates([...r]),t.dispatch(d.store).createSuccessNotice((0,n.__)("The template has been created successfully.","ai-copilot"),{type:"snackbar"}),c.template_id)},y=e=>async({registry:t,dispatch:s,select:a})=>{const r=a.getTemplates(),c=await u({method:"DELETE",data:{template_id:e}});if(c?.code||c?.message)return t.dispatch(d.store).createSuccessNotice((0,n.sprintf)("%s: %s",c.code,c.message),{type:"snackbar"}),!1;const p=r.filter(t=>parseInt(t.template_id)!==parseInt(e));return s.setTemplates([...p]),t.dispatch(d.store).createSuccessNotice((0,n.sprintf)((0,n.__)("The template %s has been deleted.","ai-copilot"),e),{type:"snackbar"}),p},g=e=>async({registry:t,dispatch:s,select:a})=>{const r=a.getTemplates(),c=await u({method:"PATCH",data:e});return c?.code||c?.message?(t.dispatch(d.store).createSuccessNotice((0,n.sprintf)("%s: %s",c.code,c.message),{type:"snackbar"}),!1):(s.setTemplates([...r.map(t=>t.template_id==e.template_id?e:t)]),t.dispatch(d.store).createSuccessNotice((0,n.__)("The template has been updated successfully.","ai-copilot"),{type:"snackbar"}),!0)},E=async()=>{try{const e=await u({method:"GET"});return Array.isArray(e)?_(e):_([])}catch(e){console.error(e)}};function w(e=l,t){return"SET_TEMPLATES"===t.type?{...e,content:t.payload}:e}const A=(0,p.createReduxStore)(o,{reducer:w,actions:a,selectors:s,resolvers:r});(0,p.register)((0,m.isVersionLessThan)(m.WP_VERSION,m.FIRST_WP_VERSION_WITH_THUNK_SUPPORT)?(0,m.applyThunkMiddleware)(A):A);const R=o;(window.aicp=window.aicp||{})["api-content-templates"]=t})();