import a from './index5.mjs';
import { i as inquirer } from './index6.mjs';
import { i18n, resolveSupportedLanguage } from './index2.mjs';
import { searchSkills, getSkill, getAllSkills, addSkill, setSkillEnabled, removeSkill, getBatchCategories, createBatchSkills } from './manager.mjs';
import '../shared/ccjk.BAGoDD49.mjs';
import 'node:readline';
import 'stream';
import 'node:tty';
import 'node:process';
import 'node:async_hooks';
import '../shared/ccjk.Cjgrln_h.mjs';
import 'node:util';
import 'tty';
import 'fs';
import 'child_process';
import 'node:path';
import 'node:os';
import 'node:crypto';
import 'buffer';
import 'string_decoder';
import 'node:fs';
import 'node:url';
import '../shared/ccjk.bQ7Dh1g4.mjs';
import './constants.mjs';
import './skill.mjs';
import './fs-operations.mjs';
import 'node:fs/promises';

function resolveSkillsLanguage(lang) {
  return resolveSupportedLanguage(lang);
}
async function listSkills(options = {}) {
  const lang = resolveSkillsLanguage(options.lang);
  console.log("");
  console.log(a.bold.cyan("\u2501".repeat(60)));
  console.log(a.bold.cyan(`  ${i18n.t("skills:title.list")}`));
  console.log(a.bold.cyan("\u2501".repeat(60)));
  console.log("");
  try {
    const skills = searchSkills({
      category: options.category,
      enabled: options.showDisabled ? void 0 : true
    });
    if (skills.length === 0) {
      console.log(a.yellow(`  ${i18n.t("skills:message.noSkills")}`));
      console.log("");
      console.log(a.dim(`  ${i18n.t("skills:hint.createSkill")}`));
      console.log(a.dim(`    ccjk skills create <name>`));
      console.log("");
      return;
    }
    const byCategory = /* @__PURE__ */ new Map();
    for (const skill of skills) {
      const categorySkills = byCategory.get(skill.category) || [];
      categorySkills.push(skill);
      byCategory.set(skill.category, categorySkills);
    }
    for (const [category, categorySkills] of byCategory) {
      console.log(a.bold.green(`  ${getCategoryIcon(category)} ${i18n.t(`skills:category.${category}`)}`));
      console.log("");
      for (const skill of categorySkills) {
        const statusIcon = skill.enabled ? a.green("\u2713") : a.dim("\u25CB");
        const name = skill.name[lang];
        const description = skill.description[lang];
        const triggers = skill.triggers.map((t) => a.green(t)).join(", ");
        console.log(`  ${statusIcon} ${a.bold(name)} ${a.dim(`(${skill.id})`)}`);
        console.log(`    ${a.dim(description)}`);
        console.log(`    ${a.dim(i18n.t("skills:label.triggers"))}: ${triggers}`);
        if (skill.tags && skill.tags.length > 0) {
          const tags = skill.tags.map((tag) => a.bgGray.white(` ${tag} `)).join(" ");
          console.log(`    ${tags}`);
        }
        console.log("");
      }
    }
    console.log(a.dim(`  ${i18n.t("skills:message.totalSkills", { count: skills.length })}`));
    console.log("");
  } catch (error) {
    console.error(a.red(`
  ${i18n.t("skills:error.listFailed")}: ${error}`));
    throw error;
  }
}
async function runSkill(skillName, options = {}) {
  const lang = resolveSkillsLanguage(options.lang);
  console.log("");
  console.log(a.bold.cyan("\u2501".repeat(60)));
  console.log(a.bold.cyan(`  ${i18n.t("skills:title.run")}`));
  console.log(a.bold.cyan("\u2501".repeat(60)));
  console.log("");
  try {
    let skill = getSkill(skillName);
    if (!skill) {
      const allSkills = getAllSkills();
      skill = allSkills.find((s) => s.triggers.includes(skillName) || s.triggers.includes(`/${skillName}`));
    }
    if (!skill) {
      console.error(a.red(`  ${i18n.t("skills:error.skillNotFound", { name: skillName })}`));
      console.log("");
      console.log(a.dim(`  ${i18n.t("skills:hint.listSkills")}`));
      console.log(a.dim(`    ccjk skills list`));
      console.log("");
      return;
    }
    if (!skill.enabled) {
      console.warn(a.yellow(`  ${i18n.t("skills:warning.skillDisabled", { name: skill.name[lang] })}`));
      console.log("");
      console.log(a.dim(`  ${i18n.t("skills:hint.enableSkill")}`));
      console.log(a.dim(`    ccjk skills enable ${skill.id}`));
      console.log("");
      return;
    }
    console.log(a.bold(`  ${skill.name[lang]}`));
    console.log(a.dim(`  ${skill.description[lang]}`));
    console.log("");
    console.log(a.bold.green(`  ${i18n.t("skills:label.template")}:`));
    console.log("");
    console.log(a.dim("  \u2500".repeat(30)));
    console.log(skill.template.split("\n").map((line) => `  ${line}`).join("\n"));
    console.log(a.dim("  \u2500".repeat(30)));
    console.log("");
    console.log(a.green(`  \u2713 ${i18n.t("skills:message.skillExecuted")}`));
    console.log("");
    console.log(a.dim(`  ${i18n.t("skills:hint.copyTemplate")}`));
    console.log("");
  } catch (error) {
    console.error(a.red(`
  ${i18n.t("skills:error.runFailed")}: ${error}`));
    throw error;
  }
}
async function showSkillInfo(skillName, options = {}) {
  const lang = resolveSkillsLanguage(options.lang);
  console.log("");
  console.log(a.bold.cyan("\u2501".repeat(60)));
  console.log(a.bold.cyan(`  ${i18n.t("skills:title.info")}`));
  console.log(a.bold.cyan("\u2501".repeat(60)));
  console.log("");
  try {
    const skill = getSkill(skillName);
    if (!skill) {
      console.error(a.red(`  ${i18n.t("skills:error.skillNotFound", { name: skillName })}`));
      console.log("");
      return;
    }
    const name = skill.name[lang];
    const description = skill.description[lang];
    const statusBadge = skill.enabled ? a.bgGreen.white(" ENABLED ") : a.bgRed.white(" DISABLED ");
    console.log(`${a.bold.green(`  ${name}`)} ${statusBadge}`);
    console.log(a.dim(`  ${description}`));
    console.log("");
    console.log(a.bold(`  ${i18n.t("skills:label.details")}:`));
    console.log(a.dim(`    ${i18n.t("skills:label.id")}: ${skill.id}`));
    console.log(a.dim(`    ${i18n.t("skills:label.version")}: ${skill.version}`));
    console.log(a.dim(`    ${i18n.t("skills:label.category")}: ${i18n.t(`skills:category.${skill.category}`)}`));
    if (skill.author) {
      console.log(a.dim(`    ${i18n.t("skills:label.author")}: ${skill.author}`));
    }
    console.log("");
    console.log(a.bold(`  ${i18n.t("skills:label.triggers")}:`));
    for (const trigger of skill.triggers) {
      console.log(a.green(`    ${trigger}`));
    }
    if (skill.tags && skill.tags.length > 0) {
      console.log("");
      console.log(a.bold(`  ${i18n.t("skills:label.tags")}:`));
      const tags = skill.tags.map((tag) => a.bgGray.white(` ${tag} `)).join(" ");
      console.log(`    ${tags}`);
    }
    if (skill.agents && skill.agents.length > 0) {
      console.log("");
      console.log(a.bold(`  ${i18n.t("skills:label.agents")}:`));
      for (const agent of skill.agents) {
        console.log(a.dim(`    - ${agent}`));
      }
    }
    console.log("");
    console.log(a.bold(`  ${i18n.t("skills:label.template")}:`));
    console.log(a.dim(`    ${skill.template.length} ${i18n.t("skills:label.characters")}`));
    console.log("");
  } catch (error) {
    console.error(a.red(`
  ${i18n.t("skills:error.infoFailed")}: ${error}`));
    throw error;
  }
}
async function createSkill(skillName, options = {}) {
  const lang = resolveSkillsLanguage(options.lang);
  console.log("");
  console.log(a.bold.cyan("\u2501".repeat(60)));
  console.log(a.bold.cyan(`  ${i18n.t("skills:title.create")}`));
  console.log(a.bold.cyan("\u2501".repeat(60)));
  console.log("");
  try {
    if (options.batch) {
      await createBatchSkillsInteractive(options);
      return;
    }
    const existing = getSkill(skillName);
    if (existing) {
      console.error(a.red(`  ${i18n.t("skills:error.skillExists", { name: skillName })}`));
      console.log("");
      return;
    }
    const answers = await inquirer.prompt([
      {
        type: "input",
        name: "nameEn",
        message: i18n.t("skills:prompt.nameEn"),
        default: skillName,
        validate: (input) => input.length > 0 || i18n.t("skills:error.nameRequired")
      },
      {
        type: "input",
        name: "nameZh",
        message: i18n.t("skills:prompt.nameZh"),
        default: skillName
      },
      {
        type: "input",
        name: "descriptionEn",
        message: i18n.t("skills:prompt.descriptionEn"),
        validate: (input) => input.length > 0 || i18n.t("skills:error.descriptionRequired")
      },
      {
        type: "input",
        name: "descriptionZh",
        message: i18n.t("skills:prompt.descriptionZh")
      },
      {
        type: "list",
        name: "category",
        message: i18n.t("skills:prompt.category"),
        choices: [
          { name: i18n.t("skills:category.git"), value: "git" },
          { name: i18n.t("skills:category.dev"), value: "dev" },
          { name: i18n.t("skills:category.testing"), value: "testing" },
          { name: i18n.t("skills:category.docs"), value: "docs" },
          { name: i18n.t("skills:category.review"), value: "review" },
          { name: i18n.t("skills:category.seo"), value: "seo" },
          { name: i18n.t("skills:category.devops"), value: "devops" },
          { name: i18n.t("skills:category.custom"), value: "custom" }
        ],
        default: options.category || "custom"
      },
      {
        type: "input",
        name: "triggers",
        message: i18n.t("skills:prompt.triggers"),
        default: `/${skillName}`,
        validate: (input) => input.length > 0 || i18n.t("skills:error.triggersRequired")
      },
      {
        type: "editor",
        name: "template",
        message: i18n.t("skills:prompt.template"),
        default: getDefaultTemplate(skillName, lang)
      }
    ]);
    const skill = {
      id: skillName,
      name: {
        "en": answers.nameEn,
        "zh-CN": answers.nameZh || answers.nameEn
      },
      description: {
        "en": answers.descriptionEn,
        "zh-CN": answers.descriptionZh || answers.descriptionEn
      },
      category: answers.category,
      triggers: answers.triggers.split(",").map((t) => t.trim()),
      template: answers.template,
      enabled: true,
      version: "1.0.0"
    };
    const result = addSkill(skill);
    if (result.success) {
      console.log(a.green(`
  \u2713 ${i18n.t("skills:message.skillCreated", { name: skill.name[lang] })}`));
      console.log(a.dim(`    ${i18n.t("skills:label.path")}: ${result.path}`));
      console.log("");
      console.log(a.dim(`  ${i18n.t("skills:hint.runSkill")}`));
      console.log(a.dim(`    ccjk skills run ${skillName}`));
      console.log("");
    } else {
      console.error(a.red(`
  ${i18n.t("skills:error.createFailed")}: ${result.error}`));
    }
  } catch (error) {
    console.error(a.red(`
  ${i18n.t("skills:error.createFailed")}: ${error}`));
    throw error;
  }
}
async function enableSkill(skillName, options = {}) {
  try {
    const lang = resolveSkillsLanguage(options.lang);
    const skill = getSkill(skillName);
    if (!skill) {
      console.error(a.red(`  ${i18n.t("skills:error.skillNotFound", { name: skillName })}`));
      return;
    }
    if (skill.enabled) {
      console.log(a.yellow(`  ${i18n.t("skills:message.alreadyEnabled", { name: skill.name[lang] })}`));
      return;
    }
    const success = setSkillEnabled(skillName, true);
    if (success) {
      console.log(a.green(`  \u2713 ${i18n.t("skills:message.skillEnabled", { name: skill.name[lang] })}`));
    } else {
      console.error(a.red(`  ${i18n.t("skills:error.enableFailed")}`));
    }
  } catch (error) {
    console.error(a.red(`
  ${i18n.t("skills:error.enableFailed")}: ${error}`));
    throw error;
  }
}
async function disableSkill(skillName, options = {}) {
  try {
    const lang = resolveSkillsLanguage(options.lang);
    const skill = getSkill(skillName);
    if (!skill) {
      console.error(a.red(`  ${i18n.t("skills:error.skillNotFound", { name: skillName })}`));
      return;
    }
    if (!skill.enabled) {
      console.log(a.yellow(`  ${i18n.t("skills:message.alreadyDisabled", { name: skill.name[lang] })}`));
      return;
    }
    const success = setSkillEnabled(skillName, false);
    if (success) {
      console.log(a.green(`  \u2713 ${i18n.t("skills:message.skillDisabled", { name: skill.name[lang] })}`));
    } else {
      console.error(a.red(`  ${i18n.t("skills:error.disableFailed")}`));
    }
  } catch (error) {
    console.error(a.red(`
  ${i18n.t("skills:error.disableFailed")}: ${error}`));
    throw error;
  }
}
async function deleteSkill(skillName, options = {}) {
  try {
    const lang = resolveSkillsLanguage(options.lang);
    const skill = getSkill(skillName);
    if (!skill) {
      console.error(a.red(`  ${i18n.t("skills:error.skillNotFound", { name: skillName })}`));
      return;
    }
    const { confirm } = await inquirer.prompt({
      type: "confirm",
      name: "confirm",
      message: i18n.t("skills:prompt.confirmDelete", { name: skill.name[lang] }),
      default: false
    });
    if (!confirm) {
      console.log(a.yellow(`  ${i18n.t("skills:message.deleteCancelled")}`));
      return;
    }
    const success = removeSkill(skillName);
    if (success) {
      console.log(a.green(`  \u2713 ${i18n.t("skills:message.skillDeleted", { name: skill.name[lang] })}`));
    } else {
      console.error(a.red(`  ${i18n.t("skills:error.deleteFailed")}`));
    }
  } catch (error) {
    console.error(a.red(`
  ${i18n.t("skills:error.deleteFailed")}: ${error}`));
    throw error;
  }
}
async function skillsMenu(options = {}) {
  while (true) {
    console.log("");
    console.log(a.bold.cyan("\u2501".repeat(60)));
    console.log(a.bold.cyan(`  ${i18n.t("skills:menu.title")}`));
    console.log(a.bold.cyan("\u2501".repeat(60)));
    console.log("");
    const { action } = await inquirer.prompt({
      type: "list",
      name: "action",
      message: i18n.t("skills:menu.prompt"),
      choices: [
        { name: `\u{1F4CB} ${i18n.t("skills:menu.list")}`, value: "list" },
        { name: `\u25B6\uFE0F  ${i18n.t("skills:menu.run")}`, value: "run" },
        { name: `\u2139\uFE0F  ${i18n.t("skills:menu.info")}`, value: "info" },
        { name: `\u2795 ${i18n.t("skills:menu.create")}`, value: "create" },
        { name: `\u{1F4E6} ${i18n.t("skills:menu.batch")}`, value: "batch" },
        { name: `\u2705 ${i18n.t("skills:menu.enable")}`, value: "enable" },
        { name: `\u274C ${i18n.t("skills:menu.disable")}`, value: "disable" },
        { name: `\u{1F5D1}\uFE0F  ${i18n.t("skills:menu.delete")}`, value: "delete" },
        new inquirer.Separator(),
        { name: `\u{1F519} ${i18n.t("skills:menu.back")}`, value: "back" }
      ]
    });
    if (action === "back") {
      break;
    }
    try {
      switch (action) {
        case "list":
          await listSkills(options);
          break;
        case "run": {
          const { skillName } = await inquirer.prompt({
            type: "input",
            name: "skillName",
            message: i18n.t("skills:prompt.skillName")
          });
          await runSkill(skillName, options);
          break;
        }
        case "info": {
          const { skillName } = await inquirer.prompt({
            type: "input",
            name: "skillName",
            message: i18n.t("skills:prompt.skillName")
          });
          await showSkillInfo(skillName, options);
          break;
        }
        case "create": {
          const { skillName } = await inquirer.prompt({
            type: "input",
            name: "skillName",
            message: i18n.t("skills:prompt.newSkillName")
          });
          await createSkill(skillName, options);
          break;
        }
        case "batch":
          await createBatchSkillsInteractive(options);
          break;
        case "enable": {
          const { skillName } = await inquirer.prompt({
            type: "input",
            name: "skillName",
            message: i18n.t("skills:prompt.skillName")
          });
          await enableSkill(skillName, options);
          break;
        }
        case "disable": {
          const { skillName } = await inquirer.prompt({
            type: "input",
            name: "skillName",
            message: i18n.t("skills:prompt.skillName")
          });
          await disableSkill(skillName, options);
          break;
        }
        case "delete": {
          const { skillName } = await inquirer.prompt({
            type: "input",
            name: "skillName",
            message: i18n.t("skills:prompt.skillName")
          });
          await deleteSkill(skillName, options);
          break;
        }
      }
    } catch (error) {
      console.error(a.red(`
  ${i18n.t("common.error")}: ${error}`));
    }
    await inquirer.prompt({
      type: "input",
      name: "continue",
      message: i18n.t("common.pressEnterToContinue")
    });
  }
}
function getCategoryIcon(category) {
  const icons = {
    git: "\u{1F500}",
    dev: "\u{1F4BB}",
    testing: "\u{1F9EA}",
    docs: "\u{1F4DA}",
    review: "\u{1F440}",
    seo: "\u{1F50D}",
    devops: "\u{1F680}",
    custom: "\u2699\uFE0F",
    debug: "\u{1F41B}",
    planning: "\u{1F4CB}"
  };
  return icons[category] || "\u{1F4E6}";
}
function getDefaultTemplate(skillName, lang) {
  const isZh = lang === "zh-CN";
  if (isZh) {
    return `# ${skillName}

## \u6280\u80FD\u63CF\u8FF0

\u8FD9\u662F\u4E00\u4E2A\u81EA\u5B9A\u4E49\u6280\u80FD\u6A21\u677F\u3002\u8BF7\u63CF\u8FF0\u8FD9\u4E2A\u6280\u80FD\u7684\u7528\u9014\u548C\u529F\u80FD\u3002

## \u4F7F\u7528\u573A\u666F

- \u573A\u666F 1
- \u573A\u666F 2
- \u573A\u666F 3

## \u6267\u884C\u6B65\u9AA4

1. \u7B2C\u4E00\u6B65
2. \u7B2C\u4E8C\u6B65
3. \u7B2C\u4E09\u6B65

## \u6CE8\u610F\u4E8B\u9879

- \u6CE8\u610F\u4E8B\u9879 1
- \u6CE8\u610F\u4E8B\u9879 2
`;
  }
  return `# ${skillName}

## Skill Description

This is a custom skill template. Please describe the purpose and functionality of this skill.

## Use Cases

- Use case 1
- Use case 2
- Use case 3

## Execution Steps

1. Step 1
2. Step 2
3. Step 3

## Notes

- Note 1
- Note 2
`;
}
async function createBatchSkillsInteractive(_options) {
  console.log(a.bold(`  ${i18n.t("skills:batch.title")}`));
  console.log("");
  const categories = getBatchCategories();
  const answers = await inquirer.prompt([
    {
      type: "checkbox",
      name: "categories",
      message: i18n.t("skills:batch.prompt"),
      choices: categories.map((cat) => ({
        name: i18n.t(`skills:batch.${cat}`),
        value: cat,
        checked: false
      }))
    }
  ]);
  if (answers.categories.length === 0) {
    console.log(a.yellow(`  ${i18n.t("skills:batch.noneSelected")}`));
    return;
  }
  console.log("");
  console.log(a.dim(`  ${i18n.t("skills:batch.creating")}...`));
  console.log("");
  const results = createBatchSkills({
    lang: answers.categories.includes("typescript") ? "typescript" : answers.categories.includes("python") ? "python" : void 0,
    seo: answers.categories.includes("seo"),
    devops: answers.categories.includes("devops")
  });
  const succeeded = results.filter((r) => r.success).length;
  const failed = results.filter((r) => !r.success).length;
  console.log(a.green(`  \u2713 ${i18n.t("skills:batch.created", { count: succeeded })}`));
  if (failed > 0) {
    console.log(a.red(`  \u2717 ${i18n.t("skills:batch.failed", { count: failed })}`));
  }
  console.log("");
}

export { createSkill, deleteSkill, disableSkill, enableSkill, listSkills, runSkill, showSkillInfo, skillsMenu };
