'use strict';

require('fs');
require('path');
require('child_process');
require('util');

// src/platform/core/helpers.ts
function shellQuote(value) {
  return `'${value.replace(/'/g, `'\\''`)}'`;
}

// src/platform/ai-services/internal/shared.ts
var ACCOUNT_DEFAULT_MODEL = "account-default";
var SERVICE_FALLBACK_MODELS = {
  claude: [ACCOUNT_DEFAULT_MODEL, "haiku", "sonnet"],
  codex: [ACCOUNT_DEFAULT_MODEL, "gpt-5"],
  gemini: [ACCOUNT_DEFAULT_MODEL, "flash", "auto"]
};
var CAPACITY_FAILURE_PATTERNS = [
  /exhausted your capacity/i,
  /\bquota\b/i,
  /rate[- ]limit/i,
  /resource exhausted/i,
  /too many requests/i
];
function quoteArgs(args) {
  return args.map((arg) => shellQuote(arg)).join(" ");
}
function formatAttemptPreviewHeading(attempt, index) {
  return index === 0 ? `# \uCD5C\uCD08 \uC2E4\uD589 (${attempt.label})` : `# \uD3F4\uBC31 ${index} (${attempt.label})`;
}
function formatServiceCommandPreview(command, attempts) {
  return attempts.map((attempt, index) => [formatAttemptPreviewHeading(attempt, index), `${command} ${quoteArgs(attempt.args)}`].join("\n")).join("\n\n");
}
function toUnique(values) {
  const seen = /* @__PURE__ */ new Set();
  return values.filter((value) => {
    if (!value || seen.has(value)) {
      return false;
    }
    seen.add(value);
    return true;
  });
}
function buildServiceModelCandidates(service, preferredModel) {
  const fallbackModels = SERVICE_FALLBACK_MODELS[service];
  return toUnique(preferredModel ? [preferredModel, ...fallbackModels] : fallbackModels);
}
function isAccountDefaultModel(model) {
  return model === ACCOUNT_DEFAULT_MODEL;
}
function classifyServiceFailureKind(stderr, stdout) {
  const combinedOutput = `${stderr}
${stdout}`;
  return CAPACITY_FAILURE_PATTERNS.some((pattern) => pattern.test(combinedOutput)) ? "capacity" : "generic";
}
function formatServiceAttemptMessage(baseMessage, attempt, index, total) {
  return `${baseMessage} (${attempt.label}, ${index + 1}/${total})`;
}
function formatServiceFallbackMessage(service, failedAttempt, nextAttempt, failureKind) {
  if (failureKind === "capacity") {
    return `${service} ${failedAttempt.label} \uC0AC\uC6A9\uB7C9 \uD55C\uB3C4\uC5D0 \uB3C4\uB2EC\uD588\uC2B5\uB2C8\uB2E4. ${nextAttempt.label} \uB85C \uC804\uD658\uD569\uB2C8\uB2E4.`;
  }
  return `${failedAttempt.label} \uC2DC\uB3C4\uAC00 \uC2E4\uD328\uD574 ${nextAttempt.label} \uB85C \uC804\uD658\uD569\uB2C8\uB2E4.`;
}
function createServiceCommandSpec(command, attempts, promptAudit) {
  const normalizedAttempts = attempts.map(({ args, label, modelName, outputProtocol, stdoutCaptureMode }) => ({
    args,
    label,
    modelName,
    outputProtocol: outputProtocol || "plain-text",
    stdoutCaptureMode,
    preview: `${command} ${quoteArgs(args)}`
  }));
  return {
    attempts: normalizedAttempts,
    command,
    promptAudit,
    preview: formatServiceCommandPreview(command, normalizedAttempts)
  };
}

exports.ACCOUNT_DEFAULT_MODEL = ACCOUNT_DEFAULT_MODEL;
exports.buildServiceModelCandidates = buildServiceModelCandidates;
exports.classifyServiceFailureKind = classifyServiceFailureKind;
exports.createServiceCommandSpec = createServiceCommandSpec;
exports.formatServiceAttemptMessage = formatServiceAttemptMessage;
exports.formatServiceFallbackMessage = formatServiceFallbackMessage;
exports.isAccountDefaultModel = isAccountDefaultModel;
exports.quoteArgs = quoteArgs;
exports.toUnique = toUnique;
