"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.env = void 0;
const zod_1 = require("zod");
const envSchema = zod_1.z.object({
    NODE_ENV: zod_1.z.enum(['development', 'production', 'test']).default('development'),
    HOST: zod_1.z.string().default('0.0.0.0'),
    PORT: zod_1.z.string().default('3900').transform((value) => parseInt(value, 10)),
    DB_HOST: zod_1.z.string(),
    DB_PORT: zod_1.z.string().default('3306').transform((value) => parseInt(value, 10)),
    DB_USER: zod_1.z.string(),
    DB_PASS: zod_1.z.string(),
    DB_NAME: zod_1.z.string(),
    REDIS_URL: zod_1.z.string(),
    ENCRYPTION_MASTER_KEY: zod_1.z
        .string()
        .length(64)
        .regex(/^[0-9a-fA-F]{64}$/, 'ENCRYPTION_MASTER_KEY must be 32 bytes hex-encoded (64 hex chars)'),
    LOG_LEVEL: zod_1.z.enum(['debug', 'info', 'warn', 'error']).default('info'),
    ALLOWED_ORIGINS: zod_1.z.string(),
    ADMIN_API_KEY: zod_1.z.string().min(1).optional(),
    HMAC_SECRET: zod_1.z.string().min(1).optional(),
    RATE_LIMIT_WINDOW_MS: zod_1.z.string().default('900000').transform((value) => parseInt(value, 10)),
    RATE_LIMIT_MAX: zod_1.z.string().default('100').transform((value) => parseInt(value, 10)),
    ONESIGNAL_APP_ID: zod_1.z.string().min(1).optional(),
    ONESIGNAL_REST_API_KEY: zod_1.z.string().min(1).optional(),
    PUBLIC_BASE_URL: zod_1.z.string().url().optional(),
    IMAGES_DIR: zod_1.z.string().default('public/images'),
    WEBSOCKET_ENABLED: zod_1.z.enum(['true', 'false']).default('true'),
    WEBSOCKET_PATH: zod_1.z.string().default('/ws'),
    COINMARKETCAP_API_BASE_URL: zod_1.z.string().default('https://pro-api.coinmarketcap.com/v1'),
    COINMARKETCAP_API_KEYS: zod_1.z
        .string()
        .min(1, 'COINMARKETCAP_API_KEYS is required')
        .transform((value) => value
        .split(',')
        .map((key) => key.trim())
        .filter((key) => key.length > 0))
        .refine((keys) => keys.length > 0, 'At least one CoinMarketCap API key is required'),
});
const parseEnv = () => {
    try {
        const parsed = envSchema.parse(process.env);
        return {
            ...parsed,
            PUBLIC_BASE_URL: parsed.PUBLIC_BASE_URL ?? `http://localhost:${parsed.PORT}`,
        };
    }
    catch (error) {
        if (error instanceof zod_1.z.ZodError) {
            const issues = error.issues.map((issue) => `${issue.path.join('.')}: ${issue.message}`).join('\n');
            // eslint-disable-next-line no-console
            console.error(`Invalid environment configuration:\n${issues}`);
        }
        else {
            // eslint-disable-next-line no-console
            console.error('Unexpected error parsing environment variables', error);
        }
        process.exit(1);
    }
};
exports.env = parseEnv();
//# sourceMappingURL=env.js.map