"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatAccountDisplayName = exports.formatAccountVariantLabel = exports.buildEmailBackedNickname = exports.buildEmailBackedAccountId = exports.extractEmailAccountVariantKey = exports.extractCanonicalEmailFromAccountId = exports.supportsDuplicateEmailAccounts = void 0;
const DUPLICATE_EMAIL_ACCOUNT_PROVIDERS = new Set(['codex']);
const FREE_PLAN_PARTS = new Set(['free']);
const PERSONAL_PLAN_PARTS = new Set(['plus', 'pro']);
const BUSINESS_PLAN_PARTS = new Set(['team']);
// Keep variant parsing aligned with ui/src/lib/account-identity.ts. The UI copy is
// separate because the browser bundle cannot import this server module directly.
function normalizeProvider(provider) {
    return provider.trim().toLowerCase();
}
function cleanVariantTokenPart(value) {
    return value
        .trim()
        .replace(/^[^a-z0-9]+|[^a-z0-9]+$/gi, '')
        .replace(/[^a-z0-9._-]+/gi, '-')
        .replace(/-+/g, '-')
        .toLowerCase();
}
function baseNicknameFromEmail(email) {
    if (!email)
        return 'default';
    return email.split('@')[0].replace(/\s+/g, '').slice(0, 50) || 'default';
}
function formatVariantPart(value) {
    const normalized = value.trim().toLowerCase();
    if (!normalized) {
        return '';
    }
    switch (normalized) {
        case 'team':
            return 'Team';
        case 'free':
            return 'Free';
        case 'plus':
            return 'Plus';
        case 'pro':
            return 'Pro';
        default:
            return /^[a-f0-9]{8}$/i.test(normalized)
                ? normalized
                : normalized
                    .split(/[._-]+/)
                    .filter(Boolean)
                    .map((part) => part[0]?.toUpperCase() + part.slice(1))
                    .join(' ');
    }
}
function formatWorkspaceLabel(parts) {
    const workspaceId = parts.find((part) => /^[a-f0-9]{8}$/i.test(part));
    if (workspaceId) {
        return `Workspace ${workspaceId.toLowerCase()}`;
    }
    return parts.map(formatVariantPart).filter(Boolean).join(' · ') || null;
}
function formatAudienceDetail(parts) {
    const label = parts.map(formatVariantPart).filter(Boolean).join(' · ');
    return label || null;
}
function supportsDuplicateEmailAccounts(provider) {
    return DUPLICATE_EMAIL_ACCOUNT_PROVIDERS.has(normalizeProvider(provider));
}
exports.supportsDuplicateEmailAccounts = supportsDuplicateEmailAccounts;
function extractCanonicalEmailFromAccountId(accountId) {
    const canonical = accountId.split('#')[0]?.trim();
    return canonical && canonical.includes('@') ? canonical : null;
}
exports.extractCanonicalEmailFromAccountId = extractCanonicalEmailFromAccountId;
function extractEmailAccountVariantKey(provider, tokenFile, email) {
    if (!email || !supportsDuplicateEmailAccounts(provider)) {
        return null;
    }
    const normalizedProvider = normalizeProvider(provider);
    const baseName = tokenFile.replace(/\.json$/i, '');
    const providerPrefix = `${normalizedProvider}-`;
    const candidate = baseName.toLowerCase().startsWith(providerPrefix)
        ? baseName.slice(providerPrefix.length)
        : baseName;
    const emailIndex = candidate.toLowerCase().indexOf(email.toLowerCase());
    if (emailIndex === -1) {
        const fallback = cleanVariantTokenPart(candidate);
        return fallback && fallback !== cleanVariantTokenPart(email) ? fallback : null;
    }
    const before = cleanVariantTokenPart(candidate.slice(0, emailIndex));
    const after = cleanVariantTokenPart(candidate.slice(emailIndex + email.length));
    const parts = [before, after].filter(Boolean);
    return parts.length > 0 ? parts.join('-') : null;
}
exports.extractEmailAccountVariantKey = extractEmailAccountVariantKey;
function buildEmailBackedAccountId(provider, tokenFile, email, duplicateEmailCount = 1) {
    if (!email) {
        return 'default';
    }
    if (!supportsDuplicateEmailAccounts(provider) || duplicateEmailCount <= 1) {
        return email;
    }
    const variantKey = extractEmailAccountVariantKey(provider, tokenFile, email);
    return variantKey ? `${email}#${variantKey}` : email;
}
exports.buildEmailBackedAccountId = buildEmailBackedAccountId;
function buildEmailBackedNickname(provider, tokenFile, email, duplicateEmailCount = 1) {
    const base = baseNicknameFromEmail(email);
    if (!supportsDuplicateEmailAccounts(provider) || duplicateEmailCount <= 1) {
        return base;
    }
    const variantKey = extractEmailAccountVariantKey(provider, tokenFile, email);
    if (!variantKey) {
        return base;
    }
    return `${base}-${variantKey}`.slice(0, 50);
}
exports.buildEmailBackedNickname = buildEmailBackedNickname;
function formatAccountVariantLabel(accountId, email) {
    const variantKey = extractCanonicalEmailFromAccountId(accountId) === email ? accountId.split('#')[1] : null;
    if (!variantKey) {
        return null;
    }
    const parts = variantKey.split('-').filter(Boolean);
    if (parts.length === 0) {
        return null;
    }
    const suffix = parts[parts.length - 1]?.toLowerCase();
    if (suffix && BUSINESS_PLAN_PARTS.has(suffix)) {
        return ['Business', formatWorkspaceLabel(parts.slice(0, -1))].filter(Boolean).join(' · ');
    }
    if (suffix && FREE_PLAN_PARTS.has(suffix)) {
        return ['Free', formatAudienceDetail(parts.slice(0, -1))].filter(Boolean).join(' · ');
    }
    if (suffix && PERSONAL_PLAN_PARTS.has(suffix)) {
        return ['Personal', formatVariantPart(suffix), formatAudienceDetail(parts.slice(0, -1))]
            .filter(Boolean)
            .join(' · ');
    }
    return parts.map(formatVariantPart).filter(Boolean).join(' · ');
}
exports.formatAccountVariantLabel = formatAccountVariantLabel;
function formatAccountDisplayName(account) {
    const base = account.email || account.id;
    const variantLabel = formatAccountVariantLabel(account.id, account.email);
    return variantLabel ? `${base} (${variantLabel})` : base;
}
exports.formatAccountDisplayName = formatAccountDisplayName;
//# sourceMappingURL=email-account-identity.js.map