// SPDX-License-Identifier: Apache-2.0
export class SimpleConfigSourceFixture {
    name;
    ordinal;
    prefix;
    backend;
    _properties;
    // used for testing to fake a refresh
    props2 = new Map();
    constructor(name, ordinal, prefix, backend, _properties = new Map()) {
        this.name = name;
        this.ordinal = ordinal;
        this.prefix = prefix;
        this.backend = backend;
        this._properties = _properties;
    }
    asBoolean(key) {
        const value = this._properties.get(key);
        if (value === null || value === undefined || typeof value !== 'string') {
            return null;
        }
        return value.toLowerCase() === 'true';
    }
    asNumber(key) {
        const value = this._properties.get(key);
        if (value === null || value === undefined) {
            return null;
        }
        return Number(value);
    }
    asObject(cls, key) {
        const value = this._properties.get(key ?? '');
        if (!value) {
            return null;
        }
        try {
            const parsedValue = JSON.parse(value);
            if (typeof parsedValue !== 'object' || parsedValue === null) {
                return null;
            }
            // Create a new instance and assign properties manually
            const instance = new cls();
            Object.assign(instance, parsedValue);
            return instance;
        }
        catch {
            return null;
        }
    }
    asObjectArray(cls, key) {
        const value = this._properties.get(key ?? '');
        if (!value) {
            return null;
        }
        try {
            const parsedValue = JSON.parse(value);
            if (!Array.isArray(parsedValue)) {
                return null;
            }
            return parsedValue
                .filter(item => typeof item === 'object' && item !== null)
                .map(item => {
                const instance = new cls();
                Object.assign(instance, item);
                return instance;
            });
        }
        catch {
            return null;
        }
    }
    asString(key) {
        const value = this._properties.get(key);
        if (value === null || value === undefined || typeof value !== 'string') {
            return null;
        }
        return value;
    }
    asStringArray(key) {
        const value = this._properties.get(key);
        if (value === null || value === undefined) {
            return null;
        }
        const parsedValue = JSON.parse(value);
        if (!Array.isArray(parsedValue) ||
            parsedValue.length === 0 ||
            typeof parsedValue[0] !== 'string') {
            return null;
        }
        return parsedValue;
    }
    async load() { }
    properties() {
        return this._properties;
    }
    propertyNames() {
        return new Set(this._properties.keys());
    }
    async refresh() {
        this._properties = this.props2;
    }
}
//# sourceMappingURL=simple-config-source.fixture.js.map