import 'posthog-node';
import path from 'node:path';
import { execFile } from 'node:child_process';
import { promisify } from 'node:util';

// src/platform/command-runtime/help.ts
function formatSlashCommands(commands) {
  return commands.map((command) => `  ${command.command.padEnd(24)} ${command.description}`).join("\n");
}
function createCliHelpText(commands, currentService, binaryName = "ju-hong-e") {
  const serviceSection = currentService ? `Current Service:
  ${currentService}

` : "";
  return `
Usage: ${binaryName} [options]

Options:
  --service <codex|gemini|claude>   \uC2DC\uC791 \uC11C\uBE44\uC2A4 \uC9C0\uC815
  --model <name>                    \uC11C\uBE44\uC2A4\uBCC4 \uBAA8\uB378 \uC9C0\uC815
  --reasoning-effort <level>        minimal|low|medium|high
  --test                            \uC2E4\uC81C AI \uD638\uCD9C \uC5C6\uC774 \uC804\uCCB4 \uD750\uB984 \uD14C\uC2A4\uD2B8
  --trace                           trace \uB85C\uADF8\uB97C \uCF58\uC194\uC5D0 \uCD9C\uB825
  --help                            \uB3C4\uC6C0\uB9D0 \uCD9C\uB825

Slash Commands:
${formatSlashCommands(commands)}

Shell Commands:
  /sh <command>              \uB85C\uCEEC shell\uC5D0 \uC6D0\uBB38 \uADF8\uB300\uB85C \uC804\uB2EC\uD574 \uC21C\uC218 \uD130\uBBF8\uB110 \uBA85\uB839 \uC2E4\uD589
  /sh                        \uC778\uD130\uB799\uD2F0\uBE0C shell \uC138\uC158\uC744 \uC5F4\uACE0 \uC885\uB8CC \uC2DC \uC8FC\uD64D\uC774\uB85C \uBCF5\uADC0

Shortcuts:
  ESC                        \uD604\uC7AC \uC791\uC5C5 \uB610\uB294 \uC785\uB825\uB9CC \uCDE8\uC18C
  Ctrl+C                     \uC138\uC158 \uC885\uB8CC
  Shift+Up/Down              \uC785\uB825 \uD788\uC2A4\uD1A0\uB9AC \uC774\uB3D9

${serviceSection}`.trim();
}

// src/platform/runtime-profile.ts
var runtimeState = globalThis.__JU_HONG_E_RUNTIME_PROFILE__ ?? (globalThis.__JU_HONG_E_RUNTIME_PROFILE__ = {
  binaryName: "ju-hong-e",
  profile: "all"
});
function getCliBinaryName() {
  return runtimeState.binaryName;
}

// src/platform/analytics/posthog.ts
globalThis.__JU_HONG_E_POSTHOG_RUNTIME_STATE__ ?? (globalThis.__JU_HONG_E_POSTHOG_RUNTIME_STATE__ = {});
function isPromptMarkdownFileReference(asset) {
  return typeof asset !== "string";
}
function isPromptRuleGroupAsset(asset) {
  return typeof asset !== "string" && !("kind" in asset && asset.kind === "file-reference");
}
function clonePromptMarkdownAsset(asset) {
  return isPromptMarkdownFileReference(asset) ? {
    ...asset,
    filePath: path.normalize(asset.filePath)
  } : asset;
}
promisify(execFile);

// src/platform/request-security/registry.ts
var securityRegistryState = globalThis.__JUHONG_E_SECURITY_REGISTRY__ ?? (globalThis.__JUHONG_E_SECURITY_REGISTRY__ = {
  patternRules: /* @__PURE__ */ new Map(),
  validatorRules: /* @__PURE__ */ new Map()
});
var GLOBAL_PATTERN_RULES = securityRegistryState.patternRules;
securityRegistryState.validatorRules;
function addGlobalSecurityPattern(rule) {
  GLOBAL_PATTERN_RULES.set(rule.name, rule);
}

// src/platform/request-security/default-rules.ts
var initialized = false;
function formatResidentRegistrationNumber(matchedText) {
  const digits = matchedText.replace(/\D/g, "");
  if (digits.length !== 13) {
    return `${matchedText.slice(0, 3)}***`;
  }
  return `${digits.slice(0, 6)}-${digits[6]}******`;
}
function ensureDefaultSecurityRules() {
  if (initialized) {
    return;
  }
  initialized = true;
  addGlobalSecurityPattern({
    formatMatch: formatResidentRegistrationNumber,
    message: "\uBCF4\uC548 \uC815\uCC45\uC5D0 \uC758\uD574 \uC8FC\uBBFC\uB4F1\uB85D\uBC88\uD638 \uD615\uD0DC\uC758 \uAC12\uC774 \uD3EC\uD568\uB41C \uC785\uB825\uC740 \uC2E4\uD589\uD560 \uC218 \uC5C6\uC2B5\uB2C8\uB2E4.",
    name: "resident-registration-number",
    pattern: /\b\d{6}[- ]?[1-4]\d{6}\b/
  });
}

// src/platform/command-runtime/registry.ts
var registryState = globalThis.__JU_HONG_E_COMMAND_REGISTRY__ ?? (globalThis.__JU_HONG_E_COMMAND_REGISTRY__ = {
  juHongECommands: /* @__PURE__ */ new Map()
});
var JU_HONG_E_COMMANDS = registryState.juHongECommands;
function resolveRegistration(registration, api) {
  if (typeof registration === "function") {
    return registration(api);
  }
  return registration;
}
function createCommand(definition) {
  return {
    ...definition,
    forms: definition.forms ? definition.forms.map(clonePromptMarkdownAsset) : void 0,
    instructions: definition.instructions ? definition.instructions.map(clonePromptMarkdownAsset) : void 0,
    kind: "ju-hong-e",
    rules: definition.rules ? definition.rules.map(
      (ruleAsset) => !isPromptRuleGroupAsset(ruleAsset) ? clonePromptMarkdownAsset(ruleAsset) : {
        ...ruleAsset,
        fileNamePatterns: ruleAsset.fileNamePatterns ? [...ruleAsset.fileNamePatterns] : void 0,
        rules: ruleAsset.rules ? ruleAsset.rules.map(clonePromptMarkdownAsset) : void 0
      }
    ) : void 0
  };
}
var REGISTRATION_API = {
  createCommand
};
function addCommand(registration) {
  ensureDefaultSecurityRules();
  const definition = resolveRegistration(registration, REGISTRATION_API);
  if (JU_HONG_E_COMMANDS.has(definition.command)) {
    throw new Error(`\uC774\uBBF8 \uB4F1\uB85D\uB41C \uC8FC\uD64D\uC774 \uBA85\uB839\uC5B4\uC785\uB2C8\uB2E4. command=${definition.command}`);
  }
  JU_HONG_E_COMMANDS.set(definition.command, definition);
}

// src/command-modules/help/index.ts
addCommand(
  ({ createCommand: createCommand2 }) => createCommand2({
    command: "/help",
    description: "\uB3C4\uC6C0\uB9D0 \uCD9C\uB825",
    /**
     * @description
     * `execute` 는 대상 값를 실행한다.
     * 사용자가 해당 builtin 명령을 선택했을 때 가장 먼저 호출되는 실행 엔트리다.
     *
     * @param context 현재 명령 또는 action 실행 컨텍스트
     * @returns execute 실행 결과
     */
    execute: (context) => {
      console.log(createCliHelpText(context.catalog.builtinCommands, context.service, getCliBinaryName()));
      return {};
    },
    order: 60
  })
);
