"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBMHStatus = exports.getBMHStatusKey = exports.getWizardStepAgentStatus = exports.getAgentStatus = exports.getAgentStatusKey = exports.isDraft = exports.getClusterStatus = void 0;
const validationsInfoUtils_1 = require("../../../common/components/clusterWizard/validationsInfoUtils");
const wizardTransition_1 = require("../ClusterDeployment/wizardTransition");
const agentStatus_1 = require("./agentStatus");
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const conditionsByTypeReducer = (result, condition) => (Object.assign(Object.assign({}, result), { [condition.type]: condition }));
const getClusterStatus = (agentClusterInstall) => {
    var _a;
    const { state: status, stateInfo: statusInfo } = ((_a = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.status) === null || _a === void 0 ? void 0 : _a.debugInfo) || {};
    return [status || 'insufficient', statusInfo || ''];
};
exports.getClusterStatus = getClusterStatus;
const isDraft = (agentClusterInstall) => !!agentClusterInstall &&
    ['pending-for-input', 'insufficient', 'ready'].includes((0, exports.getClusterStatus)(agentClusterInstall)[0]);
exports.isDraft = isDraft;
const getInsufficientState = (agent) => { var _a, _b; return ((_b = (_a = agent === null || agent === void 0 ? void 0 : agent.spec) === null || _a === void 0 ? void 0 : _a.clusterDeploymentName) === null || _b === void 0 ? void 0 : _b.name) ? 'insufficient' : 'insufficient-unbound'; };
const getAgentStatusKey = (agent, excludeDiscovered = false) => {
    var _a, _b, _c, _d;
    const specSyncErr = (_b = (_a = agent.status) === null || _a === void 0 ? void 0 : _a.conditions) === null || _b === void 0 ? void 0 : _b.find((c) => c.type === 'SpecSynced' && c.status === 'False');
    if (specSyncErr) {
        return 'specSyncErr';
    }
    else {
        if (!excludeDiscovered && !agent.spec.approved) {
            return 'discovered';
        }
        else {
            return ((_d = (_c = agent.status) === null || _c === void 0 ? void 0 : _c.debugInfo) === null || _d === void 0 ? void 0 : _d.state) || getInsufficientState(agent);
        }
    }
};
exports.getAgentStatusKey = getAgentStatusKey;
const getAgentStatus = (agent, agentStatuses, excludeDiscovered = false) => {
    var _a;
    const key = (0, exports.getAgentStatusKey)(agent, excludeDiscovered);
    let validationsInfo = {};
    const status = agentStatuses[key];
    if (key !== 'specSyncErr') {
        validationsInfo = ((_a = agent.status) === null || _a === void 0 ? void 0 : _a.validationsInfo) || {};
    }
    // TODO(jtomasek): Implement this
    // const sublabel = areOnlyClusterSoftValidationsFailing(agentStatus.validationsInfo)
    //   ? 'Some validations failed'
    //   : undefined;
    return { status, validationsInfo };
};
exports.getAgentStatus = getAgentStatus;
const getWizardStepAgentStatus = (agent, wizardStepId, t, excludeDiscovered = false) => {
    const agentStatuses = (0, agentStatus_1.agentStatus)(t);
    const aStatus = (0, exports.getAgentStatus)(agent, agentStatuses, excludeDiscovered);
    if ([agentStatuses.discovered, agentStatuses.specSyncErr].includes(aStatus.status)) {
        return aStatus;
    }
    const status = agentStatuses[(0, validationsInfoUtils_1.getWizardStepHostStatus)(wizardStepId, wizardTransition_1.wizardStepsValidationsMap, {
        status: aStatus.status.key,
        validationsInfo: aStatus.validationsInfo,
    })];
    const validationsInfo = (0, validationsInfoUtils_1.getWizardStepHostValidationsInfo)(aStatus.validationsInfo, wizardStepId, wizardTransition_1.wizardStepsValidationsMap);
    status.sublabel = (0, validationsInfoUtils_1.areOnlySoftValidationsOfWizardStepFailing)(aStatus.validationsInfo, wizardStepId, wizardTransition_1.wizardStepsValidationsMap)
        ? t('ai:Some validations failed')
        : undefined;
    return {
        status,
        validationsInfo,
    };
};
exports.getWizardStepAgentStatus = getWizardStepAgentStatus;
const getBMHStatusKey = (bmh) => {
    var _a, _b, _c, _d, _e;
    if ((_a = bmh.status) === null || _a === void 0 ? void 0 : _a.errorType) {
        return 'bmh-error';
    }
    else if ((_c = (_b = bmh.status) === null || _b === void 0 ? void 0 : _b.provisioning) === null || _c === void 0 ? void 0 : _c.state) {
        return (_e = (_d = bmh.status) === null || _d === void 0 ? void 0 : _d.provisioning) === null || _e === void 0 ? void 0 : _e.state;
    }
};
exports.getBMHStatusKey = getBMHStatusKey;
const getBMHStatus = (bmh, bmhStatuses) => {
    var _a;
    let bmhState = bmhStatuses.pending;
    const statusKey = (0, exports.getBMHStatusKey)(bmh);
    if (statusKey) {
        bmhState = bmhStatuses[statusKey] || bmhState;
    }
    return {
        state: bmhState,
        errorMessage: (_a = bmh.status) === null || _a === void 0 ? void 0 : _a.errorMessage,
    };
};
exports.getBMHStatus = getBMHStatus;
//# sourceMappingURL=status.js.map