"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.infraEnvColumn = exports.clusterColumn = exports.agentStatusColumn = exports.discoveryTypeColumn = exports.agentHostnameColumn = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_router_dom_v5_compat_1 = require("react-router-dom-v5-compat");
const common_1 = require("../../../common");
const common_2 = require("../common");
const helpers_1 = require("../helpers");
const AgentStatus_1 = tslib_1.__importDefault(require("./AgentStatus"));
const BMHStatus_1 = tslib_1.__importDefault(require("./BMHStatus"));
const BMHStatusInfo_1 = require("./BMHStatusInfo");
const agentHostnameColumn = (t, hosts, agents, bareMetalHosts, onEditHostname, canEditHostname, canEditBMH) => ({
    header: {
        title: t('ai:Hostname'),
        props: {
            id: 'col-header-hostname', // ACM jest tests require id over testId
            modifier: 'breakWord',
        },
        sort: true,
    },
    cell: (host) => {
        const inventory = (0, common_1.getInventory)(host);
        const editHostname = onEditHostname ? () => onEditHostname(host) : undefined;
        const computedHostname = (0, common_1.getHostname)(host, inventory);
        let readonly = true;
        const agent = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
        if (agent) {
            readonly = canEditHostname ? !canEditHostname(host) : false;
        }
        else {
            const bmh = bareMetalHosts.find((bmh) => { var _a; return ((_a = bmh.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
            if (bmh && canEditBMH) {
                readonly = !canEditBMH(host);
            }
        }
        return {
            title: (React.createElement(common_1.Hostname, { host: host, inventory: inventory, onEditHostname: editHostname, hosts: hosts, readonly: readonly })),
            props: { 'data-testid': 'host-name' },
            sortableValue: computedHostname || '',
        };
    },
});
exports.agentHostnameColumn = agentHostnameColumn;
const discoveryTypeColumn = (agents, bareMetalHosts, t) => {
    return {
        header: {
            title: t('ai:Discovery type'),
            props: {
                id: 'col-header-discovery-type',
            },
            sort: true,
        },
        cell: (host) => {
            var _a, _b;
            const agent = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
            let discoveryType = t('ai:Unknown');
            if (agent) {
                // eslint-disable-next-line no-prototype-builtins
                discoveryType = ((_b = (_a = agent === null || agent === void 0 ? void 0 : agent.metadata) === null || _a === void 0 ? void 0 : _a.labels) === null || _b === void 0 ? void 0 : _b.hasOwnProperty(common_2.AGENT_BMH_NAME_LABEL_KEY))
                    ? t('ai:BMC')
                    : t('ai:Discovery ISO');
            }
            else {
                const bmh = bareMetalHosts.find((bmh) => { var _a; return ((_a = bmh.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
                if (bmh) {
                    discoveryType = t('ai:BMC');
                }
            }
            return {
                title: discoveryType,
                props: { 'data-testid': 'discovery-type' },
                sortableValue: discoveryType,
            };
        },
    };
};
exports.discoveryTypeColumn = discoveryTypeColumn;
const agentStatusColumn = ({ agents, agentStatuses, bareMetalHosts, bmhStatuses, onEditHostname, onApprove, wizardStepId, t, isDay2, }) => {
    return {
        header: {
            title: t('ai:Status'),
            props: {
                id: 'col-header-infraenvstatus',
            },
            sort: true,
        },
        cell: (host) => {
            const agent = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
            const bmh = bareMetalHosts === null || bareMetalHosts === void 0 ? void 0 : bareMetalHosts.find((b) => {
                var _a, _b;
                return ((_b = (_a = b.metadata) === null || _a === void 0 ? void 0 : _a.annotations) === null || _b === void 0 ? void 0 : _b['bmac.agent-install.openshift.io/hostname']) ===
                    host.requestedHostname;
            });
            let bmhStatus;
            let title = '--';
            if (agent) {
                const editHostname = onEditHostname ? () => onEditHostname(agent) : undefined;
                title = (React.createElement(AgentStatus_1.default, { agent: agent, onApprove: onApprove, onEditHostname: editHostname, wizardStepId: wizardStepId, isDay2: isDay2, additionalBMHInfo: bmh &&
                        bmhStatuses && React.createElement(BMHStatusInfo_1.BMHStatusInfo, { bmhStatus: (0, helpers_1.getBMHStatus)(bmh, bmhStatuses), bmh: bmh }) }));
            }
            else if (bmh && bmhStatuses) {
                bmhStatus = (0, helpers_1.getBMHStatus)(bmh, bmhStatuses);
                title = React.createElement(BMHStatus_1.default, { bmhStatus: bmhStatus, bmh: bmh });
            }
            return {
                title,
                props: { 'data-testid': 'host-status' },
                sortableValue: agent
                    ? wizardStepId
                        ? (0, helpers_1.getWizardStepAgentStatus)(agent, wizardStepId, t).status.title
                        : (0, helpers_1.getAgentStatus)(agent, agentStatuses).status.title
                    : (bmhStatus === null || bmhStatus === void 0 ? void 0 : bmhStatus.state.title) || '',
            };
        },
    };
};
exports.agentStatusColumn = agentStatusColumn;
const clusterColumn = (agents, agentMachines, getClusterDeploymentLink, t) => {
    return {
        header: {
            title: t('ai:Cluster'),
            props: {
                id: 'col-header-cluster',
            },
            sort: true,
        },
        cell: (host) => {
            var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
            const agent = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
            let cdLink = undefined;
            if ((_a = agent === null || agent === void 0 ? void 0 : agent.spec) === null || _a === void 0 ? void 0 : _a.clusterDeploymentName) {
                //hypershift
                const amNamespace = (_c = (_b = agent.metadata) === null || _b === void 0 ? void 0 : _b.annotations) === null || _c === void 0 ? void 0 : _c['agentMachineRefNamespace'];
                if (amNamespace) {
                    const am = agentMachines.find((am) => {
                        var _a, _b, _c, _d, _e, _f;
                        return ((_b = (_a = am.status) === null || _a === void 0 ? void 0 : _a.agentRef) === null || _b === void 0 ? void 0 : _b.name) === ((_c = agent.metadata) === null || _c === void 0 ? void 0 : _c.name) &&
                            ((_e = (_d = am.status) === null || _d === void 0 ? void 0 : _d.agentRef) === null || _e === void 0 ? void 0 : _e.namespace) === ((_f = agent.metadata) === null || _f === void 0 ? void 0 : _f.namespace);
                    });
                    const nodePool = (_e = (_d = am === null || am === void 0 ? void 0 : am.metadata) === null || _d === void 0 ? void 0 : _d.annotations) === null || _e === void 0 ? void 0 : _e['hypershift.openshift.io/nodePool'];
                    if (nodePool) {
                        const hcNamespace = nodePool.split('/')[0];
                        const hcName = agent.spec.clusterDeploymentName.name;
                        cdLink = getClusterDeploymentLink({ name: hcName, namespace: hcNamespace });
                    }
                }
                else {
                    cdLink = getClusterDeploymentLink(agent.spec.clusterDeploymentName);
                }
            }
            return {
                title: cdLink ? React.createElement(react_router_dom_v5_compat_1.Link, { to: cdLink }, (_g = (_f = agent === null || agent === void 0 ? void 0 : agent.spec) === null || _f === void 0 ? void 0 : _f.clusterDeploymentName) === null || _g === void 0 ? void 0 : _g.name) : '--',
                props: { 'data-testid': 'cluster' },
                sortableValue: (_k = (_j = (_h = agent === null || agent === void 0 ? void 0 : agent.spec) === null || _h === void 0 ? void 0 : _h.clusterDeploymentName) === null || _j === void 0 ? void 0 : _j.name) !== null && _k !== void 0 ? _k : '--',
            };
        },
    };
};
exports.clusterColumn = clusterColumn;
const infraEnvColumn = (agents, t) => {
    return {
        header: {
            title: t('ai:Infrastructure env'),
            props: {
                id: 'col-header-infraenv',
            },
            sort: true,
        },
        cell: (host) => {
            var _a;
            const agent = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
            const infraEnvName = (0, helpers_1.getInfraEnvNameOfAgent)(agent);
            const title = infraEnvName ? (React.createElement(react_router_dom_v5_compat_1.Link, { to: `/multicloud/infrastructure/environments/details/${((_a = agent === null || agent === void 0 ? void 0 : agent.metadata) === null || _a === void 0 ? void 0 : _a.namespace) || ''}/${infraEnvName}/overview` }, infraEnvName)) : ('--');
            return {
                title,
                props: { 'data-testid': 'infra-env' },
                sortableValue: infraEnvName,
            };
        },
    };
};
exports.infraEnvColumn = infraEnvColumn;
//# sourceMappingURL=tableColumns.js.map