// SPDX-License-Identifier: Apache-2.0
import { LayeredConfigSource } from './layered-config-source.js';
import { ReflectAssist } from '../../../business/utils/reflect-assist.js';
import { ConfigurationError } from '../api/configuration-error.js';
import { IllegalArgumentError } from '../../../business/errors/illegal-argument-error.js';
import { Forest } from '../../key/lexer/forest.js';
export class LayeredModelConfigSource extends LayeredConfigSource {
    key;
    schema;
    _modelData;
    get modelData() {
        return this._modelData;
    }
    set modelData(value) {
        this._modelData = value;
    }
    constructor(key, schema, backend, mapper, prefix) {
        super(backend, mapper, prefix);
        this.key = key;
        this.schema = schema;
        if (!key) {
            throw new IllegalArgumentError('key must not be null or undefined');
        }
        if (!ReflectAssist.isObjectStorageBackend(this.backend)) {
            throw new IllegalArgumentError('backend must be an object storage backend');
        }
        if (!schema) {
            throw new IllegalArgumentError('schema must not be null or undefined');
        }
        if (!mapper) {
            throw new IllegalArgumentError('mapper must not be null or undefined');
        }
    }
    async load() {
        if (!ReflectAssist.isObjectStorageBackend(this.backend)) {
            throw new ConfigurationError('backend must be an object storage backend');
        }
        this.forest = null;
        const value = await this.backend.readObject(this.key);
        this.modelData = await this.schema.transform(value);
        this.forest = Forest.from(this.mapper.toFlatKeyMap(this.modelData));
    }
}
//# sourceMappingURL=layered-model-config-source.js.map