"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getStartupConfig = getStartupConfig;
const node_os_1 = require("node:os");
const node_path_1 = require("node:path");
const node_process_1 = __importDefault(require("node:process"));
const fs_extra_1 = require("fs-extra");
const logger_service_1 = require("../logger/logger.service");
async function getStartupConfig() {
    const logger = new logger_service_1.Logger();
    const configPath = node_process_1.default.env.UIX_CONFIG_PATH || (0, node_path_1.resolve)((0, node_os_1.homedir)(), '.homebridge/config.json');
    const homebridgeConfig = await (0, fs_extra_1.readJson)(configPath);
    const ui = Array.isArray(homebridgeConfig.platforms) ? homebridgeConfig.platforms.find((x) => x.platform === 'config') : undefined;
    const config = {};
    const ipv6 = Object.entries((0, node_os_1.networkInterfaces)()).filter(([, addresses]) => {
        return addresses.find(x => x.family === 'IPv6');
    }).length;
    config.host = ipv6 ? '::' : '0.0.0.0';
    config.webroot = '';
    if (!ui) {
        return config;
    }
    if (ui.host && node_process_1.default.env.UIX_DEVELOPMENT !== '1') {
        config.host = ui.host;
    }
    if (ui.ssl && ((ui.ssl.key && ui.ssl.cert) || ui.ssl.pfx) && node_process_1.default.env.UIX_DEVELOPMENT !== '1') {
        for (const attribute of ['key', 'cert', 'pfx']) {
            if (ui.ssl[attribute]) {
                if (!(await ((0, fs_extra_1.stat)(ui.ssl[attribute]))).isFile()) {
                    logger.error(`SSL config error: ui.ssl.${attribute}: ${ui.ssl[attribute]} is not a valid file.`);
                }
            }
        }
        try {
            config.httpsOptions = {
                key: ui.ssl.key ? await (0, fs_extra_1.readFile)(ui.ssl.key) : undefined,
                cert: ui.ssl.cert ? await (0, fs_extra_1.readFile)(ui.ssl.cert) : undefined,
                pfx: ui.ssl.pfx ? await (0, fs_extra_1.readFile)(ui.ssl.pfx) : undefined,
                passphrase: ui.ssl.passphrase,
            };
        }
        catch (e) {
            logger.error(`Could not start server with SSL enabled as ${e.message}.`);
            logger.error(e);
        }
    }
    if (ui.proxyHost && node_process_1.default.env.UIX_DEVELOPMENT !== '1') {
        config.cspWsOverride = `wss://${ui.proxyHost} ws://${ui.proxyHost}`;
    }
    if (ui.debug) {
        config.debug = true;
        node_process_1.default.env.UIX_DEBUG_LOGGING = '1';
    }
    else {
        config.debug = false;
        node_process_1.default.env.UIX_DEBUG_LOGGING = '0';
    }
    if (ui.webroot && node_process_1.default.env.UIX_DEVELOPMENT !== '1') {
        let webroot = `/${ui.webroot.toString().trim()}`.replace(/\/+/g, '/').replace(/\/$/, '');
        if (webroot === '/') {
            webroot = '';
        }
        config.webroot = webroot;
        logger.log(`Setting up the UI on webroot: ${webroot}`);
    }
    else {
        config.webroot = '';
    }
    return config;
}
//# sourceMappingURL=config.startup.js.map