// SPDX-License-Identifier: Apache-2.0
import { LayeredModelConfigSource } from './layered-model-config-source.js';
import { ReflectAssist } from '../../../business/utils/reflect-assist.js';
import { ConfigurationError } from '../api/configuration-error.js';
import { instanceToPlain } from 'class-transformer';
import { IllegalArgumentError } from '../../../business/errors/illegal-argument-error.js';
export class MutableModelConfigSource extends LayeredModelConfigSource {
    constructor(key, schema, backend, mapper, prefix) {
        super(key, schema, backend, mapper, prefix);
    }
    async persist() {
        if (!ReflectAssist.isObjectStorageBackend(this.backend)) {
            throw new ConfigurationError('backend must be a persistable storage backend');
        }
        await this.backend.writeObject(this.key, instanceToPlain(this.modelData));
    }
    putObject(key, value) {
        if (!key) {
            throw new IllegalArgumentError('key must not be null or undefined');
        }
        this.mapper.applyPropertyValue(this.modelData, key, value);
        this.forest.addOrReplaceValue(key, JSON.stringify(value));
    }
    putObjectArray(key, value) {
        if (!key) {
            throw new IllegalArgumentError('key must not be null or undefined');
        }
        this.mapper.applyPropertyValue(this.modelData, key, value);
        this.forest.addOrReplaceArray(key, value);
    }
    putScalar(key, value) {
        if (!key) {
            throw new IllegalArgumentError('key must not be null or undefined');
        }
        if (!this.forest.has(key)) {
            throw new ConfigurationError(`model object does not support the specified key: ${key}`);
        }
        const stringValue = value === null ? null : value.toString();
        this.forest.addOrReplaceValue(key, stringValue);
        this.mapper.applyPropertyValue(this.modelData, key, value);
    }
    putScalarArray(key, value) {
        if (!key) {
            throw new IllegalArgumentError('key must not be null or undefined');
        }
        this.forest.addOrReplaceArray(key, value);
        this.mapper.applyPropertyValue(this.modelData, key, value);
    }
}
//# sourceMappingURL=mutable-model-config-source.js.map