// SPDX-License-Identifier: Apache-2.0
import { VersionRange } from '../../../../../business/utils/version-range.js';
import { Version } from '../../../../../business/utils/version.js';
import { IllegalArgumentError } from '../../../../../business/errors/illegal-argument-error.js';
import { ComponentIdsSchema } from '../../../model/remote/state/component-ids-schema.js';
import { Templates } from '../../../../../core/templates.js';
export class RemoteConfigV3Migration {
    get range() {
        return VersionRange.fromIntegerVersion(2);
    }
    get version() {
        return new Version(3);
    }
    async migrate(source) {
        if (!source) {
            // We should never pass null or undefined to this method, if this happens we should throw an error
            throw new IllegalArgumentError('source must not be null or undefined');
        }
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        const clone = structuredClone(source);
        const state = clone.state;
        const componentIds = new ComponentIdsSchema();
        componentIds.consensusNodes = (state?.consensusNodes?.length || 0) + 1;
        componentIds.envoyProxies = (state?.envoyProxies?.length || 0) + 1;
        componentIds.mirrorNodes = (state?.mirrorNodes?.length || 0) + 1;
        componentIds.explorers = (state?.explorers?.length || 0) + 1;
        componentIds.haProxies = (state?.haProxies?.length || 0) + 1;
        componentIds.blockNodes = (state?.blockNodes?.length || 0) + 1;
        componentIds.relayNodes = (state?.relayNodes?.length || 0) + 1;
        clone.state.componentIds = componentIds;
        // eslint-disable-next-line unicorn/consistent-function-scoping
        function incrementComponentIds(components) {
            for (const component of components) {
                component.metadata.id++;
            }
        }
        incrementComponentIds(clone.state.consensusNodes);
        incrementComponentIds(clone.state.envoyProxies);
        incrementComponentIds(clone.state.mirrorNodes);
        incrementComponentIds(clone.state.explorers);
        incrementComponentIds(clone.state.haProxies);
        incrementComponentIds(clone.state.blockNodes);
        incrementComponentIds(clone.state.relayNodes);
        for (const component of clone.state.relayNodes) {
            if (component?.metadata?.consensusNodeIds) {
                if (typeof component?.metadata?.consensusNodeIds?.[0] === 'string') {
                    component.consensusNodeIds = component.metadata.consensusNodeIds.map((nodeAlias) => Templates.nodeIdFromNodeAlias(nodeAlias));
                }
                else if (typeof component?.metadata?.consensusNodeIds?.[0] === 'number') {
                    component.consensusNodeIds = component.metadata.consensusNodeIds;
                }
                delete component.metadata.consensusNodeIds;
            }
        }
        // Set the schema version to the new version
        clone.schemaVersion = this.version.value;
        return clone;
    }
}
//# sourceMappingURL=remote-config-v3-migration.js.map