import { DEFAULT_CODEX_CONVERSION_CONFIG, isObject, normalizeCodexVerbosity, normalizeCompactionModel, normalizeCompactionReasoning, normalizeProviderList, } from "./config.js";
export function migrateCodexConversionConfigIfNeeded(value) {
    if (!isObject(value))
        return { migrated: false, config: value };
    if (isObject(value["scope"]) || isObject(value["tools"]) || isObject(value["ui"]) || isObject(value["compaction"]) || isObject(value["beta"]) || isObject(value["openai"])) {
        const beta = isObject(value["beta"]) ? value["beta"] : undefined;
        if (beta && typeof beta["responsesLite"] === "boolean" && typeof beta["codeMode"] !== "boolean") {
            const { responsesLite, ...rest } = beta;
            return { migrated: true, config: { ...value, beta: { ...rest, codeMode: responsesLite, responsesLite: false } } };
        }
        return { migrated: false, config: value };
    }
    const adapterProviderCodexToolsDisabled = value["adapterProviderCodexTools"] === false;
    const config = {
        ...structuredClone(DEFAULT_CODEX_CONVERSION_CONFIG),
        scope: {
            allProviders: value["useOnAllModels"] === true ? "on" : value["useOnAllModels"] === false ? "off" : DEFAULT_CODEX_CONVERSION_CONFIG.scope["allProviders"],
            additionalProviders: value["useAdapterProviders"] === true ? normalizeProviderList(value["adapterProviders"]) : [],
        },
        tools: {
            webRun: adapterProviderCodexToolsDisabled ? false : typeof value["webSearch"] === "boolean" ? value["webSearch"] : DEFAULT_CODEX_CONVERSION_CONFIG.tools["webRun"],
            imageGeneration: adapterProviderCodexToolsDisabled ? false : typeof value["imageGeneration"] === "boolean" ? value["imageGeneration"] : DEFAULT_CODEX_CONVERSION_CONFIG.tools["imageGeneration"],
            viewImageFallback: DEFAULT_CODEX_CONVERSION_CONFIG.tools["viewImageFallback"],
            applyPatchOnly: typeof value["applyPatchOnly"] === "boolean" ? value["applyPatchOnly"] : DEFAULT_CODEX_CONVERSION_CONFIG.tools["applyPatchOnly"],
            viewImageOnly: DEFAULT_CODEX_CONVERSION_CONFIG.tools["viewImageOnly"],
            webRunOnly: DEFAULT_CODEX_CONVERSION_CONFIG.tools["webRunOnly"],
            imageGenerationOnly: DEFAULT_CODEX_CONVERSION_CONFIG.tools["imageGenerationOnly"],
        },
        ui: {
            statusLine: typeof value["statusLine"] === "boolean" ? value["statusLine"] : DEFAULT_CODEX_CONVERSION_CONFIG.ui["statusLine"],
            toolRenaming: DEFAULT_CODEX_CONVERSION_CONFIG.ui["toolRenaming"],
            compactTools: DEFAULT_CODEX_CONVERSION_CONFIG.ui["compactTools"],
            codeModeDetails: DEFAULT_CODEX_CONVERSION_CONFIG.ui["codeModeDetails"],
            backgroundShellWidget: typeof value["backgroundShellWidget"] === "boolean" ? value["backgroundShellWidget"] : DEFAULT_CODEX_CONVERSION_CONFIG.ui["backgroundShellWidget"],
            backgroundShellToggleShortcut: stringValue(value["backgroundShellToggleShortcut"], DEFAULT_CODEX_CONVERSION_CONFIG.ui["backgroundShellToggleShortcut"]),
            backgroundShellPrevShortcut: stringValue(value["backgroundShellPrevShortcut"], DEFAULT_CODEX_CONVERSION_CONFIG.ui["backgroundShellPrevShortcut"]),
            backgroundShellNextShortcut: stringValue(value["backgroundShellNextShortcut"], DEFAULT_CODEX_CONVERSION_CONFIG.ui["backgroundShellNextShortcut"]),
            backgroundShellCloseShortcut: stringValue(value["backgroundShellCloseShortcut"], DEFAULT_CODEX_CONVERSION_CONFIG.ui["backgroundShellCloseShortcut"]),
        },
        compaction: {
            responsesCompaction: typeof value["responsesCompaction"] === "boolean" ? value["responsesCompaction"] : DEFAULT_CODEX_CONVERSION_CONFIG.compaction["responsesCompaction"],
        },
        beta: { ...DEFAULT_CODEX_CONVERSION_CONFIG.beta },
        openai: {
            fast: typeof value["fast"] === "boolean" ? value["fast"] : DEFAULT_CODEX_CONVERSION_CONFIG.openai["fast"],
            verbosity: normalizeCodexVerbosity(value["verbosity"]) ?? DEFAULT_CODEX_CONVERSION_CONFIG.openai["verbosity"],
            forceCachedWebSockets: typeof value["forceCachedWebSockets"] === "boolean" ? value["forceCachedWebSockets"] : DEFAULT_CODEX_CONVERSION_CONFIG.openai["forceCachedWebSockets"],
            webSearchModel: DEFAULT_CODEX_CONVERSION_CONFIG.openai["webSearchModel"],
            compactionModel: normalizeCompactionModel(value["compactionModel"]) ?? DEFAULT_CODEX_CONVERSION_CONFIG.openai["compactionModel"],
            compactionReasoning: normalizeCompactionReasoning(value["compactionReasoning"]) ?? DEFAULT_CODEX_CONVERSION_CONFIG.openai["compactionReasoning"],
        },
    };
    return { migrated: true, config };
}
function stringValue(value, fallback) {
    return typeof value === "string" && value.trim() ? value.trim() : fallback;
}
