import { resolve } from "node:path";
function required(name) {
    const value = process.env[name]?.trim();
    if (!value)
        throw new Error(`${name} is required`);
    if (value.length < 32)
        throw new Error(`${name} must contain at least 32 characters`);
    return value;
}
export function readConfiguration() {
    const port = Number(process.env.PORT ?? "8789");
    if (!Number.isInteger(port) || port < 1 || port > 65535)
        throw new Error("PORT is invalid");
    const host = process.env.HOST?.trim() || "127.0.0.1";
    if (!/^[a-zA-Z0-9.:-]+$/.test(host))
        throw new Error("HOST is invalid");
    return Object.freeze({
        host,
        port,
        apiSecret: required("BGM_BRIDGE_API_SECRET"),
        callbackSecret: required("BGM_API_CALLBACK_SECRET"),
        sessionEncryptionKey: required("BGM_SESSION_ENCRYPTION_KEY"),
        sessionDirectory: resolve(process.env.BGM_SESSION_DIRECTORY?.trim() || "./runtime/sessions"),
        logLevel: process.env.LOG_LEVEL?.trim() || "info",
    });
}
