"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAuthProfileId = createAuthProfileId;
exports.formatAuthProvider = formatAuthProvider;
exports.formatAuthProfileDetail = formatAuthProfileDetail;
exports.formatAuthProfileBaseLabel = formatAuthProfileBaseLabel;
exports.formatAuthProfileAddedDate = formatAuthProfileAddedDate;
exports.formatAuthProfileLabel = formatAuthProfileLabel;
exports.formatAuthAccountDisplay = formatAuthAccountDisplay;
const node_crypto_1 = require("node:crypto");
const definitions_1 = require("./definitions");
const model_1 = require("./model");
const catalog_1 = require("../i18n/catalog");
const kind_1 = require("./kind");
const ULID_ALPHABET = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
const AUTH_LABEL_DATE_FORMATTER = new Intl.DateTimeFormat("en-US", {
    month: "short",
    day: "numeric",
    timeZone: "UTC",
});
function createAuthProfileId(now = new Date()) {
    const timePart = encodeBase32(BigInt(now.getTime()), 10);
    const randomPart = encodeBase32([...(0, node_crypto_1.randomBytes)(10)].reduce((value, byte) => (value << 8n) | BigInt(byte), 0n), 16);
    return model_1.authProfileIdSchema.parse(`auth_${timePart}${randomPart}`);
}
function formatAuthProvider(provider) {
    return definitions_1.AUTH_PROFILE_PROVIDER_LABELS[provider];
}
function formatAuthProfileDetail(profile, locale = catalog_1.DEFAULT_APP_LOCALE) {
    switch (profile.kind) {
        case "codex-home":
            if (profile.codexLoginMethod === "device-auth") {
                return (0, catalog_1.translate)(locale, "auth.profile.detail.codexDevice");
            }
            if (profile.codexLoginMethod === "api-key") {
                return (0, catalog_1.translate)(locale, "auth.profile.detail.codexApiKey");
            }
            return (0, catalog_1.translate)(locale, "auth.profile.detail.codexBrowser");
        case "codex-env-var":
            return (0, catalog_1.translate)(locale, "auth.profile.detail.codexEnvVar", {
                envVarName: profile.envVarName?.trim() || definitions_1.CODEX_API_KEY_ENV_VAR_NAME,
            });
        case "claude-cli":
            return (0, catalog_1.translate)(locale, "auth.profile.detail.claudeCli");
        case "claude-api-key":
            return (0, catalog_1.translate)(locale, "auth.profile.detail.claudeApiKey");
        case "claude-env-var":
            return (0, catalog_1.translate)(locale, "auth.profile.detail.claudeEnvVar", {
                envVarName: profile.envVarName?.trim() || definitions_1.CLAUDE_API_KEY_ENV_VAR_NAME,
            });
    }
}
function formatAuthProfileBaseLabel(profile, locale = catalog_1.DEFAULT_APP_LOCALE) {
    return `${formatAuthProvider((0, kind_1.getAuthProfileProvider)(profile))} (${formatAuthProfileDetail(profile, locale)})`;
}
function formatAuthProfileAddedDate(createdAt, locale = catalog_1.DEFAULT_APP_LOCALE) {
    if (!createdAt) {
        return (0, catalog_1.translate)(locale, "common.placeholder.unknown");
    }
    const date = new Date(createdAt);
    if (Number.isNaN(date.getTime())) {
        return (0, catalog_1.translate)(locale, "common.placeholder.unknown");
    }
    return AUTH_LABEL_DATE_FORMATTER.format(date);
}
function formatAuthProfileLabel(profile, profiles = [], locale = catalog_1.DEFAULT_APP_LOCALE) {
    const base = formatAuthProfileBaseLabel(profile, locale);
    if (!needsDisambiguation(profile, profiles, locale)) {
        return base;
    }
    return (0, catalog_1.translate)(locale, "auth.profile.label.added", {
        base: base.slice(0, -1),
        addedAt: formatAuthProfileAddedDate(profile.createdAt, locale),
    });
}
function formatAuthAccountDisplay(profile, profiles = [], locale = catalog_1.DEFAULT_APP_LOCALE) {
    return formatAuthProfileLabel(profile, profiles, locale);
}
function needsDisambiguation(profile, profiles, locale) {
    const label = formatAuthProfileBaseLabel(profile, locale);
    let matches = 0;
    for (const candidate of profiles) {
        if (formatAuthProfileBaseLabel(candidate, locale) !== label) {
            continue;
        }
        matches += 1;
        if (matches > 1) {
            return true;
        }
    }
    return false;
}
function encodeBase32(value, length) {
    let remainder = value;
    let result = "";
    for (let index = 0; index < length; index += 1) {
        const digit = Number(remainder & 31n);
        result = `${ULID_ALPHABET[digit]}${result}`;
        remainder >>= 5n;
    }
    return result;
}
