import{bV as M,d2 as j,d3 as B,r as l,a$ as x,j as F}from"./index-CfKY7XrC.js";import{a as T}from"./sortable.esm-CHIiExJi.js";function W(n,e){const t=n?.skillsConfig?.skills??[],u=new Map(e.map(s=>[s.repo,s]));return t.map(s=>{const p=u.get(s.repo);return{repo:s.repo,skillId:s.repo.replace(/^system\//,"")||s.name,name:s.name||p?.name||s.repo,description:s.description??p?.description,instructions:s.instructions??p?.instructions,content:s.content,enabled:s.enabled,system:s.system,nixPackages:s.nixPackages??p?.nixPackages,_original:s}})}function _(n){return{skills:n.map(e=>{const t=e.content||e.instructions||e.name;return{...e._original,repo:e.repo,name:e.name,description:e.description,instructions:e.instructions,content:t,enabled:e.enabled,system:e.system,nixPackages:e.nixPackages}})}}function $(n){return{skillsConfig:_(n)}}function q(n){const e=[];return n.repo.startsWith("system/")&&e.push("System"),(n.nixPackages?.length??0)>0&&e.push("Packages"),e}function V(n,e){return n.filter(t=>!t.hidden&&!e.has(t.repo)).map(t=>({key:t.repo,name:t.name,description:t.description||t.instructions||"Add this skill to give the agent tools and capabilities.",badges:q(t)}))}function G(n,e){const t=e.trim().toLowerCase();return t?n.filter(u=>u.name.toLowerCase().includes(t)||u.description.toLowerCase().includes(t)||u.key.toLowerCase().includes(t)):n}function z(n){const e=n.instructions?.trim();return{repo:n.repo,skillId:n.skillId,name:n.name,description:n.description,instructions:e,content:e||n.name,enabled:!0,system:n.repo.startsWith("system/"),nixPackages:n.nixPackages}}const L=l.createContext(null);function H({agentId:n,children:e}){const{data:t,isLoading:u}=M(n),{data:s,isLoading:p}=j(n),C=B(n),c=l.useMemo(()=>t?W(t,s?.catalog??[]):[],[t,s]),[w,m]=l.useState(null),d=w??c,g=l.useCallback(async(r,a)=>{if(!t)return x.error("Still loading agent settings — try again in a moment"),!1;try{const o=$(r);return await C.mutateAsync(o),x.success(a),!0}catch(o){return x.error(o instanceof Error?o.message:"Failed to save skills"),!1}},[t,C]),y=l.useCallback(r=>{const a=s?.catalog.find(o=>o.repo===r);a&&m(o=>{const i=o??c;return i.some(k=>k.repo===r)?i:[...i,z(a)]})},[c,s?.catalog]),v=l.useCallback(r=>{const a=r.name.trim();if(!a)return null;const o=`custom/${a.toLowerCase().replace(/[^a-z0-9-]/g,"-")}`,i=r.instructions.trim();return m(k=>{const f=k??c;return f.some(S=>S.repo===o)?f:[...f,{repo:o,skillId:a,name:a,instructions:i||void 0,content:i||a,enabled:!0,nixPackages:r.nixPackages.length>0?r.nixPackages:void 0}]}),o},[c]),P=l.useCallback((r,a)=>{m(o=>(o??c).map(i=>i.repo===r?{...i,...a}:i))},[c]),h=l.useCallback((r,a)=>{m(o=>{const i=o??c,k=i.findIndex(b=>b.repo===r),f=i.findIndex(b=>b.repo===a);if(k<0||f<0||k===f)return i;const S=T(i,k,f);return g(S,"Skill order updated"),S})},[g,c]),A=l.useCallback(async r=>{const a=d.filter(o=>o.repo!==r);return m(a),g(a,"Skill removed")},[g,d]),R=l.useCallback(async(r,a)=>{const o=d.map(i=>i.repo===r?{...i,enabled:a}:i);return m(o),g(o,a?"Skill enabled":"Skill disabled")},[g,d]),E=l.useCallback(async()=>{await g(d,"Skills saved")},[g,d]),I=l.useMemo(()=>({agentId:n,skillRows:d,catalog:s,isLoading:u||p,isSaving:C.isPending,addSkillFromCatalog:y,addCustomSkill:v,updateSkillRow:P,reorderSkills:h,removeSkillAndSave:A,setSkillEnabledAndSave:R,saveSkills:E}),[v,y,n,p,u,h,A,R,E,d,s,C.isPending,P]);return F.jsx(L.Provider,{value:I,children:e})}function J(){const n=l.useContext(L);if(!n)throw new Error("useAgentSkillEditor must be used within AgentSkillEditorProvider");return n}export{H as A,V as b,G as f,q as s,J as u};
