"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_core_1 = require("@patternfly/react-core");
const info_circle_icon_1 = require("@patternfly/react-icons/dist/js/icons/info-circle-icon");
const react_router_dom_v5_compat_1 = require("react-router-dom-v5-compat");
const common_1 = require("../../../common");
const HostsTable_1 = tslib_1.__importDefault(require("../../../common/components/hosts/HostsTable"));
const tableUtils_1 = require("../Agent/tableUtils");
const constants_1 = require("../common/constants");
const AgentStatus_1 = tslib_1.__importDefault(require("../Agent/AgentStatus"));
const BMHStatus_1 = tslib_1.__importDefault(require("../Agent/BMHStatus"));
const helpers_1 = require("../helpers");
const usePagination_1 = require("../../../common/components/hosts/usePagination");
const use_translation_wrapper_1 = require("../../../common/hooks/use-translation-wrapper");
const agentStatus_1 = require("../helpers/agentStatus");
const hostnameColumn = (agents, t) => {
    return {
        header: {
            title: t('ai:Hostname'),
            props: {
                id: 'col-header-hostname', // ACM jest tests require id over testId
            },
            sort: true,
        },
        cell: (host) => {
            var _a, _b, _c;
            const inventory = (0, common_1.getInventory)(host);
            const computedHostname = (0, common_1.getHostname)(host, inventory);
            const assignedToCluster = (_c = (_b = (_a = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; })) === null || _a === void 0 ? void 0 : _a.spec) === null || _b === void 0 ? void 0 : _b.clusterDeploymentName) === null || _c === void 0 ? void 0 : _c.name;
            return {
                title: (React.createElement("div", { className: assignedToCluster ? 'pf-v6-u-color-200' : undefined }, computedHostname)),
                props: { 'data-testid': 'hostname' },
                sortableValue: computedHostname,
            };
        },
    };
};
const statusColumn = (agents, bmhs, t) => {
    const agentStatuses = (0, agentStatus_1.agentStatus)(t);
    const bmhStatuses = (0, agentStatus_1.bmhStatus)(t);
    return {
        header: {
            title: t('ai:Status'),
            props: {
                id: 'col-header-status',
            },
            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 clusterName = (_a = agent === null || agent === void 0 ? void 0 : agent.spec.clusterDeploymentName) === null || _a === void 0 ? void 0 : _a.name;
            const bmh = bmhs === null || bmhs === void 0 ? void 0 : bmhs.find((b) => { var _a; return ((_a = b.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
            let bmhStatus;
            let title = '--';
            if (agent) {
                title = clusterName ? (React.createElement(react_core_1.Flex, null,
                    React.createElement(react_core_1.FlexItem, null,
                        React.createElement(AgentStatus_1.default, { agent: agent, zIndex: 7000 })),
                    React.createElement(react_core_1.FlexItem, { align: { default: 'alignRight' } },
                        React.createElement(react_core_1.Popover, { "aria-label": t('ai:Cluster popover'), headerContent: React.createElement("div", null, t('ai:Cannot be deleted')), bodyContent: React.createElement("div", null, t('ai:Hosts that are bound to a cluster cannot be deleted. Remove the host from the cluster and try again.')), footerContent: React.createElement(react_router_dom_v5_compat_1.Link, { to: `/multicloud/infrastructure/clusters/details/${clusterName}/` }, t('ai:Go to cluster {{clusterName}}', { clusterName })) },
                            React.createElement(react_core_1.Button, { variant: "link", icon: React.createElement(react_core_1.Icon, { status: "info" },
                                    React.createElement(info_circle_icon_1.InfoCircleIcon, null)) }, t('ai:Cannot be deleted')))))) : (React.createElement(AgentStatus_1.default, { agent: agent }));
            }
            else if (bmh) {
                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
                    ? (0, helpers_1.getAgentStatus)(agent, agentStatuses).status.title
                    : (bmhStatus === null || bmhStatus === void 0 ? void 0 : bmhStatus.state.title) || '',
            };
        },
    };
};
const tableContent = (agents, bareMetalHosts, t) => [hostnameColumn(agents, t), statusColumn(agents, bareMetalHosts, t)];
const MassDeleteAgentModal = ({ isOpen, onClose, onDelete, agents, bmhs, infraEnv, nmStates, }) => {
    const [hosts] = (0, tableUtils_1.useAgentsTable)({ agents, bmhs, infraEnv });
    const onClick = (host) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        var _a, _b, _c, _d;
        const agent = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
        const bmhLabel = (_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[constants_1.AGENT_BMH_NAME_LABEL_KEY];
        let bmh;
        if (!agent) {
            bmh = bmhs.find((bmh) => { var _a; return ((_a = bmh.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
        }
        else if (bmhLabel) {
            bmh = bmhs.find((bmh) => { var _a, _b, _c; return ((_a = bmh === null || bmh === void 0 ? void 0 : bmh.metadata) === null || _a === void 0 ? void 0 : _a.name) === bmhLabel && ((_b = agent.metadata) === null || _b === void 0 ? void 0 : _b.namespace) === ((_c = bmh.metadata) === null || _c === void 0 ? void 0 : _c.namespace); });
        }
        if (!((_d = (_c = agent === null || agent === void 0 ? void 0 : agent.spec) === null || _c === void 0 ? void 0 : _c.clusterDeploymentName) === null || _d === void 0 ? void 0 : _d.name)) {
            return onDelete(agent, bmh, nmStates);
        }
    });
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const content = React.useMemo(() => tableContent(agents, bmhs, t), [agents, bmhs, t]);
    const paginationProps = (0, usePagination_1.usePagination)(hosts.length);
    return (React.createElement(common_1.MassDeleteHostModal, { hosts: hosts, isOpen: isOpen, onClose: onClose, onDelete: onClick },
        React.createElement(HostsTable_1.default, Object.assign({ hosts: hosts, content: content }, paginationProps),
            React.createElement("div", null, t('ai:No hosts selected')))));
};
exports.default = MassDeleteAgentModal;
//# sourceMappingURL=MassDeleteAgentModal.js.map