import { InvalidConfigurationError } from '../errors/configuration-error.js';
export 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 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 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;
    }
}
//# sourceMappingURL=env.js.map