import { existsSync, readdirSync } from 'node:fs';
import { r as readMcpConfig, f as fixWindowsMcpConfig, m as mergeMcpServers, w as writeMcpConfig, s as syncMcpPermissions, M as MCP_SERVICE_CONFIGS, b as buildMcpServerConfig } from '../chunks/claude-config.mjs';
import { s as selectAndInstallWorkflows, W as WORKFLOW_CONFIG_BASE } from './ccjk.BLsIiTqO.mjs';
import { i as isCcrInstalled, a as installCcr } from './ccjk.DFRPtmK_.mjs';
import { readJsonConfig } from '../chunks/json-config.mjs';
import { c as copyOutputStyles, s as setGlobalDefaultOutputStyle, g as getAvailableOutputStyles } from './ccjk.C0WLUnFV.mjs';
import { c as commandExists } from '../chunks/platform.mjs';
import { r as resolveClaudeFamilySettingsTarget } from './ccjk.DDL-4C-k.mjs';
import { j as join } from './ccjk.bQ7Dh1g4.mjs';

const DEFAULT_CONFIG_LANG = "en";
const OUTPUT_STYLE_TEMPLATE_LANG = "zh-CN";
function getCoreWorkflowIds() {
  return WORKFLOW_CONFIG_BASE.filter((workflow) => workflow.defaultSelected).sort((a, b) => a.order - b.order).map((workflow) => workflow.id);
}
function getCoreMcpServiceIds() {
  return MCP_SERVICE_CONFIGS.filter((service) => service.defaultSelected && !service.requiresApiKey).map((service) => service.id);
}
function getCoreOutputStyleIds() {
  return getAvailableOutputStyles().filter((style) => style.isCustom).map((style) => style.id);
}
function getCoreMcpPermission(serviceId) {
  return `mcp__${serviceId.toLowerCase().replace(/-/g, "_")}__*`;
}
function listMarkdownFiles(dir) {
  if (!existsSync(dir)) {
    return [];
  }
  try {
    return readdirSync(dir, { recursive: true }).map((entry) => String(entry)).filter((entry) => entry.endsWith(".md"));
  } catch {
    return [];
  }
}
function readPermissionAllow(codeTool) {
  const target = resolveClaudeFamilySettingsTarget(codeTool);
  const settings = readJsonConfig(target.settingsFile) || {};
  return Array.isArray(settings.permissions?.allow) ? settings.permissions.allow.filter((item) => typeof item === "string") : [];
}
async function inspectClaudeFamilyCoreFeatures(codeTool) {
  const target = resolveClaudeFamilySettingsTarget(codeTool);
  const expectedWorkflows = getCoreWorkflowIds();
  const expectedMcpServices = getCoreMcpServiceIds();
  const expectedOutputStyles = getCoreOutputStyleIds();
  const commandFiles = listMarkdownFiles(join(target.configDir, "commands", "ccjk"));
  const installedMcp = Object.keys(readMcpConfig(target.codeTool)?.mcpServers || {});
  const allow = readPermissionAllow(target.codeTool);
  const styleFiles = listMarkdownFiles(join(target.configDir, "output-styles"));
  const ccrStatus = await isCcrInstalled().catch(() => ({ isInstalled: false, hasCorrectPackage: false }));
  return {
    workflows: {
      installed: commandFiles.length,
      expected: expectedWorkflows.length,
      missing: commandFiles.length > 0 ? [] : expectedWorkflows
    },
    mcp: {
      installed: installedMcp,
      expected: expectedMcpServices,
      missing: expectedMcpServices.filter((serviceId) => !installedMcp.includes(serviceId))
    },
    permissions: {
      allowCount: allow.length,
      missing: expectedMcpServices.map(getCoreMcpPermission).filter((permission) => !allow.includes(permission))
    },
    outputStyles: {
      installed: styleFiles.length,
      expected: expectedOutputStyles.length,
      missing: expectedOutputStyles.filter((styleId) => !styleFiles.includes(`${styleId}.md`))
    },
    ccr: {
      installed: ccrStatus.isInstalled,
      hasCorrectPackage: ccrStatus.hasCorrectPackage
    }
  };
}
async function ensureWorkflows(codeTool, configLang) {
  const before = await inspectClaudeFamilyCoreFeatures(codeTool);
  if (before.workflows.installed > 0) {
    return {
      feature: "workflows",
      status: "already-present",
      message: `${before.workflows.installed} workflow command(s) installed`
    };
  }
  try {
    await selectAndInstallWorkflows(configLang, getCoreWorkflowIds(), { codeToolType: codeTool });
    const after = await inspectClaudeFamilyCoreFeatures(codeTool);
    return {
      feature: "workflows",
      status: after.workflows.installed > 0 ? "installed" : "failed",
      message: `${after.workflows.installed} workflow command(s) installed`,
      details: getCoreWorkflowIds()
    };
  } catch (error) {
    return {
      feature: "workflows",
      status: "failed",
      message: "Workflow installation failed",
      error: error instanceof Error ? error.message : String(error)
    };
  }
}
function buildCoreMcpServers() {
  const servers = {};
  for (const serviceId of getCoreMcpServiceIds()) {
    const service = MCP_SERVICE_CONFIGS.find((item) => item.id === serviceId);
    if (!service) {
      continue;
    }
    servers[service.id] = buildMcpServerConfig(service.config);
  }
  return servers;
}
async function ensureMcp(codeTool) {
  const before = await inspectClaudeFamilyCoreFeatures(codeTool);
  if (before.mcp.missing.length === 0) {
    return {
      feature: "mcp",
      status: "already-present",
      message: `${before.mcp.installed.length} MCP service(s) configured`,
      details: before.mcp.installed
    };
  }
  try {
    const existingConfig = readMcpConfig(codeTool);
    const mergedConfig = fixWindowsMcpConfig(mergeMcpServers(existingConfig, buildCoreMcpServers()));
    writeMcpConfig(mergedConfig, codeTool);
    syncMcpPermissions(codeTool);
    const after = await inspectClaudeFamilyCoreFeatures(codeTool);
    return {
      feature: "mcp",
      status: after.mcp.missing.length === 0 ? "installed" : "repaired",
      message: `${after.mcp.installed.length} MCP service(s) configured`,
      details: after.mcp.installed
    };
  } catch (error) {
    return {
      feature: "mcp",
      status: "failed",
      message: "MCP configuration failed",
      error: error instanceof Error ? error.message : String(error)
    };
  }
}
async function ensurePermissions(codeTool) {
  try {
    syncMcpPermissions(codeTool);
    const after = await inspectClaudeFamilyCoreFeatures(codeTool);
    return {
      feature: "permissions",
      status: after.permissions.missing.length === 0 ? "repaired" : "failed",
      message: `${after.permissions.allowCount} permission rule(s) configured`,
      details: after.permissions.missing.length > 0 ? after.permissions.missing : void 0
    };
  } catch (error) {
    return {
      feature: "permissions",
      status: "failed",
      message: "Permission repair failed",
      error: error instanceof Error ? error.message : String(error)
    };
  }
}
async function ensureOutputStyles(codeTool) {
  const styleIds = getCoreOutputStyleIds();
  const before = await inspectClaudeFamilyCoreFeatures(codeTool);
  if (before.outputStyles.missing.length === 0) {
    return {
      feature: "output-styles",
      status: "already-present",
      message: `${before.outputStyles.installed} output style(s) installed`
    };
  }
  try {
    await copyOutputStyles(styleIds, OUTPUT_STYLE_TEMPLATE_LANG, codeTool);
    setGlobalDefaultOutputStyle(styleIds.includes("linus-mode") ? "linus-mode" : styleIds[0], codeTool);
    const after = await inspectClaudeFamilyCoreFeatures(codeTool);
    return {
      feature: "output-styles",
      status: after.outputStyles.missing.length === 0 ? "installed" : "repaired",
      message: `${after.outputStyles.installed} output style(s) installed`,
      details: styleIds
    };
  } catch (error) {
    return {
      feature: "output-styles",
      status: "failed",
      message: "Output style installation failed",
      error: error instanceof Error ? error.message : String(error)
    };
  }
}
async function ensureCcr(install) {
  try {
    const before = await isCcrInstalled();
    if (before.hasCorrectPackage) {
      return {
        feature: "ccr",
        status: "already-present",
        message: "CCR proxy package installed"
      };
    }
    if (!install) {
      return {
        feature: "ccr",
        status: "skipped",
        message: before.isInstalled ? "CCR command exists, but the managed package is not installed" : "CCR proxy package not installed"
      };
    }
    await installCcr();
    const hasCommand = await commandExists("ccr");
    return {
      feature: "ccr",
      status: hasCommand ? "installed" : "failed",
      message: hasCommand ? "CCR proxy package installed" : "CCR proxy command not found after install"
    };
  } catch (error) {
    return {
      feature: "ccr",
      status: "failed",
      message: "CCR installation failed",
      error: error instanceof Error ? error.message : String(error)
    };
  }
}
async function ensureNativeGoals(codeTool) {
  if (codeTool !== "clavue") {
    return {
      feature: "native-goals",
      status: "skipped",
      message: "Native /goal is not managed for this runtime"
    };
  }
  const hasCommand = await commandExists("clavue");
  return {
    feature: "native-goals",
    status: hasCommand ? "already-present" : "failed",
    message: hasCommand ? "Clavue native /goal available" : "Clavue command not found"
  };
}
async function ensureClaudeFamilyCoreFeatures(options = {}) {
  const target = resolveClaudeFamilySettingsTarget(options.codeTool);
  const configLang = options.configLang || DEFAULT_CONFIG_LANG;
  const results = [];
  results.push(await ensureWorkflows(target.codeTool, configLang));
  results.push(await ensureMcp(target.codeTool));
  results.push(await ensurePermissions(target.codeTool));
  results.push(await ensureOutputStyles(target.codeTool));
  results.push(await ensureNativeGoals(target.codeTool));
  results.push(await ensureCcr(options.installCcr !== false));
  return results;
}

export { ensureClaudeFamilyCoreFeatures as e, inspectClaudeFamilyCoreFeatures as i };
