/**
 * Copyright (C) 2024 Hedera Hashgraph, LLC
 *
 * Licensed under the Apache License, Version 2.0 (the ""License"");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an ""AS IS"" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
import { SoloError } from '../../errors.js';
import * as yaml from 'yaml';
import { RemoteConfigMetadata } from './metadata.js';
import { ComponentsDataWrapper } from './components_data_wrapper.js';
import * as constants from '../../constants.js';
export class RemoteConfigDataWrapper {
    _version = '1.0.0';
    _metadata;
    _clusters;
    _components;
    _commandHistory;
    _lastExecutedCommand;
    constructor(data) {
        this._metadata = data.metadata;
        this._clusters = data.clusters;
        this._components = data.components;
        this._commandHistory = data.commandHistory;
        this._lastExecutedCommand = data.lastExecutedCommand ?? '';
        this.validate();
    }
    //! -------- Modifiers -------- //
    addCommandToHistory(command) {
        this._commandHistory.push(command);
        this.lastExecutedCommand = command;
        if (this._commandHistory.length > constants.SOLO_REMOTE_CONFIG_MAX_COMMAND_IN_HISTORY) {
            this._commandHistory.shift();
        }
        this.validate();
    }
    //! -------- Getters & Setters -------- //
    get version() {
        return this._version;
    }
    get metadata() {
        return this._metadata;
    }
    set metadata(metadata) {
        this._metadata = metadata;
        this.validate();
    }
    get clusters() {
        return this._clusters;
    }
    set clusters(clusters) {
        this._clusters = clusters;
        this.validate();
    }
    get components() {
        return this._components;
    }
    set components(components) {
        this._components = components;
        this.validate();
    }
    get lastExecutedCommand() {
        return this._lastExecutedCommand;
    }
    set lastExecutedCommand(lastExecutedCommand) {
        this._lastExecutedCommand = lastExecutedCommand;
        this.validate();
    }
    get commandHistory() {
        return this._commandHistory;
    }
    set commandHistory(commandHistory) {
        this._commandHistory = commandHistory;
        this.validate();
    }
    //! -------- Utilities -------- //
    static compare(x, y) {
        // TODO
        return true;
    }
    static fromConfigmap(configMap) {
        const data = yaml.parse(configMap.data['remote-config-data']);
        return new RemoteConfigDataWrapper({
            metadata: RemoteConfigMetadata.fromObject(data.metadata),
            components: ComponentsDataWrapper.fromObject(data.components),
            clusters: data.clusters,
            commandHistory: data.commandHistory,
            lastExecutedCommand: data.lastExecutedCommand,
        });
    }
    validate() {
        if (!this._version || typeof this._version !== 'string') {
            throw new SoloError(`Invalid remote config version: ${this._version}`);
        }
        if (!this.metadata || !(this.metadata instanceof RemoteConfigMetadata)) {
            throw new SoloError(`Invalid remote config metadata: ${this.metadata}`);
        }
        if (!this.lastExecutedCommand || typeof this.lastExecutedCommand !== 'string') {
            throw new SoloError(`Invalid remote config last executed command: ${this.lastExecutedCommand}`);
        }
        if (!Array.isArray(this.commandHistory) || this.commandHistory.some(c => typeof c !== 'string')) {
            throw new SoloError(`Invalid remote config command history: ${this.commandHistory}`);
        }
        Object.entries(this.clusters).forEach(([cluster, namespace]) => {
            const clusterDataString = `cluster: { name: ${cluster}, namespace: ${namespace} }`;
            if (!cluster || typeof cluster !== 'string') {
                throw new SoloError(`Invalid remote config clusters name: ${clusterDataString}`);
            }
            if (!namespace || typeof namespace !== 'string') {
                throw new SoloError(`Invalid remote config clusters namespace: ${clusterDataString}`);
            }
        });
    }
    toObject() {
        return {
            metadata: this.metadata.toObject(),
            version: this.version,
            clusters: this.clusters,
            components: this.components.toObject(),
            commandHistory: this.commandHistory,
            lastExecutedCommand: this.lastExecutedCommand,
        };
    }
}
//# sourceMappingURL=remote_config_data_wrapper.js.map