import { randomUUID } from "node:crypto";
export const DEFAULT_GEMINI_WEB_MODEL = "gemini-3.1-pro";
export const FALLBACK_GEMINI_WEB_MODEL = "gemini-3.1-flash-lite";
const MODEL_SPECS = {
    "gemini-3.1-flash-lite": {
        hash: "1d44b34bcaa1c04d",
        modelCode: 6,
        thinkingCode: 1,
    },
    "gemini-3.5-flash": {
        hash: "56fdd199312815e2",
        modelCode: 1,
        thinkingCode: 1,
    },
    "gemini-3.1-pro": {
        hash: "797f3d0293f288ad",
        modelCode: 3,
        thinkingCode: 1,
    },
    "gemini-3-pro-deep-think": {
        hash: "797f3d0293f288ad",
        modelCode: 3,
        thinkingCode: 3,
    },
};
let clientId;
function getGeminiWebClientId() {
    clientId ??= randomUUID().toUpperCase();
    return clientId;
}
export function buildGeminiWebModelHeader(model, webClientId = getGeminiWebClientId()) {
    const spec = MODEL_SPECS[model];
    return JSON.stringify([
        1,
        null,
        null,
        null,
        spec.hash,
        null,
        null,
        1,
        [4, 5, 6, 8],
        null,
        null,
        3,
        null,
        null,
        spec.modelCode,
        spec.thinkingCode,
        webClientId,
    ]);
}
export function resolveGeminiWebModel(desiredModel, log) {
    const desired = typeof desiredModel === "string" ? desiredModel.trim() : "";
    if (!desired)
        return DEFAULT_GEMINI_WEB_MODEL;
    const normalized = desired.toLowerCase().replace(/[_\s]+/g, "-");
    switch (normalized) {
        case "gemini-3.1-pro":
        case "gemini-3-pro":
        case "gemini-3.0-pro":
            return "gemini-3.1-pro";
        case "gemini-3.5-flash":
            return "gemini-3.5-flash";
        case "gemini-3.1-flash-lite":
        case "gemini-3.1-flashlite":
            return "gemini-3.1-flash-lite";
        case "gemini-3-deep-think":
        case "gemini-3-pro-deep-think":
        case "gemini-3-pro-deepthink":
        case "gemini-3.1-pro-deep-think":
            return "gemini-3-pro-deep-think";
        case "gemini-2.5-pro":
            return "gemini-3.1-pro";
        case "gemini-2.5-flash":
            return "gemini-3.1-flash-lite";
        default:
            if (normalized.startsWith("gemini-") || normalized.includes("gemini")) {
                log?.(`[gemini-web] Unsupported Gemini web model "${desired}". Falling back to ${DEFAULT_GEMINI_WEB_MODEL}.`);
            }
            return DEFAULT_GEMINI_WEB_MODEL;
    }
}
