"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.findValidationStep = exports.areOnlySoftValidationsOfWizardStepFailing = exports.getFailingClusterWizardStepHostValidations = exports.getAllClusterWizardSoftValidationIds = exports.getWizardStepClusterStatus = exports.getWizardStepClusterValidationsInfo = exports.getWizardStepHostStatus = exports.getWizardStepHostValidationsInfo = exports.checkHostValidationGroups = exports.checkHostValidations = exports.checkClusterValidationGroups = exports.checkClusterValidations = exports.findValidationFixStep = void 0;
const tslib_1 = require("tslib");
const values_js_1 = tslib_1.__importDefault(require("lodash-es/values.js"));
const keys_js_1 = tslib_1.__importDefault(require("lodash-es/keys.js"));
const reduce_js_1 = tslib_1.__importDefault(require("lodash-es/reduce.js"));
const utils_1 = require("../../utils");
const findValidationFixStep = ({ validationId, hostGroup, clusterGroup, }, wizardStepsValidationsMap) => {
    const keys = (0, keys_js_1.default)(wizardStepsValidationsMap);
    return keys.find((key) => {
        // find first matching validation-map name
        const { cluster: clusterValidationMap, host: hostValidationMap } = wizardStepsValidationsMap[key];
        return (clusterValidationMap.validationIds.includes(validationId) ||
            hostValidationMap.validationIds.includes(validationId) ||
            (clusterGroup && clusterValidationMap.groups.includes(clusterGroup)) ||
            (hostGroup && hostValidationMap.groups.includes(hostGroup)));
    });
};
exports.findValidationFixStep = findValidationFixStep;
const checkClusterValidations = (clusterValidationsInfo, requiredIds) => {
    const requiredValidations = (0, values_js_1.default)(clusterValidationsInfo)
        .flat()
        .filter((v) => v && requiredIds.includes(v.id));
    return (requiredValidations.length === requiredIds.length &&
        requiredValidations.every((v) => (v === null || v === void 0 ? void 0 : v.status) === 'disabled' || (v === null || v === void 0 ? void 0 : v.status) === 'success'));
};
exports.checkClusterValidations = checkClusterValidations;
const checkClusterValidationGroups = (clusterValidationsInfo, groups, softValidationIds) => groups.every((group) => {
    var _a;
    return (_a = clusterValidationsInfo[group]) === null || _a === void 0 ? void 0 : _a.every((validation) => validation.status === 'disabled' ||
        validation.status === 'success' ||
        softValidationIds.includes(validation.id));
});
exports.checkClusterValidationGroups = checkClusterValidationGroups;
const checkHostValidations = (hostValidationsInfo, requiredIds) => {
    const requiredValidations = (0, values_js_1.default)(hostValidationsInfo)
        .flat()
        .filter((v) => v && requiredIds.includes(v.id));
    return (requiredValidations.length === requiredIds.length &&
        requiredValidations.every((v) => (v === null || v === void 0 ? void 0 : v.status) === 'disabled' || (v === null || v === void 0 ? void 0 : v.status) === 'success'));
};
exports.checkHostValidations = checkHostValidations;
const checkHostValidationGroups = (hostValidationsInfo, groups, softValidationIds) => groups.every((group) => {
    var _a;
    return (_a = hostValidationsInfo[group]) === null || _a === void 0 ? void 0 : _a.every((validation) => validation.status === 'disabled' ||
        validation.status === 'success' ||
        softValidationIds.includes(validation.id));
});
exports.checkHostValidationGroups = checkHostValidationGroups;
const getWizardStepHostValidationsInfo = (validationsInfo, wizardStepId, wizardStepsValidationsMap) => {
    const { groups, validationIds } = wizardStepsValidationsMap[wizardStepId].host;
    return (0, reduce_js_1.default)(validationsInfo, (result, groupValidations, groupNameStr) => {
        const groupName = groupNameStr;
        if (groups.includes(groupName)) {
            result[groupName] = groupValidations;
            return result;
        }
        const selectedValidations = (groupValidations || []).filter((validation) => validationIds.includes(validation.id));
        if (selectedValidations.length) {
            result[groupName] = selectedValidations;
            return result;
        }
        return result;
    }, {});
};
exports.getWizardStepHostValidationsInfo = getWizardStepHostValidationsInfo;
const getWizardStepHostStatus = (wizardStepId, wizardStepsValidationsMap, host) => {
    const { status } = host;
    if (['insufficient', 'pending-for-input'].includes(status)) {
        const { softValidationIds } = wizardStepsValidationsMap[wizardStepId];
        // NOTE(jtomasek): REST API validationsInfo is string, K8s validationsInfo is ClusterValidationsInfo object
        const validationsInfo = (typeof host.validationsInfo === 'string'
            ? (0, utils_1.stringToJSON)(host.validationsInfo)
            : host.validationsInfo) || {};
        const { groups, validationIds } = wizardStepsValidationsMap[wizardStepId].host;
        return (0, exports.checkHostValidationGroups)(validationsInfo, groups, softValidationIds) &&
            (0, exports.checkHostValidations)(validationsInfo, validationIds)
            ? 'known'
            : status;
    }
    return status;
};
exports.getWizardStepHostStatus = getWizardStepHostStatus;
const getWizardStepClusterValidationsInfo = (validationsInfo, wizardStepId, wizardStepsValidationsMap) => {
    const { groups, validationIds } = wizardStepsValidationsMap[wizardStepId].cluster;
    return (0, reduce_js_1.default)(validationsInfo, (result, groupValidations, groupNameStr) => {
        const groupName = groupNameStr;
        if (groups.includes(groupName)) {
            result[groupName] = groupValidations;
            return result;
        }
        const selectedValidations = (groupValidations || []).filter((validation) => validationIds.includes(validation.id));
        if (selectedValidations.length) {
            result[groupName] = selectedValidations;
            return result;
        }
        return result;
    }, {});
};
exports.getWizardStepClusterValidationsInfo = getWizardStepClusterValidationsInfo;
const getWizardStepClusterStatus = (wizardStepId, wizardStepsValidationsMap, cluster, clusterHosts = []) => {
    const { status } = cluster;
    if (['insufficient', 'pending-for-input'].includes(status)) {
        // NOTE(jtomasek): REST API validationsInfo is string, K8s validationsInfo is ClusterValidationsInfo object
        const validationsInfo = (typeof cluster.validationsInfo === 'string'
            ? (0, utils_1.stringToJSON)(cluster.validationsInfo)
            : cluster.validationsInfo) || {};
        const { groups, validationIds } = wizardStepsValidationsMap[wizardStepId].cluster;
        const { softValidationIds } = wizardStepsValidationsMap[wizardStepId];
        const { allowedStatuses } = wizardStepsValidationsMap[wizardStepId].host;
        const allHostsReady = clusterHosts.every((host) => allowedStatuses.length === 0 ||
            allowedStatuses.includes((0, exports.getWizardStepHostStatus)(wizardStepId, wizardStepsValidationsMap, host)));
        return allHostsReady &&
            (0, exports.checkClusterValidationGroups)(validationsInfo, groups, softValidationIds) &&
            (0, exports.checkClusterValidations)(validationsInfo, validationIds)
            ? 'ready'
            : status;
    }
    return status;
};
exports.getWizardStepClusterStatus = getWizardStepClusterStatus;
const getAllClusterWizardSoftValidationIds = (wizardStepsValidationsMap) => Object.keys(wizardStepsValidationsMap).reduce((prev, curr) => {
    const failingStepValidations = wizardStepsValidationsMap[curr];
    return [...prev, ...failingStepValidations.softValidationIds];
}, []);
exports.getAllClusterWizardSoftValidationIds = getAllClusterWizardSoftValidationIds;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const getFailingClusterWizardStepHostValidations = (wizardHostStepValidationsInfo) => Object.keys(wizardHostStepValidationsInfo).reduce((curr, groupStr) => {
    const groupValidations = wizardHostStepValidationsInfo[groupStr];
    if (!groupValidations) {
        return curr;
    }
    const failingValidations = groupValidations.filter((validation) => validation.status === 'failure') || [];
    return [...curr, ...failingValidations];
}, []);
exports.getFailingClusterWizardStepHostValidations = getFailingClusterWizardStepHostValidations;
const areOnlySoftValidationsOfWizardStepFailing = (validationsInfo, wizardStepId, wizardStepsValidationsMap) => {
    const stepValidationsInfo = (0, exports.getWizardStepHostValidationsInfo)(validationsInfo, wizardStepId, wizardStepsValidationsMap);
    const failingValidationIds = (0, exports.getFailingClusterWizardStepHostValidations)(stepValidationsInfo).map((validation) => validation.id);
    if (!failingValidationIds.length)
        return false;
    for (const id of failingValidationIds) {
        if (!wizardStepsValidationsMap[wizardStepId].softValidationIds.includes(id)) {
            return false;
        }
    }
    return true;
};
exports.areOnlySoftValidationsOfWizardStepFailing = areOnlySoftValidationsOfWizardStepFailing;
const findValidationStep = (validationId, wizardStepsValidationsMap, minimumStep) => {
    const wizardStepsIds = (0, keys_js_1.default)(wizardStepsValidationsMap);
    let isBeforeMinimumStep = true;
    return wizardStepsIds.find((wizardStepId) => {
        if (wizardStepId === minimumStep) {
            isBeforeMinimumStep = false;
        }
        if (isBeforeMinimumStep) {
            return false;
        }
        // find first matching validation-map name, ignoring steps before minimumStep
        const { host: hostValidationMap } = wizardStepsValidationsMap[wizardStepId];
        return hostValidationMap.validationIds.includes(validationId);
    });
};
exports.findValidationStep = findValidationStep;
//# sourceMappingURL=validationsInfoUtils.js.map