"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getGridSpans = exports.getAgentsHostsNames = exports.isAgentOfCluster = exports.isAgentOfInfraEnv = exports.isCIMFlow = exports.getOnFetchEventsHandler = exports.formatEventsData = exports.shouldShowClusterInstallationError = exports.isInstallationReady = exports.isInstallationInProgress = exports.shouldShowClusterInstallationProgress = exports.shouldShowClusterCredentials = exports.shouldShowClusterDeploymentValidationOverview = void 0;
const tslib_1 = require("tslib");
const mapKeys_js_1 = tslib_1.__importDefault(require("lodash-es/mapKeys.js"));
const camelCase_js_1 = tslib_1.__importDefault(require("lodash-es/camelCase.js"));
const uniq_js_1 = tslib_1.__importDefault(require("lodash-es/uniq.js"));
const status_1 = require("../helpers/status");
const proxy_1 = require("../helpers/proxy");
const agents_1 = require("../helpers/agents");
const constants_1 = require("../common/constants");
const utils_1 = require("../../../common/utils");
const shouldShowClusterDeploymentValidationOverview = (agentClusterInstall) => {
    var _a, _b;
    const [clusterStatus] = (0, status_1.getClusterStatus)(agentClusterInstall);
    return (((_a = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.status) === null || _a === void 0 ? void 0 : _a.validationsInfo) &&
        (['insufficient', 'pending-for-input'].includes(clusterStatus) ||
            (clusterStatus === 'ready' && ((_b = agentClusterInstall.spec) === null || _b === void 0 ? void 0 : _b.holdInstallation))));
};
exports.shouldShowClusterDeploymentValidationOverview = shouldShowClusterDeploymentValidationOverview;
const shouldShowClusterCredentials = (agentClusterInstall) => {
    const [clusterStatus] = (0, status_1.getClusterStatus)(agentClusterInstall);
    return ['installed', 'adding-hosts'].includes(clusterStatus);
};
exports.shouldShowClusterCredentials = shouldShowClusterCredentials;
const shouldShowClusterInstallationProgress = (agentClusterInstall) => {
    var _a;
    const [clusterStatus] = (0, status_1.getClusterStatus)(agentClusterInstall);
    return ([
        'preparing-for-installation',
        'installing',
        'installing-pending-user-action',
        'finalizing',
        'installed',
        'error',
        'cancelled',
        'adding-hosts',
        'ready',
    ].includes(clusterStatus) && !((_a = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _a === void 0 ? void 0 : _a.holdInstallation));
};
exports.shouldShowClusterInstallationProgress = shouldShowClusterInstallationProgress;
const isInstallationInProgress = (agentClusterInstall) => {
    var _a;
    const [clusterStatus] = (0, status_1.getClusterStatus)(agentClusterInstall);
    if (!((_a = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _a === void 0 ? void 0 : _a.holdInstallation) &&
        ['insufficient', 'ready'].includes(clusterStatus)) {
        // special initial state
        return true;
    }
    return [
        'preparing-for-installation',
        'installing',
        'finalizing',
        'installing-pending-user-action',
    ].includes(clusterStatus);
};
exports.isInstallationInProgress = isInstallationInProgress;
const isInstallationReady = (agentClusterInstall) => {
    const [clusterStatus] = (0, status_1.getClusterStatus)(agentClusterInstall);
    return [
        'ready',
        'error',
        'preparing-for-installation',
        'installing',
        'finalizing',
        'installed',
        'adding-hosts',
        'cancelled',
        'installing-pending-user-action',
    ].includes(clusterStatus);
};
exports.isInstallationReady = isInstallationReady;
const shouldShowClusterInstallationError = (agentClusterInstall) => {
    const [clusterStatus] = (0, status_1.getClusterStatus)(agentClusterInstall);
    return ['error', 'cancelled'].includes(clusterStatus);
};
exports.shouldShowClusterInstallationError = shouldShowClusterInstallationError;
/* eslint-disable */
const formatEventsData = (rawData) => rawData.map((event) => (0, mapKeys_js_1.default)(event, (value, key) => (0, camelCase_js_1.default)(key)));
exports.formatEventsData = formatEventsData;
/* eslint-enable */
// events are downloaded using ACM's wrapped fetchGet(), so the backendUrl is missing here
const getEventsURL = (aiNamespace, agentClusterInstall) => {
    var _a, _b, _c, _d;
    if ((_b = (_a = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.status) === null || _a === void 0 ? void 0 : _a.debugInfo) === null || _b === void 0 ? void 0 : _b.eventsURL) {
        const eventsURL = new URL((_d = (_c = agentClusterInstall.status) === null || _c === void 0 ? void 0 : _c.debugInfo) === null || _d === void 0 ? void 0 : _d.eventsURL);
        return `${(0, proxy_1.getK8sProxyURL)(aiNamespace)}${eventsURL.pathname}${eventsURL.search}`;
    }
    return null;
};
const getOnFetchEventsHandler = (fetchEvents, aiNamespace, agents, agentClusterInstall) => (_a, onSuccess_1, onError_1) => tslib_1.__awaiter(void 0, [_a, onSuccess_1, onError_1], void 0, function* ({ message, severities: severitiesFilter, hostIds, clusterLevel, deletedHosts }, onSuccess, onError) {
    const eventsURL = getEventsURL(aiNamespace, agentClusterInstall);
    if (!eventsURL) {
        onError('Cannot determine events URL');
        return;
    }
    try {
        const result = yield fetchEvents(eventsURL);
        let data = (0, exports.formatEventsData)(result);
        const allNames = data.filter((d) => d.hostId).map((d) => d.hostId);
        const severities = {
            critical: data.filter((d) => d.severity === 'critical').length,
            error: data.filter((d) => d.severity === 'error').length,
            info: data.filter((d) => d.severity === 'info').length,
            warning: data.filter((d) => d.severity === 'warning').length,
        };
        if (message) {
            data = data.filter((d) => d.message.includes(message));
        }
        if (severitiesFilter && severitiesFilter.length > 0) {
            data = data.filter((d) => severitiesFilter.includes(d.severity));
        }
        if (clusterLevel || deletedHosts || (hostIds && hostIds.length > 0)) {
            data = data.filter((d) => {
                if (clusterLevel && d.clusterId && !d.hostId) {
                    return d;
                }
                if (hostIds && hostIds.length > 0) {
                    const names = agents
                        .filter((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) && hostIds.includes(a.metadata.uid); })
                        .map((a) => { var _a; return (_a = a.metadata) === null || _a === void 0 ? void 0 : _a.name; });
                    if (d.hostId && names.includes(d.hostId)) {
                        return d;
                    }
                }
                if (deletedHosts) {
                    const deleted = allNames.filter((n) => !agents.some((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.name) === n; }));
                    if (d.hostId && deleted.includes(d.hostId)) {
                        return d;
                    }
                }
                return false;
            });
        }
        onSuccess({
            data,
            totalEvents: data.length,
            severities,
        });
    }
    catch (e) {
        onError((0, utils_1.getErrorMessage)(e));
    }
});
exports.getOnFetchEventsHandler = getOnFetchEventsHandler;
const isCIMFlow = (clusterDeployment) => {
    var _a, _b, _c, _d, _e;
    return !((_e = (_d = (_c = (_b = (_a = clusterDeployment === null || clusterDeployment === void 0 ? void 0 : clusterDeployment.spec) === null || _a === void 0 ? void 0 : _a.platform) === null || _b === void 0 ? void 0 : _b.agentBareMetal) === null || _c === void 0 ? void 0 : _c.agentSelector) === null || _d === void 0 ? void 0 : _d.matchLabels) === null || _e === void 0 ? void 0 : _e[constants_1.INFRAENV_GENERATED_AI_FLOW]);
};
exports.isCIMFlow = isCIMFlow;
const isAgentOfInfraEnv = (infraEnv, agent) => {
    var _a, _b, _c;
    return (0, agents_1.getInfraEnvNameOfAgent)(agent) === ((_a = infraEnv === null || infraEnv === void 0 ? void 0 : infraEnv.metadata) === null || _a === void 0 ? void 0 : _a.name) &&
        ((_b = agent === null || agent === void 0 ? void 0 : agent.metadata) === null || _b === void 0 ? void 0 : _b.namespace) === ((_c = infraEnv === null || infraEnv === void 0 ? void 0 : infraEnv.metadata) === null || _c === void 0 ? void 0 : _c.namespace);
};
exports.isAgentOfInfraEnv = isAgentOfInfraEnv;
const isAgentOfCluster = (agent, cdName, cdNamespace) => {
    var _a, _b, _c, _d;
    return ((_b = (_a = agent.spec) === null || _a === void 0 ? void 0 : _a.clusterDeploymentName) === null || _b === void 0 ? void 0 : _b.name) === cdName &&
        ((_d = (_c = agent.spec) === null || _c === void 0 ? void 0 : _c.clusterDeploymentName) === null || _d === void 0 ? void 0 : _d.namespace) === cdNamespace;
};
exports.isAgentOfCluster = isAgentOfCluster;
const getAgentsHostsNames = (agents = [], bmhs = []) => {
    const raw = agents.map((agent) => { var _a, _b, _c; return ((_a = agent.spec) === null || _a === void 0 ? void 0 : _a.hostname) || ((_c = (_b = agent.status) === null || _b === void 0 ? void 0 : _b.inventory) === null || _c === void 0 ? void 0 : _c.hostname); });
    bmhs.forEach((bmh) => {
        var _a, _b;
        const hostname = (_b = (_a = bmh.metadata) === null || _a === void 0 ? void 0 : _a.annotations) === null || _b === void 0 ? void 0 : _b[constants_1.BMH_HOSTNAME_ANNOTATION];
        if (hostname) {
            raw.push(hostname);
        }
    });
    return (0, uniq_js_1.default)(raw.filter(Boolean));
};
exports.getAgentsHostsNames = getAgentsHostsNames;
const getGridSpans = (isPreviewOpen) => isPreviewOpen
    ? {
        span: 12,
    }
    : {
        span: 12,
        lg: 10,
        xl: 9,
        xl2: 7,
    };
exports.getGridSpans = getGridSpans;
//# sourceMappingURL=helpers.js.map