// src/actions/skills/installed.ts
import {
  existsSync,
  readdirSync,
  readFileSync,
  statSync
} from "node:fs";
import PATH from "node:path";
var PROJECT_SKILL_DIRS = [".claude/skills", ".agents/skills"];
var VERSION_PATTERN = /metadata:\s*\n(?:[ \t]+[^\n]+\n)*?[ \t]+version:\s*([^\s#]+)/;
var listSkillDirs = (root) => {
  if (!existsSync(root)) return [];
  return readdirSync(root).filter(
    (f) => statSync(PATH.resolve(root, f)).isDirectory()
  );
};
var recordInstalledSkill = (installed, relativeDir, name, version) => {
  const existing = installed.get(name);
  if (existing == null) {
    installed.set(name, {
      locations: [relativeDir],
      name,
      version
    });
    return;
  }
  existing.locations.push(relativeDir);
  if (existing.version == null && version != null) {
    existing.version = version;
  }
};
var readSkillVersion = (skillDir) => {
  const skillMdPath = PATH.resolve(skillDir, "SKILL.md");
  if (!existsSync(skillMdPath)) return void 0;
  try {
    const content = readFileSync(skillMdPath, "utf8");
    const match = VERSION_PATTERN.exec(content);
    return match?.[1]?.trim();
  } catch {
    return void 0;
  }
};
var scanInstalledSkills = (cwd, projectDirs = PROJECT_SKILL_DIRS) => {
  const installed = /* @__PURE__ */ new Map();
  for (const relativeDir of projectDirs) {
    const root = PATH.resolve(cwd, relativeDir);
    for (const name of listSkillDirs(root)) {
      const version = readSkillVersion(PATH.resolve(root, name));
      recordInstalledSkill(installed, relativeDir, name, version);
    }
  }
  return installed;
};
var listSkillDirsAt = (root) => {
  return listSkillDirs(root);
};
export {
  PROJECT_SKILL_DIRS,
  listSkillDirsAt,
  readSkillVersion,
  scanInstalledSkills
};
//# sourceMappingURL=installed.mjs.map
