// src/skills/index.ts
function inlineResource(name, content) {
  return { kind: "inline", name, content };
}
function skillMountPath(id) {
  return `~/.claude/skills/${id}/SKILL.md`;
}
function normalizeKey(key, anchor) {
  const marker = `${anchor}/`;
  const at = key.lastIndexOf(marker);
  if (at >= 0) return key.slice(at);
  return key.startsWith("./") ? key.slice(2) : key;
}
function toCorpusId(normalizedKey, anchor) {
  const nested = normalizedKey.match(new RegExp(`${anchor}/([^/]+)/SKILL\\.md$`));
  if (nested) return nested[1];
  const flat = normalizedKey.match(new RegExp(`${anchor}/(.+)\\.md$`));
  if (flat) return flat[1];
  return normalizedKey;
}
function nodeBuiltins() {
  const getBuiltin = globalThis.process?.getBuiltinModule;
  if (typeof getBuiltin !== "function") return void 0;
  return {
    fs: getBuiltin("node:fs"),
    path: getBuiltin("node:path"),
    url: getBuiltin("node:url")
  };
}
function fsWalkFlat(builtins, root) {
  const { fs, path } = builtins;
  const out = {};
  if (!fs.existsSync(root)) return out;
  const walk = (dir) => {
    let entries;
    try {
      entries = fs.readdirSync(dir, { withFileTypes: true });
    } catch {
      return;
    }
    for (const entry of entries) {
      if (entry.name.startsWith(".")) continue;
      const full = path.join(dir, entry.name);
      if (entry.isDirectory()) walk(full);
      else if (entry.isFile() && entry.name.endsWith(".md")) out[full] = fs.readFileSync(full, "utf8");
    }
  };
  walk(root);
  return out;
}
function fsWalkNested(builtins, root) {
  const { fs, path } = builtins;
  const out = {};
  if (!fs.existsSync(root)) return out;
  let entries;
  try {
    entries = fs.readdirSync(root, { withFileTypes: true });
  } catch {
    return out;
  }
  for (const entry of entries) {
    if (!entry.isDirectory()) continue;
    const skillFile = path.join(root, entry.name, "SKILL.md");
    if (!fs.existsSync(skillFile)) continue;
    out[skillFile] = fs.readFileSync(skillFile, "utf8");
  }
  return out;
}
function resolveFsBase(builtins, fsBaseDir, importMetaUrl) {
  const { path, url } = builtins;
  if (path.isAbsolute(fsBaseDir)) return fsBaseDir;
  const here = importMetaUrl ? path.dirname(url.fileURLToPath(importMetaUrl)) : process.cwd();
  return path.join(here, fsBaseDir);
}
function loadMarkdownCorpus(options, importMetaUrl) {
  const { anchor, globModules, fsBaseDir, fsLayout = "flat", skip } = options;
  let modules;
  let source;
  if (globModules && Object.keys(globModules).length > 0) {
    modules = globModules;
    source = "vite";
  } else {
    const builtins = nodeBuiltins();
    if (builtins && fsBaseDir) {
      const root = resolveFsBase(builtins, fsBaseDir, importMetaUrl);
      modules = fsLayout === "nested" ? fsWalkNested(builtins, root) : fsWalkFlat(builtins, root);
      source = Object.keys(modules).length > 0 ? "fs" : "empty";
    } else {
      modules = {};
      source = "empty";
    }
  }
  const entries = [];
  for (const [rawKey, content] of Object.entries(modules)) {
    if (typeof content !== "string") continue;
    const key = normalizeKey(rawKey, anchor);
    if (skip && skip(key)) continue;
    entries.push({ id: toCorpusId(key, anchor), key, content });
  }
  entries.sort((a, b) => a.id.localeCompare(b.id));
  return { source, entries };
}
function corpusSkills(corpus, anchor) {
  return corpus.map(
    (entry) => ({
      path: `${anchor}/${entry.id}.md`,
      resource: inlineResource(`${anchor}-${entry.id}`, entry.content)
    })
  ).sort((a, b) => a.path.localeCompare(b.path));
}
function registrySkills(registry, tier = "free") {
  return registry.filter((s) => s.tier === tier).map(
    (s) => ({
      path: skillMountPath(s.id),
      resource: inlineResource(s.id, s.skillMd)
    })
  ).sort((a, b) => a.path.localeCompare(b.path));
}
function composeShellResources(input) {
  const { skills = [], knowledge = [], evolvable = [], registry = [], predicate } = input;
  const composed = [...skills, ...knowledge, ...evolvable, ...registry];
  return predicate ? composed.filter(predicate) : composed;
}

export {
  skillMountPath,
  loadMarkdownCorpus,
  corpusSkills,
  registrySkills,
  composeShellResources
};
//# sourceMappingURL=chunk-KOG473C4.js.map