"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get ensurePortSet () {
        return ensurePortSet;
    },
    get fetchServerPort () {
        return fetchServerPort;
    },
    get isEnvProd () {
        return isEnvProd;
    },
    get setupEnvConfig () {
        return setupEnvConfig;
    },
    get setupSentry () {
        return setupSentry;
    }
});
const _nodepath = require("node:path");
const _node = /*#__PURE__*/ _interop_require_wildcard(require("@sentry/node"));
const _serverconstants = require("./server.constants");
const _logger = require("./handlers/logger");
const _envutils = require("./utils/env.utils");
const _errorutils = require("./utils/error.utils");
const _fsutils = require("./utils/fs.utils");
const _promclient = require("prom-client");
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const instructionsReferralURL = "https://docs.fdm-monster.net";
const packageJsonPath = (0, _nodepath.join)((0, _fsutils.superRootPath)(), "./package.json");
function isEnvTest() {
    return process.env[_serverconstants.AppConstants.NODE_ENV_KEY] === _serverconstants.AppConstants.defaultTestEnv;
}
function isEnvProd() {
    return process.env[_serverconstants.AppConstants.NODE_ENV_KEY] === _serverconstants.AppConstants.defaultProductionEnv;
}
function ensureNodeEnvSet() {
    const logger = new _logger.LoggerService("FDM-Environment");
    const environment = process.env[_serverconstants.AppConstants.NODE_ENV_KEY];
    if (!environment || !_serverconstants.AppConstants.knownEnvNames.includes(environment)) {
        const newEnvName = _serverconstants.AppConstants.defaultProductionEnv;
        process.env[_serverconstants.AppConstants.NODE_ENV_KEY] = newEnvName;
        logger.warn(`NODE_ENV=${environment} was not set, or not known. Defaulting to NODE_ENV=${newEnvName}`);
    } else {
        logger.log(`✓ NODE_ENV variable correctly set (${environment})!`);
    }
}
function ensurePackageVersionSet() {
    const logger = new _logger.LoggerService("FDM-Environment");
    const packageJsonVersion = require(packageJsonPath).version;
    process.env[_serverconstants.AppConstants.VERSION_KEY] ??= packageJsonVersion;
    logger.log(`✓ Running server version ${process.env[_serverconstants.AppConstants.VERSION_KEY]}`);
}
function printInstructionsURL() {
    const logger = new _logger.LoggerService("FDM-Environment");
    logger.log(`Please make sure to read ${instructionsReferralURL} for more information.`);
}
function fetchServerPort() {
    let port = process.env[_serverconstants.AppConstants.SERVER_PORT_KEY];
    if (Number.isNaN(Number.parseInt(port))) {
        process.env[_serverconstants.AppConstants.SERVER_PORT_KEY] = _serverconstants.AppConstants.defaultServerPort.toString();
        port = process.env[_serverconstants.AppConstants.SERVER_PORT_KEY];
    }
    return port;
}
function setupSentry() {
    const logger = new _logger.LoggerService("FDM-Environment");
    const sentryDsnToken = (0, _envutils.getEnvOrDefault)(_serverconstants.AppConstants.sentryCustomDsnToken, _serverconstants.AppConstants.sentryCustomDsnDefault);
    _node.init({
        dsn: sentryDsnToken,
        environment: process.env.NODE_ENV,
        release: process.env.npm_package_version,
        enabled: !isEnvTest(),
        tracesSampleRate: (0, _envutils.isProductionEnvironment)() ? 0.25 : 1
    });
    process.on("unhandledRejection", (e)=>{
        const message = `Unhandled rejection error - ${(0, _errorutils.errorSummary)(e)}`;
        logger.error(message);
        _node.captureException(e);
    });
}
function ensurePortSet() {
    const logger = new _logger.LoggerService("FDM-Environment");
    fetchServerPort();
    if (!process.env[_serverconstants.AppConstants.SERVER_PORT_KEY]) {
        logger.log(`~ ${_serverconstants.AppConstants.SERVER_PORT_KEY} environment variable is not set`);
        printInstructionsURL();
        process.env[_serverconstants.AppConstants.SERVER_PORT_KEY] = _serverconstants.AppConstants.defaultServerPort.toString();
    }
}
function setupEnvConfig() {
    ensureNodeEnvSet();
    ensurePackageVersionSet();
    setupSentry();
    ensurePortSet();
    if (process.env[_serverconstants.AppConstants.ENABLE_PROMETHEUS_METRICS] === "true") {
        (0, _promclient.collectDefaultMetrics)({
            register: _promclient.register
        });
        _promclient.register.removeSingleMetric("nodejs_version_info");
    }
}

//# sourceMappingURL=server.env.js.map