"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isStaticIpStep = exports.canNextNetwork = exports.canNextOperators = exports.canNextStorage = exports.canNextHostDiscovery = exports.canNextClusterDetails = exports.allClusterWizardSoftValidationIds = exports.wizardStepsValidationsMap = exports.getClusterWizardFirstStep = exports.isStepAfter = exports.getLastStepForWizard = exports.ClusterWizardFlowStateNew = exports.disconnectedSteps = void 0;
const validationsInfoUtils_1 = require("../../../common/components/clusterWizard/validationsInfoUtils");
const dataTypes_1 = require("../clusterConfiguration/staticIp/data/dataTypes");
const utils_1 = require("../../../common/utils");
const wizardStepsOrder = [
    'cluster-details',
    'static-ip-yaml-view',
    'static-ip-network-wide-configurations',
    'static-ip-host-configurations',
    'operators',
    'host-discovery',
    'storage',
    'networking',
    'custom-manifests',
    'credentials-download',
    'review',
];
exports.disconnectedSteps = [
    'disconnected-basic',
    'disconnected-review',
];
exports.ClusterWizardFlowStateNew = 'new';
const getLastStepForWizard = (customManifestsStepNeedsToBeFilled) => {
    if (customManifestsStepNeedsToBeFilled) {
        return 'custom-manifests';
    }
    else {
        return 'review';
    }
};
exports.getLastStepForWizard = getLastStepForWizard;
const isStepAfter = (stepA, stepB) => {
    const indexA = wizardStepsOrder.findIndex((step) => step === stepA);
    const indexB = wizardStepsOrder.findIndex((step) => step === stepB);
    if (indexA === -1 || indexB === -1) {
        return false; // Missing step in "wizardStepsOrder"
    }
    return indexA > indexB;
};
exports.isStepAfter = isStepAfter;
const getClusterWizardFirstStep = (locationState, staticIpInfo, state, hosts, customManifestsStepNeedsToBeFilled) => {
    // Just for the first time when the cluster is created
    if (locationState === exports.ClusterWizardFlowStateNew && !staticIpInfo) {
        return 'operators';
    }
    if (staticIpInfo && !staticIpInfo.isDataComplete) {
        if (staticIpInfo.view === dataTypes_1.StaticIpView.YAML) {
            return 'static-ip-yaml-view';
        }
        return 'static-ip-network-wide-configurations';
    }
    switch (state) {
        case 'ready':
            return (0, exports.getLastStepForWizard)(customManifestsStepNeedsToBeFilled || false);
        case 'pending-for-input':
        case 'adding-hosts':
        case 'insufficient':
            if (customManifestsStepNeedsToBeFilled) {
                return 'custom-manifests';
            }
            return getStepForFailingHostValidations(hosts);
        default:
            return 'cluster-details';
    }
};
exports.getClusterWizardFirstStep = getClusterWizardFirstStep;
const getStepForFailingHostValidations = (hosts) => {
    const failingValidations = getHostFailingValidationIds(hosts);
    const minimumStep = 'host-discovery';
    let step;
    for (const failingValidationId of failingValidations) {
        step = (0, validationsInfoUtils_1.findValidationStep)(failingValidationId, exports.wizardStepsValidationsMap, minimumStep);
        if (step !== undefined) {
            break;
        }
    }
    return (step || minimumStep);
};
const getHostFailingValidationIds = (hosts) => {
    const failingValidations = [];
    hosts === null || hosts === void 0 ? void 0 : hosts.forEach((host) => {
        const validationsInfo = (0, utils_1.stringToJSON)(host.validationsInfo) || {};
        (0, utils_1.getKeys)(validationsInfo).forEach((group) => {
            const f = (validation) => {
                if (validation.status === 'failure') {
                    failingValidations.push(validation.id);
                }
            };
            const validationGroup = validationsInfo[group];
            validationGroup.forEach(f);
        });
    });
    return failingValidations;
};
const buildEmptyValidationsMap = () => ({
    cluster: {
        groups: [],
        validationIds: [],
    },
    host: {
        allowedStatuses: [],
        groups: [],
        validationIds: [],
    },
    softValidationIds: [],
});
const staticIpValidationsMap = buildEmptyValidationsMap();
const operatorsStepValidationsMap = buildEmptyValidationsMap();
const clusterDetailsStepValidationsMap = {
    cluster: {
        groups: [],
        validationIds: ['pull-secret-set', 'dns-domain-defined'],
    },
    host: {
        allowedStatuses: [],
        groups: [],
        validationIds: [],
    },
    softValidationIds: [],
};
const hostDiscoveryStepValidationsMap = {
    cluster: {
        groups: [],
        validationIds: [
            'sufficient-masters-count',
            'lso-requirements-satisfied',
            'odf-requirements-satisfied',
            'lvm-requirements-satisfied',
            'cnv-requirements-satisfied',
            'nvidia-gpu-requirements-satisfied',
            'openshift-ai-requirements-satisfied',
            'osc-requirements-satisfied',
            'amd-gpu-requirements-satisfied',
        ],
    },
    host: {
        allowedStatuses: ['known', 'disabled'],
        groups: ['hardware'],
        validationIds: [
            'connected',
            'media-connected',
            'lso-requirements-satisfied',
            'odf-requirements-satisfied',
            'lvm-requirements-satisfied',
            'cnv-requirements-satisfied',
            'nvidia-gpu-requirements-satisfied',
            'openshift-ai-requirements-satisfied',
            'osc-requirements-satisfied',
            'amd-gpu-requirements-satisfied',
        ],
    },
    softValidationIds: [
        'no-skip-installation-disk',
        'no-skip-missing-disk',
        'compatible-agent',
        'openshift-ai-gpu-requirements-satisfied',
    ],
};
const storageStepValidationsMap = {
    cluster: {
        groups: [],
        validationIds: ['sufficient-masters-count'],
    },
    host: {
        allowedStatuses: ['known', 'disabled'],
        groups: ['hardware'],
        validationIds: [
            'connected',
            'media-connected',
            'no-skip-installation-disk',
            'no-skip-missing-disk',
        ],
    },
    softValidationIds: [],
};
const networkingStepValidationsMap = {
    cluster: {
        groups: ['network'],
        validationIds: [],
    },
    host: {
        allowedStatuses: ['known', 'disabled'],
        groups: ['network'],
        validationIds: [],
    },
    // TODO(jtomasek): container-images-available validation is currently not running on the backend, it stays in pending.
    // marking it as soft validation is the easy way to prevent it from blocking the progress.
    // Alternatively we would have to whitelist network validations instead of using group
    // TODO(mlibra): remove that container-images-available from soft validations and let backend drive it via disabling it.
    //   Depends on: https://issues.redhat.com/browse/MGMT-5265
    softValidationIds: [
        'ntp-synced',
        'container-images-available',
        'mtu-valid',
        'custom-manifests-requirements-satisfied',
    ],
};
const reviewStepValidationsMap = {
    cluster: {
        groups: [],
        validationIds: ['all-hosts-are-ready-to-install'],
    },
    host: {
        allowedStatuses: ['known', 'disabled'],
        groups: [],
        validationIds: [],
    },
    softValidationIds: [],
};
const credentialsValidationMap = buildEmptyValidationsMap();
const customManifestsValidationsMap = buildEmptyValidationsMap();
const disconnectedReviewValidationsMap = buildEmptyValidationsMap();
const disconnectedBasicValidationsMap = buildEmptyValidationsMap();
exports.wizardStepsValidationsMap = {
    'cluster-details': clusterDetailsStepValidationsMap,
    'static-ip-yaml-view': staticIpValidationsMap,
    'static-ip-network-wide-configurations': staticIpValidationsMap,
    'static-ip-host-configurations': staticIpValidationsMap,
    'custom-manifests': customManifestsValidationsMap,
    operators: operatorsStepValidationsMap,
    'host-discovery': hostDiscoveryStepValidationsMap,
    storage: storageStepValidationsMap,
    networking: networkingStepValidationsMap,
    review: reviewStepValidationsMap,
    'credentials-download': credentialsValidationMap,
    'disconnected-review': disconnectedReviewValidationsMap,
    'disconnected-basic': disconnectedBasicValidationsMap,
};
exports.allClusterWizardSoftValidationIds = (0, validationsInfoUtils_1.getAllClusterWizardSoftValidationIds)(exports.wizardStepsValidationsMap);
/*
We are colocating all these canNext* functions for easier maintenance.
However transitions among steps should be independent on each other.
*/
const canNextClusterDetails = ({ cluster }) => (0, validationsInfoUtils_1.getWizardStepClusterStatus)('cluster-details', exports.wizardStepsValidationsMap, cluster, cluster.hosts) === 'ready';
exports.canNextClusterDetails = canNextClusterDetails;
const canNextHostDiscovery = ({ cluster }) => (0, validationsInfoUtils_1.getWizardStepClusterStatus)('host-discovery', exports.wizardStepsValidationsMap, cluster, cluster.hosts) === 'ready';
exports.canNextHostDiscovery = canNextHostDiscovery;
const canNextStorage = ({ cluster }) => (0, validationsInfoUtils_1.getWizardStepClusterStatus)('storage', exports.wizardStepsValidationsMap, cluster, cluster.hosts) ===
    'ready';
exports.canNextStorage = canNextStorage;
const canNextOperators = ({ cluster }) => (0, validationsInfoUtils_1.getWizardStepClusterStatus)('operators', exports.wizardStepsValidationsMap, cluster, cluster.hosts) ===
    'ready';
exports.canNextOperators = canNextOperators;
const canNextNetwork = ({ cluster }) => (0, validationsInfoUtils_1.getWizardStepClusterStatus)('networking', exports.wizardStepsValidationsMap, cluster, cluster.hosts) ===
    'ready';
exports.canNextNetwork = canNextNetwork;
const isStaticIpStep = (stepId) => {
    return stepId.startsWith('static-ip');
};
exports.isStaticIpStep = isStaticIpStep;
//# sourceMappingURL=wizardTransition.js.map