// src/actions/skills/lint.ts
import {
  existsSync,
  readdirSync,
  statSync
} from "node:fs";
import OS from "node:os";
import PATH from "node:path";
import chalk from "chalk";

// src/lib/crossSpawn.ts
import crossSpawn from "cross-spawn";
function spawn(command, args, options) {
  return crossSpawn(command, args, options);
}

// src/lib/initCwd.ts
function INIT_CWD() {
  return process.env.INIT_CWD ?? process.cwd();
}

// src/actions/skills/lint.ts
var listSkillDirs = (root) => {
  if (!existsSync(root)) return [];
  return readdirSync(root).filter(
    (f) => statSync(PATH.resolve(root, f)).isDirectory()
  );
};
var skillsLint = (options = {}) => {
  const projectDir = options.projectDir ?? PATH.resolve(INIT_CWD(), ".claude", "skills");
  const globalDir = options.globalDir ?? PATH.resolve(OS.homedir(), ".claude", "skills");
  const projectSkills = listSkillDirs(projectDir);
  const globalSkills = new Set(listSkillDirs(globalDir));
  const duplicates = projectSkills.filter((name) => globalSkills.has(name)).toSorted();
  if (duplicates.length === 0) {
    console.log(chalk.gray("skills: no project/global duplicates"));
    return 0;
  }
  for (const name of duplicates) {
    console.log(chalk.yellow(`skills: '${name}' is installed in both project and global scope`));
  }
  console.log(chalk.yellow(`skills: ${duplicates.length} duplicate(s) found`));
  return 1;
};

// src/actions/skills/passthrough.ts
var NPX_SKILLS_PATTERN = /(?<=\[[\d;]*m|\W|^)npx(?:\s+-y)?\s+skills(?=\W|$)/g;
var rewriteSkillsRefs = (text) => text.replaceAll(NPX_SKILLS_PATTERN, "xy skills");
var runSkills = (subcommand, args, options = {}) => {
  const stdio = options.interactive ? "inherit" : ["inherit", "pipe", "pipe"];
  return new Promise((resolve) => {
    const child = spawn("npx", ["-y", "skills", subcommand, ...args], {
      env: { FORCE_COLOR: "3", ...process.env },
      stdio
    });
    if (!options.interactive) {
      child.stdout?.setEncoding("utf8");
      child.stderr?.setEncoding("utf8");
      child.stdout?.on("data", (chunk) => {
        process.stdout.write(rewriteSkillsRefs(chunk));
      });
      child.stderr?.on("data", (chunk) => {
        process.stderr.write(rewriteSkillsRefs(chunk));
      });
    }
    child.on("error", () => resolve(1));
    child.on("close", (code) => resolve(code ?? 1));
  });
};
var argvAfterSkillsSubcommand = (canonicalName, aliases = []) => {
  const argv = process.argv;
  const skillsIdx = argv.indexOf("skills");
  if (skillsIdx === -1) return [];
  const names = /* @__PURE__ */ new Set([canonicalName, ...aliases]);
  for (let i = skillsIdx + 1; i < argv.length; i++) {
    const token = argv[i];
    if (token !== void 0 && names.has(token)) {
      return argv.slice(i + 1);
    }
  }
  return [];
};

// src/actions/skills/remove.ts
var INTERACTIVE_FLAG_PATTERN = /^--(?:no-)?interactive(?:=.+)?$/;
var shouldUseInteractiveRemove = (argv) => {
  if (argv.interactive === true) return true;
  if (Array.isArray(argv.skills) && argv.skills.length > 0) return false;
  if (argv.all === true) return false;
  if (Array.isArray(argv.skill) && argv.skill.length > 0) return false;
  return true;
};

// src/actions/skills/updo.ts
var SCOPE_FLAGS = /* @__PURE__ */ new Set(["-g", "--global", "-p", "--project"]);
var hasSkillsScopeFlag = (args) => args.some((a) => SCOPE_FLAGS.has(a));
var skillsUpdo = (args) => {
  const finalArgs = hasSkillsScopeFlag(args) ? args : ["-p", ...args];
  return runSkills("update", finalArgs);
};
export {
  INTERACTIVE_FLAG_PATTERN,
  argvAfterSkillsSubcommand,
  hasSkillsScopeFlag,
  rewriteSkillsRefs,
  runSkills,
  shouldUseInteractiveRemove,
  skillsLint,
  skillsUpdo
};
//# sourceMappingURL=index.mjs.map
