"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EnvStore = void 0;
const configuration_error_js_1 = require("../errors/configuration-error.js");
class EnvStore {
    vars;
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    values;
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    constructor(values) {
        this.vars = {};
        this.values = values;
    }
    add(env) {
        const name = env.name;
        const value = this.values[name];
        if (env.defaultValue === undefined && value == null) {
            throw new configuration_error_js_1.InvalidConfigurationError(`Env variable ${name} is missing`);
        }
        // TODO validate env var with regex
        this.vars[name] = env;
    }
    addAll(envs) {
        for (const env of envs) {
            this.add(env);
        }
    }
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    get(name) {
        const env = this.vars[name];
        if (env === undefined) {
            throw new configuration_error_js_1.InvalidConfigurationError(`Env variable ${name} is missing, use addEnv to add it first`);
        }
        if (env.regex) {
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            const matchedKeys = {};
            Object.entries(this.values).forEach(([key, value]) => {
                if (key.match(env.regex ?? '')) {
                    matchedKeys[key] = value;
                }
            });
            return matchedKeys;
        }
        const value = this.values[name];
        if (value == null) {
            return env.defaultValue;
        }
        return value;
    }
}
exports.EnvStore = EnvStore;
//# sourceMappingURL=env.js.map