"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Configuration = void 0;
const logger_1 = require("@refloat/logger");
const os_1 = require("os");
const schema_1 = require("./generated/schema");
const error_1 = require("./utils/error");
var Configuration;
(function (Configuration) {
    let configuration = null;
    function set(options) {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
        const configured = configuration !== null;
        configuration = {
            token: (_b = (_a = options.token) !== null && _a !== void 0 ? _a : configuration === null || configuration === void 0 ? void 0 : configuration.token) !== null && _b !== void 0 ? _b : required('TOKEN'),
            debug: (_d = (_c = options.debug) !== null && _c !== void 0 ? _c : configuration === null || configuration === void 0 ? void 0 : configuration.debug) !== null && _d !== void 0 ? _d : optional.boolean('DEBUG'),
            platform: (_f = (_e = options.platform) !== null && _e !== void 0 ? _e : configuration === null || configuration === void 0 ? void 0 : configuration.platform) !== null && _f !== void 0 ? _f : getPlatform(),
            api: new URL(((_j = (_h = (_g = options.api) === null || _g === void 0 ? void 0 : _g.href) !== null && _h !== void 0 ? _h : configuration === null || configuration === void 0 ? void 0 : configuration.api.href) !== null && _j !== void 0 ? _j : required('API')).replace(/\/+$/, '')),
            name: (_m = (_l = (_k = options.name) !== null && _k !== void 0 ? _k : configuration === null || configuration === void 0 ? void 0 : configuration.name) !== null && _l !== void 0 ? _l : optional('AGENT_NAME')) !== null && _m !== void 0 ? _m : (0, os_1.hostname)(),
        };
        if (!configured || typeof options.debug === 'boolean') {
            logger_1.Logger.configure({ debug: configuration.debug });
        }
        return configuration;
    }
    Configuration.set = set;
    function get() {
        return configuration !== null && configuration !== void 0 ? configuration : set({});
    }
    Configuration.get = get;
    function required(name, defaultValue) {
        var _a, _b;
        const key = prefix(name);
        return ((_b = (_a = process.env[key]) !== null && _a !== void 0 ? _a : defaultValue) !== null && _b !== void 0 ? _b : (0, error_1.error)(`Please configure "${key}"`));
    }
    Configuration.required = required;
    function optional(name, defaultValue) {
        var _a, _b;
        const key = prefix(name);
        return (_b = (_a = process.env[key]) !== null && _a !== void 0 ? _a : defaultValue) !== null && _b !== void 0 ? _b : null;
    }
    Configuration.optional = optional;
    (function (optional) {
        function boolean(name, defaultValue) {
            const value = optional(name, String(defaultValue));
            return value === 'true' || value === '1';
        }
        optional.boolean = boolean;
    })(optional = Configuration.optional || (Configuration.optional = {}));
})(Configuration = exports.Configuration || (exports.Configuration = {}));
function prefix(name) {
    return `REFLOAT_${name}`;
}
function getPlatform() {
    switch (process.platform) {
        case 'darwin':
            return schema_1.AgentPlatform.macOS;
        case 'linux':
            return schema_1.AgentPlatform.Linux;
        case 'win32':
            return schema_1.AgentPlatform.Windows;
        default:
            throw new Error(`Unsupported platform: "${process.platform}"`);
    }
}
//# sourceMappingURL=config.js.map