(()=>{"use strict";var e={d:(s,t)=>{for(var i in t)e.o(t,i)&&!e.o(s,i)&&Object.defineProperty(s,i,{enumerable:!0,get:t[i]})},o:(e,s)=>Object.prototype.hasOwnProperty.call(e,s),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},s={};e.r(s),e.d(s,{INITIAL_STATE:()=>l,QUADLAYERS_AICP_STORE_NAME:()=>A,STORE_NAME:()=>o,actions:()=>i,fetchRestApiFiles:()=>u,fetchRestApiFilesExampleFile:()=>f,fetchRestApiFilesSync:()=>y,reducer:()=>c,resolvers:()=>a,selectors:()=>t,useApiFile:()=>S});var t={};e.r(t),e.d(t,{getFiles:()=>n});var i={};e.r(i),e.d(i,{createFile:()=>w,deleteFile:()=>m,setFiles:()=>_,syncFiles:()=>g,updateFile:()=>T});var a={};e.r(a),e.d(a,{getFiles:()=>E});var c={};e.r(c),e.d(c,{default:()=>R});const r=window.wp.data,o="aicp/files/store",l={files:[]},n=e=>e.files,d=window.wp.i18n,p=window.wp.notices,h=window.aicp.helpers,{QUADLAYERS_AICP_API_ASSISTANT_FILES_REST_ROUTES:F}=aicpApiAssistantFiles,u=({method:e,data:s}={})=>(0,h.apiFetch)({path:F.assistants_files,method:e,data:s}),f=({method:e,data:s}={})=>(0,h.apiFetch)({path:F.assistants_file_example,method:e,data:s}),y=({method:e,data:s}={})=>(0,h.apiFetch)({path:F.assistants_files+"?sync=true",method:e,data:s});function S(){const{createFile:e,deleteFile:s,syncFiles:t,updateFile:i,setFiles:a}=(0,r.useDispatch)(o),{files:c,isResolvingFiles:l,hasResolvedFiles:n}=(0,r.useSelect)(e=>{const{isResolving:s,hasFinishedResolution:t,getFiles:i}=e(o);return{files:i()||[],isResolvingFiles:s("getFiles"),hasResolvedFiles:t("getFiles")}},[]);return{files:c,isResolvingFiles:l,hasResolvedFiles:n,hasFiles:!(!n||!c?.length),createFile:e,deleteFile:s,syncFiles:t,updateFile:i,setFiles:a}}const _=e=>({type:"SET_FILES",payload:e}),g=()=>async({registry:e,dispatch:s,select:t})=>{const i=await y({method:"GET"});return i?.code||i?.message?(e.dispatch(p.store).createSuccessNotice((0,d.sprintf)("%s: %s",i.code,i.message),{type:"snackbar"}),!1):(s.setFiles([...i]),e.dispatch(p.store).createSuccessNotice((0,d.__)("The files has been synchronized successfully.","ai-copilot"),{type:"snackbar"}),i)},m=e=>async({registry:s,dispatch:t,select:i})=>{const a=i.getFiles(),c=await u({method:"DELETE",data:{file_id:e}});if(c?.code||c?.message)return s.dispatch(p.store).createSuccessNotice((0,d.sprintf)("%s: %s",c.code,c.message),{type:"snackbar"}),!1;const r=a.filter(s=>parseInt(s.file_id)!==parseInt(e));return t.setFiles([...r]),s.dispatch(p.store).createSuccessNotice((0,d.sprintf)((0,d.__)("The file %s has been deleted.","ai-copilot"),e),{type:"snackbar"}),!0},T=(e,s)=>async({registry:t,dispatch:i,select:a})=>{a.getFiles();const c=await u({method:"PATCH",data:{file_id:e,attachment_id:s}});if(c?.code||c?.message)return t.dispatch(p.store).createSuccessNotice((0,d.sprintf)("%s: %s",c.code,c.message),{type:"snackbar"}),!1;const r=await y({method:"GET"});return r?.code||r?.message?(t.dispatch(p.store).createSuccessNotice((0,d.sprintf)("%s: %s",r.code,r.message),{type:"snackbar"}),!1):(i.setFiles([...r]),t.dispatch(p.store).createSuccessNotice((0,d.sprintf)((0,d.__)("The file %s has been restored.","ai-copilot"),e),{type:"snackbar"}),!0)},w=e=>async({registry:s,dispatch:t,select:i})=>{const a=await u({method:"POST",data:e});if(a?.code||a?.message)return s.dispatch(p.store).createSuccessNotice((0,d.sprintf)("%s: %s",a.code,a.message),{type:"snackbar"}),!1;const c=i.getFiles();return c.push({...a,assistants:[]}),t.setFiles([...c]),s.dispatch(p.store).createSuccessNotice((0,d.__)("File created successfully","ai-copilot"),{type:"snackbar"}),a},E=async()=>{try{const e=await u({method:"GET"});return _(e)}catch(e){console.error(e)}};function R(e=l,s){return"SET_FILES"===s.type?{...e,files:s.payload}:e}const b=(0,r.createReduxStore)(o,{reducer:R,actions:i,selectors:t,resolvers:a});(0,r.register)((0,h.isVersionLessThan)(h.WP_VERSION,h.FIRST_WP_VERSION_WITH_THUNK_SUPPORT)?(0,h.applyThunkMiddleware)(b):b);const A=o;(window.aicp=window.aicp||{})["api-assistant-files"]=s})();