"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveEnvironmentConfig = resolveEnvironmentConfig;
exports.formatEnvironmentList = formatEnvironmentList;
const DEFAULT_ENVIRONMENTS = {
    dev: {
        apiBase: 'http://localhost:4000',
        webBase: 'http://localhost:8080',
        cdnBase: 'http://localhost:8082',
        aiBase: 'http://localhost:8083',
    },
    local: {
        apiBase: 'http://localhost:4000',
        webBase: 'https://www.localhost',
        cdnBase: 'https://assets-local.playdrop.ai',
        aiBase: 'https://ai.localhost',
    },
    prod: {
        apiBase: 'https://api.playdrop.ai',
        webBase: 'https://www.playdrop.ai',
        cdnBase: 'https://assets.playdrop.ai',
        aiBase: 'https://ai.playdrop.ai',
    },
    preview: {
        apiBase: 'https://api.preview.playdrop.ai',
        webBase: 'https://preview.playdrop.ai',
        cdnBase: 'https://assets.playdrop.ai',
        aiBase: 'https://ai.preview.playdrop.ai',
    },
    blue: {
        apiBase: 'https://api.blue.playdrop.ai',
        webBase: 'https://blue.playdrop.ai',
        cdnBase: 'https://assets.playdrop.ai',
        aiBase: 'https://ai.blue.playdrop.ai',
    },
    green: {
        apiBase: 'https://api.green.playdrop.ai',
        webBase: 'https://green.playdrop.ai',
        cdnBase: 'https://assets.playdrop.ai',
        aiBase: 'https://ai.green.playdrop.ai',
    }
};
function normalizeEnvironment(env) {
    if (!env)
        return undefined;
    const lowered = env.toLowerCase();
    if (lowered === 'dev'
        || lowered === 'local'
        || lowered === 'prod'
        || lowered === 'preview'
        || lowered === 'blue'
        || lowered === 'green') {
        return lowered;
    }
    return undefined;
}
function trimTrailingSlash(value) {
    if (!value)
        return value;
    return value.replace(/\/$/, '');
}
function resolveEnvironmentConfig(env) {
    const normalized = normalizeEnvironment(env);
    if (!normalized) {
        return undefined;
    }
    const defaults = DEFAULT_ENVIRONMENTS[normalized];
    if (!defaults) {
        return undefined;
    }
    const upper = normalized.toUpperCase();
    const apiOverride = process.env[`PLAYDROP_API_BASE_${upper}`] || process.env.PLAYDROP_API_BASE;
    const webOverride = process.env[`PLAYDROP_WEB_BASE_${upper}`] || process.env.PLAYDROP_WEB_BASE;
    const aiOverride = process.env[`PLAYDROP_AI_BASE_${upper}`] || process.env.PLAYDROP_AI_BASE;
    const insecureOverride = process.env[`PLAYDROP_INSECURE_${upper}`];
    const cdnOverride = process.env[`PLAYDROP_CDN_BASE_${upper}`] || process.env.PLAYDROP_CDN_BASE;
    const allowInsecure = insecureOverride === '1' || process.env.PLAYDROP_INSECURE === '1';
    return {
        name: normalized,
        apiBase: trimTrailingSlash(apiOverride || defaults.apiBase),
        webBase: trimTrailingSlash(webOverride || defaults.webBase),
        cdnBase: trimTrailingSlash(cdnOverride || defaults.cdnBase),
        aiBase: trimTrailingSlash(aiOverride || defaults.aiBase),
        allowInsecureRequests: allowInsecure,
    };
}
function formatEnvironmentList() {
    return Object.keys(DEFAULT_ENVIRONMENTS).join(', ');
}
