"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getStartupConfig = void 0;
const os_1 = require("os");
const os_2 = require("os");
const path_1 = require("path");
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 = process.env.UIX_CONFIG_PATH || (0, path_1.resolve)((0, 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, os_2.networkInterfaces)()).filter(([, addresses]) => {
        return addresses.find(x => x.family === 'IPv6');
    }).length;
    config.host = ipv6 ? '::' : '0.0.0.0';
    if (!ui) {
        return config;
    }
    if (ui.host) {
        config.host = ui.host;
    }
    if (ui.ssl && ((ui.ssl.key && ui.ssl.cert) || ui.ssl.pfx)) {
        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('WARNING: COULD NOT START SERVER WITH SSL ENABLED');
            logger.error(e);
        }
    }
    if (ui.proxyHost) {
        config.cspWsOverride = `wss://${ui.proxyHost} ws://${ui.proxyHost}`;
    }
    if (ui.debug) {
        config.debug = true;
        process.env.UIX_DEBUG_LOGGING = '1';
    }
    else {
        config.debug = false;
        process.env.UIX_DEBUG_LOGGING = '0';
    }
    return config;
}
exports.getStartupConfig = getStartupConfig;
//# sourceMappingURL=config.startup.js.map