import{n as e}from"./chunk-DR8-3Aex.js";import{n as t,s as n}from"./envUtils-BWeoiL4Y.js";import{basename as r,join as i,resolve as a}from"path";import{existsSync as o,mkdirSync as s,readFileSync as c,realpathSync as l,writeFileSync as u}from"fs";import{createHash as d}from"crypto";import{execFileSync as f}from"child_process";function p(){return i(t(),`skill-learning`)}function m(){return i(p(),`projects.json`)}function h(e){return e===M?i(p(),`global`):i(p(),`projects`,e)}function g(e){return i(h(e),`project.json`)}function _(e,t){if(I.has(e)&&I.delete(e),I.set(e,t),I.size>P){let e=I.size-F,t=I.keys();for(let n=0;n<e;n++){let e=t.next();if(e.done)break;I.delete(e.value)}}}function v(e=process.cwd()){let t=I.get(e);if(t){I.delete(e),I.set(e,t);let n=Date.now();return n-R>L&&(R=n,C(t)),t}let n=x(e);return _(e,n),C(n),R=Date.now(),n}function y(){I.clear(),R=0}function b(){let e=w(m());return Object.values(e.projects).sort((e,t)=>e.projectName.localeCompare(t.projectName))}function x(e){let t=process.env.CLAUDE_PROJECT_DIR?.trim();if(t){let n=D(t);return S({source:`claude_project_dir`,scope:`project`,cwd:e,projectRoot:n,identity:`claude-project-dir:${n}`,projectName:r(n)||`project`})}let n=E([`remote`,`get-url`,`origin`],e);if(n){let t=E([`rev-parse`,`--show-toplevel`],e),r=O(n);return S({source:`git_remote`,scope:`project`,cwd:e,projectRoot:D(t||e),gitRemote:r,identity:`git-remote:${r}`,projectName:k(r)})}let i=E([`rev-parse`,`--show-toplevel`],e);if(i){let t=D(i);return S({source:`git_root`,scope:`project`,cwd:e,projectRoot:t,identity:`git-root:${t}`,projectName:r(t)||`project`})}return S({source:`global`,scope:`global`,cwd:e,projectRoot:void 0,identity:`global`,projectName:N})}function S(e){let t=e.scope===`global`?M:A(e.identity);return{projectId:t,projectName:e.projectName,scope:e.scope,source:e.source,cwd:D(e.cwd),projectRoot:e.projectRoot,gitRemote:e.gitRemote,storageDir:h(t)}}function C(e){let t=new Date().toISOString(),n=m(),r=w(n),i=r.projects[e.projectId],a={...e,firstSeenAt:i?.firstSeenAt??t,lastSeenAt:t};r.projects[e.projectId]=a,r.updatedAt=t,s(e.storageDir,{recursive:!0}),s(p(),{recursive:!0}),T(n,r),T(g(e.projectId),a)}function w(e){if(!o(e))return{version:j,updatedAt:new Date(0).toISOString(),projects:{}};try{let t=JSON.parse(c(e,`utf8`));if(t.version===j&&typeof t.projects==`object`&&t.projects)return{version:j,updatedAt:typeof t.updatedAt==`string`?t.updatedAt:new Date(0).toISOString(),projects:t.projects}}catch{}return{version:j,updatedAt:new Date(0).toISOString(),projects:{}}}function T(e,t){u(e,`${JSON.stringify(t,null,2)}\n`,`utf8`)}function E(e,t){try{return f(`git`,[`-C`,t,...e],{encoding:`utf8`,stdio:[`ignore`,`pipe`,`ignore`]}).trim()||null}catch{return null}}function D(e){let t=a(e);try{return l.native(t).normalize(`NFC`)}catch{return t.normalize(`NFC`)}}function O(e){let t=e.trim().replace(/\\/g,`/`);return t=t.replace(/\.git$/i,``),t=t.replace(/\/+$/g,``),t.toLowerCase()}function k(e){return e.match(/[:/]([^/:]+?)(?:\.git)?$/)?.[1]||`project`}function A(e){return`project-${d(`sha256`).update(e).digest(`hex`).slice(0,16)}`}var j,M,N,P,F,I,L,R,z=e((()=>{n(),j=1,M=`global`,N=`Global`,P=32,F=24,I=new Map,L=300*1e3,R=0}));export{z as a,v as c,p as i,h as n,b as o,m as r,y as s,g as t};