import * as fs from "node:fs/promises";
import * as os from "node:os";
import * as path from "node:path";
const CLAUDE_THINKING_OPTIONS = [
    { id: "low", label: "Low" },
    { id: "medium", label: "Medium" },
    { id: "high", label: "High" },
    { id: "max", label: "Max" },
];
const CLAUDE_OPUS_EXTENDED_THINKING_OPTIONS = [
    { id: "low", label: "Low" },
    { id: "medium", label: "Medium" },
    { id: "high", label: "High" },
    { id: "xhigh", label: "Extra High" },
    { id: "max", label: "Max" },
];
const CLAUDE_MODELS = [
    {
        provider: "claude",
        id: "claude-opus-4-8[1m]",
        label: "Opus 4.8 1M",
        description: "Opus 4.8 with 1M context window",
        thinkingOptions: [...CLAUDE_OPUS_EXTENDED_THINKING_OPTIONS],
    },
    {
        provider: "claude",
        id: "claude-opus-4-8",
        label: "Opus 4.8",
        description: "Opus 4.8 · Latest release",
        isDefault: true,
        thinkingOptions: [...CLAUDE_OPUS_EXTENDED_THINKING_OPTIONS],
    },
    {
        provider: "claude",
        id: "claude-opus-4-7[1m]",
        label: "Opus 4.7 1M",
        description: "Opus 4.7 with 1M context window",
        thinkingOptions: [...CLAUDE_OPUS_EXTENDED_THINKING_OPTIONS],
    },
    {
        provider: "claude",
        id: "claude-opus-4-7",
        label: "Opus 4.7",
        description: "Opus 4.7 · Previous release",
        thinkingOptions: [...CLAUDE_OPUS_EXTENDED_THINKING_OPTIONS],
    },
    {
        provider: "claude",
        id: "claude-opus-4-6[1m]",
        label: "Opus 4.6 1M",
        description: "Opus 4.6 with 1M context window",
        thinkingOptions: [...CLAUDE_THINKING_OPTIONS],
    },
    {
        provider: "claude",
        id: "claude-opus-4-6",
        label: "Opus 4.6",
        description: "Opus 4.6 · Most capable for complex work",
        thinkingOptions: [...CLAUDE_THINKING_OPTIONS],
    },
    {
        provider: "claude",
        id: "claude-sonnet-4-6[1m]",
        label: "Sonnet 4.6 1M",
        description: "Sonnet 4.6 with 1M context window",
        thinkingOptions: [...CLAUDE_THINKING_OPTIONS],
    },
    {
        provider: "claude",
        id: "claude-sonnet-4-6",
        label: "Sonnet 4.6",
        description: "Sonnet 4.6 · Best for everyday tasks",
        thinkingOptions: [...CLAUDE_THINKING_OPTIONS],
    },
    {
        provider: "claude",
        id: "claude-haiku-4-5",
        label: "Haiku 4.5",
        description: "Haiku 4.5 · Fastest for quick answers",
    },
];
const CLAUDE_SETTINGS_MODEL_ENV_KEYS = [
    "ANTHROPIC_MODEL",
    "ANTHROPIC_SMALL_FAST_MODEL",
    "ANTHROPIC_DEFAULT_OPUS_MODEL",
    "ANTHROPIC_DEFAULT_SONNET_MODEL",
    "ANTHROPIC_DEFAULT_HAIKU_MODEL",
];
export function getClaudeModels() {
    return CLAUDE_MODELS.map((model) => ({ ...model }));
}
export async function getClaudeModelsWithSettings(logger) {
    const hardcodedModels = getClaudeModels();
    const settingsModels = await readClaudeSettingsModels(logger);
    if (settingsModels.length === 0) {
        return hardcodedModels;
    }
    const seenModelIds = new Set(hardcodedModels.map((model) => model.id));
    const models = [...hardcodedModels];
    for (const model of settingsModels) {
        if (seenModelIds.has(model.id)) {
            continue;
        }
        seenModelIds.add(model.id);
        models.push(model);
    }
    return models;
}
async function readClaudeSettingsModels(logger) {
    const settingsPath = path.join(resolveClaudeConfigDir(), "settings.json");
    let parsed;
    try {
        const rawSettings = await fs.readFile(settingsPath, "utf8");
        parsed = JSON.parse(rawSettings);
    }
    catch (error) {
        logger.debug({ err: error, settingsPath }, "Failed to read Claude settings models");
        return [];
    }
    if (!isRecord(parsed)) {
        logger.debug({ settingsPath }, "Claude settings.json is not an object");
        return [];
    }
    const models = [];
    addSettingsModel(models, parsed.model, "model");
    const env = parsed.env;
    if (env === undefined) {
        return models;
    }
    if (!isRecord(env)) {
        logger.debug({ settingsPath }, "Claude settings.json env is not an object");
        return models;
    }
    for (const envKey of CLAUDE_SETTINGS_MODEL_ENV_KEYS) {
        addSettingsModel(models, env[envKey], `env.${envKey}`);
    }
    return models;
}
function resolveClaudeConfigDir() {
    return process.env.CLAUDE_CONFIG_DIR ?? path.join(os.homedir(), ".claude");
}
function addSettingsModel(models, value, settingsKey) {
    if (typeof value !== "string") {
        return;
    }
    const id = value.trim();
    if (id.length === 0 || models.some((model) => model.id === id)) {
        return;
    }
    models.push({
        provider: "claude",
        id,
        label: id,
        description: `From Claude settings.json ${settingsKey}`,
    });
}
function isRecord(value) {
    return typeof value === "object" && value !== null && !Array.isArray(value);
}
/**
 * Normalize a runtime model string (from SDK init message) to a known model ID.
 * Handles the `[1m]` suffix that the SDK appends for 1M context sessions.
 */
export function normalizeClaudeRuntimeModelId(value) {
    const trimmed = typeof value === "string" ? value.trim() : "";
    if (!trimmed) {
        return null;
    }
    // Check for exact match first (handles claude-opus-4-6[1m] directly)
    if (CLAUDE_MODELS.some((model) => model.id === trimmed)) {
        return trimmed;
    }
    // Match: claude-{family}-{major}-{minor}[1m]? possibly followed by a date suffix
    const runtimeMatch = trimmed.match(/(?:claude-)?(opus|sonnet|haiku)[-_ ]+(\d+)[-.](\d+)(\[1m\])?/i);
    if (!runtimeMatch) {
        return null;
    }
    const family = runtimeMatch[1].toLowerCase();
    const major = runtimeMatch[2];
    const minor = runtimeMatch[3];
    const suffix = runtimeMatch[4] ?? "";
    return `claude-${family}-${major}-${minor}${suffix}`;
}
//# sourceMappingURL=models.js.map