"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseDeployRole = parseDeployRole;
exports.parseDeploySlot = parseDeploySlot;
exports.parsePublicSurface = parsePublicSurface;
exports.resolveDeployRole = resolveDeployRole;
exports.resolveDeploySlot = resolveDeploySlot;
exports.resolvePublicSurface = resolvePublicSurface;
exports.resolveEnvironmentName = resolveEnvironmentName;
exports.resolveSeoIndexingMode = resolveSeoIndexingMode;
exports.resolveWebOrigin = resolveWebOrigin;
exports.resolveApiOrigin = resolveApiOrigin;
exports.resolveRealtimeOrigin = resolveRealtimeOrigin;
exports.resolveAiOrigin = resolveAiOrigin;
exports.resolveStructuredLogFields = resolveStructuredLogFields;
const DEPLOY_ROLE_VALUES = new Set(['prod', 'preview']);
const DEPLOY_SLOT_VALUES = new Set(['blue', 'green']);
const PUBLIC_SURFACE_VALUES = new Set(['prod', 'preview', 'blue', 'green']);
function normalizeValue(value) {
    if (typeof value !== 'string') {
        return null;
    }
    const normalized = value.trim().toLowerCase();
    return normalized.length > 0 ? normalized : null;
}
function normalizeOrigin(value) {
    return value.trim().replace(/\/+$/, '');
}
function buildDefaultOrigin(params) {
    const { servicePrefix, surface, websocket = false } = params;
    const protocol = websocket ? 'wss' : 'https';
    const host = surface === 'prod'
        ? servicePrefix ? `${servicePrefix}.playdrop.ai` : 'www.playdrop.ai'
        : servicePrefix
            ? `${servicePrefix}.${surface}.playdrop.ai`
            : `${surface}.playdrop.ai`;
    return `${protocol}://${host}`;
}
function parseReleaseBuild(value) {
    const parsed = Number(value);
    return Number.isInteger(parsed) && parsed > 0 ? parsed : 0;
}
function parseDeployRole(value) {
    const normalized = normalizeValue(value);
    if (!normalized || !DEPLOY_ROLE_VALUES.has(normalized)) {
        return null;
    }
    return normalized;
}
function parseDeploySlot(value) {
    const normalized = normalizeValue(value);
    if (!normalized || !DEPLOY_SLOT_VALUES.has(normalized)) {
        return null;
    }
    return normalized;
}
function parsePublicSurface(value) {
    const normalized = normalizeValue(value);
    if (!normalized || !PUBLIC_SURFACE_VALUES.has(normalized)) {
        return null;
    }
    return normalized;
}
function resolveDeployRole(env = process.env) {
    return parseDeployRole(env['PLAYDROP_DEPLOY_ROLE'])
        ?? (env['NODE_ENV'] === 'production' ? 'prod' : 'preview');
}
function resolveDeploySlot(env = process.env) {
    return parseDeploySlot(env['PLAYDROP_SLOT']);
}
function resolvePublicSurface(env = process.env) {
    const explicit = parsePublicSurface(env['PLAYDROP_PUBLIC_SURFACE']);
    if (explicit) {
        return explicit;
    }
    return resolveDeployRole(env) === 'preview' ? 'preview' : 'prod';
}
function resolveEnvironmentName(env = process.env) {
    const explicit = typeof env['PLAYDROP_ENVIRONMENT'] === 'string' ? env['PLAYDROP_ENVIRONMENT'].trim() : '';
    if (explicit.length > 0) {
        return explicit;
    }
    const nodeEnv = typeof env['NODE_ENV'] === 'string' ? env['NODE_ENV'].trim() : '';
    return nodeEnv.length > 0 ? nodeEnv : 'development';
}
function resolveSeoIndexingMode(env = process.env) {
    const explicit = normalizeValue(env['PLAYDROP_SEO_INDEXING']);
    if (explicit === 'public') {
        return 'public';
    }
    if (explicit === 'private') {
        return 'private';
    }
    if (explicit === 'noindex') {
        return 'noindex';
    }
    return resolveDeployRole(env) === 'prod' ? 'public' : 'noindex';
}
function resolveWebOrigin(env = process.env) {
    const explicit = typeof env['PLAYDROP_PUBLIC_ORIGIN'] === 'string' ? env['PLAYDROP_PUBLIC_ORIGIN'].trim() : '';
    if (explicit.length > 0) {
        return normalizeOrigin(explicit);
    }
    const legacy = typeof env['PLAYDROP_WEB_BASE_URL'] === 'string' ? env['PLAYDROP_WEB_BASE_URL'].trim() : '';
    if (legacy.length > 0) {
        return normalizeOrigin(legacy);
    }
    return buildDefaultOrigin({ surface: resolvePublicSurface(env) });
}
function resolveApiOrigin(env = process.env) {
    const explicit = typeof env['PLAYDROP_API_BASE_URL'] === 'string' ? env['PLAYDROP_API_BASE_URL'].trim() : '';
    if (explicit.length > 0) {
        return normalizeOrigin(explicit);
    }
    return buildDefaultOrigin({ servicePrefix: 'api', surface: resolvePublicSurface(env) });
}
function resolveRealtimeOrigin(env = process.env) {
    const explicit = typeof env['PLAYDROP_REALTIME_BASE_URL'] === 'string' ? env['PLAYDROP_REALTIME_BASE_URL'].trim() : '';
    if (explicit.length > 0) {
        return normalizeOrigin(explicit);
    }
    return buildDefaultOrigin({ servicePrefix: 'play', surface: resolvePublicSurface(env), websocket: true });
}
function resolveAiOrigin(env = process.env) {
    const explicit = typeof env['PLAYDROP_AI_BASE_URL'] === 'string' ? env['PLAYDROP_AI_BASE_URL'].trim() : '';
    if (explicit.length > 0) {
        return normalizeOrigin(explicit);
    }
    return buildDefaultOrigin({ servicePrefix: 'ai', surface: resolvePublicSurface(env) });
}
function resolveStructuredLogFields(service, env = process.env) {
    const releaseVersion = typeof env['PLAYDROP_RELEASE_VERSION'] === 'string' && env['PLAYDROP_RELEASE_VERSION'].trim().length > 0
        ? env['PLAYDROP_RELEASE_VERSION'].trim()
        : typeof env['NEXT_PUBLIC_PLAYDROP_RELEASE_VERSION'] === 'string' && env['NEXT_PUBLIC_PLAYDROP_RELEASE_VERSION'].trim().length > 0
            ? env['NEXT_PUBLIC_PLAYDROP_RELEASE_VERSION'].trim()
            : '0.0.0';
    const buildNumber = parseReleaseBuild(env['PLAYDROP_BUILD_NUMBER'] ?? env['NEXT_PUBLIC_PLAYDROP_BUILD_NUMBER']);
    const slot = resolveDeploySlot(env);
    return {
        environment: resolveEnvironmentName(env),
        slot: slot ?? 'none',
        service,
        release_version: releaseVersion,
        build_number: buildNumber,
        deploy_role: resolveDeployRole(env),
        public_surface: resolvePublicSurface(env),
    };
}
