/**
 * 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 { Migration } from './migration.js';
import { SoloError } from '../../errors.js';
import * as k8s from '@kubernetes/client-node';
/**
 * Represent the remote config metadata object and handles:
 * - Validation
 * - Reading
 * - Making a migration
 * - Converting from and to plain object
 */
export class RemoteConfigMetadata {
    _name;
    _lastUpdatedAt;
    _lastUpdateBy;
    _migration;
    constructor(name, lastUpdatedAt, lastUpdateBy, migration) {
        this._name = name;
        this._lastUpdatedAt = lastUpdatedAt;
        this._lastUpdateBy = lastUpdateBy;
        this._migration = migration;
        this.validate();
    }
    /* -------- Modifiers -------- */
    /** Simplifies making a migration */
    makeMigration(email, fromVersion) {
        this._migration = new Migration(new Date(), email, fromVersion);
    }
    /* -------- Getters -------- */
    /** Retrieves the namespace */
    get name() {
        return this._name;
    }
    /** Retrieves the date when the remote config metadata was last updated */
    get lastUpdatedAt() {
        return this._lastUpdatedAt;
    }
    /** Retrieves the email of the user who last updated the remote config metadata */
    get lastUpdateBy() {
        return this._lastUpdateBy;
    }
    /** Retrieves the migration if such exists */
    get migration() {
        return this._migration;
    }
    /* -------- Utilities -------- */
    /** Handles conversion from plain object to instance */
    static fromObject(metadata) {
        let migration = undefined;
        if (metadata.migration) {
            const { migration: { migratedAt, migratedBy, fromVersion }, } = metadata;
            migration = new Migration(new Date(migratedAt), migratedBy, fromVersion);
        }
        return new RemoteConfigMetadata(metadata.name, new Date(metadata.lastUpdatedAt), metadata.lastUpdateBy, migration);
    }
    validate() {
        if (!this.name || typeof this.name !== 'string') {
            throw new SoloError(`Invalid name: ${this.name}`);
        }
        if (!(this.lastUpdatedAt instanceof Date)) {
            throw new SoloError(`Invalid lastUpdatedAt: ${this.lastUpdatedAt}`);
        }
        if (!this.lastUpdateBy || typeof this.lastUpdateBy !== 'string') {
            throw new SoloError(`Invalid lastUpdateBy: ${this.lastUpdateBy}`);
        }
        if (this.migration && !(this.migration instanceof Migration)) {
            throw new SoloError(`Invalid migration: ${this.migration}`);
        }
    }
    toObject() {
        const data = {
            name: this.name,
            lastUpdatedAt: new k8s.V1MicroTime(this.lastUpdatedAt),
            lastUpdateBy: this.lastUpdateBy,
        };
        if (this.migration)
            data.migration = this.migration.toObject();
        return data;
    }
}
//# sourceMappingURL=metadata.js.map