(()=>{"use strict";var e={d:(t,s)=>{for(var r in s)e.o(s,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:s[r]})},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:()=>n,QUADLAYERS_AICP_STORE_NAME:()=>b,STORE_NAME:()=>i,actions:()=>r,fetchRestApiVectorStores:()=>l,fetchRestApiVectorStoresSync:()=>V,reducer:()=>c,resolvers:()=>o,selectors:()=>s,useApiVectorStore:()=>_});var s={};e.r(s),e.d(s,{getVectorStores:()=>d});var r={};e.r(r),e.d(r,{createVectorStore:()=>m,deleteVectorStore:()=>g,editVectorStore:()=>f,setVectorStores:()=>y,syncVectorStores:()=>v,updateVectorStore:()=>T});var o={};e.r(o),e.d(o,{getVectorStores:()=>R});var c={};e.r(c),e.d(c,{default:()=>E});const a=window.wp.data,i="aicp/vector-stores/store",n={vectorStores:[]},d=e=>e.vectorStores,S=window.wp.i18n,p=window.wp.notices,h=window.aicp.helpers,{QUADLAYERS_AICP_API_ASSISTANT_VECTOR_STORES_REST_ROUTES:u}=aicpApiAssistantVectorStores,l=({method:e,data:t}={})=>(0,h.apiFetch)({path:u.assistants_vector_stores,method:e,data:t}),V=({method:e,data:t}={})=>(0,h.apiFetch)({path:u.assistants_vector_stores+"?sync=true",method:e,data:t});function _(){const{createVectorStore:e,editVectorStore:t,deleteVectorStore:s,syncVectorStores:r,updateVectorStore:o,setVectorStores:c}=(0,a.useDispatch)(i),{vectorStores:n,isResolvingVectorStores:d,hasResolvedVectorStores:S}=(0,a.useSelect)(e=>{const{isResolving:t,hasFinishedResolution:s,getVectorStores:r}=e(i);return{vectorStores:r()||[],isResolvingVectorStores:t("getVectorStores"),hasResolvedVectorStores:s("getVectorStores")}},[]);return{vectorStores:n,isResolvingVectorStores:d,hasResolvedVectorStores:S,hasVectorStores:!(!S||!n?.length),createVectorStore:e,editVectorStore:t,deleteVectorStore:s,syncVectorStores:r,updateVectorStore:o,setVectorStores:c}}const y=e=>({type:"SET_VECTOR_STORES",payload:e}),v=()=>async({registry:e,dispatch:t,select:s})=>{const r=await V({method:"GET"});return r?.code||r?.message?(e.dispatch(p.store).createSuccessNotice((0,S.sprintf)("%s: %s",r.code,r.message),{type:"snackbar"}),!1):(t.setVectorStores([...r]),e.dispatch(p.store).createSuccessNotice((0,S.__)("The vector store have been synchronized successfully.","ai-copilot"),{type:"snackbar"}),r)},g=e=>async({registry:t,dispatch:s,select:r})=>{const o=r.getVectorStores(),c=await l({method:"DELETE",data:{vector_store_id:e}});if(c?.code||c?.message)return t.dispatch(p.store).createSuccessNotice((0,S.sprintf)("%s: %s",c.code,c.message),{type:"snackbar"}),!1;const a=o.filter(t=>parseInt(t.vector_store_id)!==parseInt(e));return s.setVectorStores([...a]),t.dispatch(p.store).createSuccessNotice((0,S.sprintf)((0,S.__)("The vector store %s has been deleted.","ai-copilot"),e),{type:"snackbar"}),!0},T=e=>async({registry:t,dispatch:s})=>{const r=await l({method:"PATCH",data:e});if(r?.code||r?.message)return t.dispatch(p.store).createSuccessNotice((0,S.sprintf)("%s: %s",r.code,r.message),{type:"snackbar"}),!1;const o=await V({method:"GET"});return o?.code||o?.message?(t.dispatch(p.store).createSuccessNotice((0,S.sprintf)("%s: %s",o.code,o.message),{type:"snackbar"}),!1):(s.setVectorStores([...o]),t.dispatch(p.store).createSuccessNotice((0,S.sprintf)((0,S.__)("The vector store %s has been restored.","ai-copilot"),e.vector_store_id),{type:"snackbar"}),!0)},m=e=>async({registry:t,dispatch:s,select:r})=>{const o=await l({method:"POST",data:e});if(o?.code||o?.message)return t.dispatch(p.store).createSuccessNotice((0,S.sprintf)("%s: %s",o.code,o.message),{type:"snackbar"}),!1;const c=r.getVectorStores();return c.push({...o,assistants:[]}),s.setVectorStores([...c]),t.dispatch(p.store).createSuccessNotice((0,S.__)("Vector store created successfully.","ai-copilot"),{type:"snackbar"}),o},f=e=>async({registry:t,dispatch:s,select:r})=>{const o=await l({method:"PATCH",data:e});if(o?.code||o?.message)return t.dispatch(p.store).createSuccessNotice((0,S.sprintf)("%s: %s",o.code,o.message),{type:"snackbar"}),!1;const c=r.getVectorStores();return s.setVectorStores([...c.map(t=>t.vector_store_id==e.vector_store_id?o:t)]),t.dispatch(p.store).createSuccessNotice((0,S.__)("The vector store has been updated successfully.","ai-copilot"),{type:"snackbar"}),o},R=async()=>{try{const e=await l({method:"GET"});return y(e)}catch(e){console.error(e)}};function E(e=n,t){return"SET_VECTOR_STORES"===t.type?{...e,vectorStores:t.payload}:e}const w=(0,a.createReduxStore)(i,{reducer:E,actions:r,selectors:s,resolvers:o});(0,a.register)((0,h.isVersionLessThan)(h.WP_VERSION,h.FIRST_WP_VERSION_WITH_THUNK_SUPPORT)?(0,h.applyThunkMiddleware)(w):w);const b=i;(window.aicp=window.aicp||{})["api-assistant-vector-stores"]=t})();