// SPDX-License-Identifier: Apache-2.0
import { FileStorageBackend } from './file-storage-backend.js';
import { StorageBackendError } from '../api/storage-backend-error.js';
import yaml from 'yaml';
import { IllegalArgumentError } from '../../../core/errors/illegal-argument-error.js';
import { PathEx } from '../../../business/utils/path-ex.js';
export class YamlFileStorageBackend extends FileStorageBackend {
    constructor(basePath) {
        super(basePath);
    }
    async readObject(key) {
        const data = await this.readBytes(key);
        const filePath = PathEx.joinWithRealPath(this.basePath, key);
        if (!data) {
            throw new StorageBackendError(`failed to read file: ${filePath}`);
        }
        if (data.length === 0) {
            throw new StorageBackendError(`file is empty: ${filePath}`);
        }
        try {
            return yaml.parse(data.toString('utf8'));
        }
        catch (error) {
            throw new StorageBackendError(`error parsing yaml file: ${filePath}`, error);
        }
    }
    async writeObject(key, data) {
        if (!data) {
            throw new IllegalArgumentError('data must not be null or undefined');
        }
        const filePath = PathEx.join(this.basePath, key);
        try {
            const yamlData = yaml.stringify(data, { sortMapEntries: true });
            await this.writeBytes(key, Buffer.from(yamlData, 'utf8'));
        }
        catch (error) {
            throw new StorageBackendError(`error writing yaml file: ${filePath}`, error);
        }
    }
}
//# sourceMappingURL=yaml-file-storage-backend.js.map