import{j as S}from"./query-ATBhtd3K.js";import{r as t}from"./vendor-EnoIVk-c.js";const K="semiont.knowledgeBases",i="semiont.activeKnowledgeBaseId";function f(){try{const o=localStorage.getItem(K);return o?JSON.parse(o):[]}catch{return[]}}function C(o){localStorage.setItem(K,JSON.stringify(o))}const B=t.createContext(void 0);function p({children:o}){const[n,d]=t.useState(()=>f()),[a,c]=t.useState(()=>{var l;const e=localStorage.getItem(i),s=f();return e&&s.some(r=>r.id===e)?e:((l=s[0])==null?void 0:l.id)??null});t.useEffect(()=>{C(n)},[n]),t.useEffect(()=>{a?localStorage.setItem(i,a):localStorage.removeItem(i)},[a]);const u=t.useCallback(e=>{d(s=>[...s,e]),c(e.id)},[]),g=t.useCallback(e=>{d(s=>s.filter(r=>r.id!==e)),c(s=>{var r;return s!==e?s:((r=n.filter(x=>x.id!==e)[0])==null?void 0:r.id)??null})},[n]),w=t.useCallback(e=>{c(e)},[]),m=t.useMemo(()=>n.find(e=>e.id===a)??null,[n,a]),v=t.useMemo(()=>({knowledgeBases:n,activeKnowledgeBaseId:a,activeKnowledgeBase:m,addKnowledgeBase:u,removeKnowledgeBase:g,setActiveKnowledgeBase:w}),[n,a,m,u,g,w]);return S.jsx(B.Provider,{value:v,children:o})}function k(){const o=t.useContext(B);if(!o)throw new Error("useKnowledgeBaseContext must be used within KnowledgeBaseProvider");return o}export{p as K,k as u};
//# sourceMappingURL=KnowledgeBaseContext-CgnF8K-_.js.map
