"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 Agent_1 = require("../../Agent");
const HostsTable_1 = tslib_1.__importStar(require("../../../../common/components/hosts/HostsTable"));
const common_1 = require("../../../../common");
const MassApproveAgentModal_1 = tslib_1.__importDefault(require("../../modals/MassApproveAgentModal"));
const MassApproveAction_1 = tslib_1.__importDefault(require("../../modals/MassApproveAction"));
const usePagination_1 = require("../../../../common/components/hosts/usePagination");
const use_translation_wrapper_1 = require("../../../../common/hooks/use-translation-wrapper");
const AgentsSelectionTable_1 = require("../../Agent/AgentsSelectionTable");
const HostsTableDetailContext_1 = require("../../../../common/components/hosts/HostsTableDetailContext");
const agentStatus_1 = require("../../helpers/agentStatus");
const helpers_1 = require("../../helpers");
const ClusterDeploymentHostDiscoveryTable = ({ agents, bareMetalHosts, infraEnv, onApprove, onChangeHostname, onChangeBMHHostname, onEditRole, onSetInstallationDiskId, onEditHost, onEditBMH, onDeleteHost, width, }) => {
    const [isDiscoveryHintModalOpen, setDiscoveryHintModalOpen] = React.useState(false);
    const [isMassChangeHostOpen, setMassChangeHostOpen] = React.useState(false);
    const [isMassApproveOpen, setMassApproveOpen] = React.useState(false);
    const [selectedHostIDs, setSelectedHostIDs] = React.useState([]);
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const agentStatuses = (0, agentStatus_1.agentStatus)(t);
    const bmhStatuses = (0, agentStatus_1.bmhStatus)(t);
    const onSelect = (host, isSelected) => {
        if (isSelected) {
            setSelectedHostIDs([...selectedHostIDs, host.id]);
        }
        else {
            setSelectedHostIDs(selectedHostIDs.filter((sa) => sa !== host.id));
        }
    };
    const [hosts, hostActions, actionResolver] = (0, Agent_1.useAgentsTable)({
        agents,
        bmhs: bareMetalHosts,
        infraEnv,
    }, { onEditHost, onEditRole, onEditBMH, onDeleteHost, onSetInstallationDiskId });
    const addAll = width && width > 700;
    const content = React.useMemo(() => [
        (0, common_1.hostnameColumn)(t, hostActions.onEditHost),
        ...(addAll ? [(0, Agent_1.discoveryTypeColumn)(agents, bareMetalHosts, t)] : []),
        (0, Agent_1.agentStatusColumn)({
            agents,
            agentStatuses,
            bareMetalHosts,
            bmhStatuses,
            onEditHostname: onEditHost,
            onApprove,
            wizardStepId: 'hosts-discovery',
            t,
        }),
        (0, common_1.roleColumn)(t, hostActions.canEditRole, hostActions.onEditRole),
        ...(addAll
            ? [
                (0, common_1.discoveredAtColumn)(t),
                (0, common_1.cpuCoresColumn)(t),
                (0, common_1.gpusColumn)(t),
                (0, common_1.memoryColumn)(t),
                (0, common_1.disksColumn)(t),
                (0, common_1.labelsColumn)(t),
            ]
            : []),
    ], [
        t,
        hostActions.onEditHost,
        hostActions.canEditRole,
        hostActions.onEditRole,
        addAll,
        agents,
        bareMetalHosts,
        agentStatuses,
        bmhStatuses,
        onEditHost,
        onApprove,
    ]);
    const selectedAgents = agents.filter((a) => { var _a; return selectedHostIDs.includes(((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) || ''); });
    const massActions = [
        React.createElement(common_1.ChangeHostnameAction, { key: "hostname", onChangeHostname: () => setMassChangeHostOpen(!isMassChangeHostOpen) }),
        React.createElement(MassApproveAction_1.default, { key: "approve", onApprove: () => setMassApproveOpen(!isMassApproveOpen), selectedAgents: selectedAgents }),
    ];
    const paginationProps = (0, usePagination_1.usePagination)(hosts.length);
    const itemIDs = hosts.map((h) => h.id);
    return (React.createElement(React.Fragment, null,
        React.createElement(react_core_1.Stack, { hasGutter: true },
            React.createElement(react_core_1.StackItem, null,
                React.createElement(common_1.TableToolbar, Object.assign({ selectedIDs: selectedHostIDs, itemIDs: itemIDs, setSelectedIDs: setSelectedHostIDs, actions: massActions }, paginationProps))),
            React.createElement(react_core_1.StackItem, null,
                React.createElement(HostsTableDetailContext_1.HostsTableDetailContextProvider, { canEditDisks: hostActions.canEditDisks, onDiskRole: hostActions.onDiskRole },
                    React.createElement(HostsTable_1.default, Object.assign({ hosts: hosts, content: content, actionResolver: actionResolver, selectedIDs: selectedHostIDs, setSelectedIDs: setSelectedHostIDs, onSelect: onSelect, ExpandComponent: hostActions.onDiskRole ? AgentsSelectionTable_1.ExpandComponent : HostsTable_1.DefaultExpandComponent }, paginationProps),
                        React.createElement(HostsTable_1.HostsTableEmptyState, { setDiscoveryHintModalOpen: setDiscoveryHintModalOpen }))))),
        isDiscoveryHintModalOpen && (React.createElement(common_1.DiscoveryTroubleshootingModal, { isOpen: isDiscoveryHintModalOpen, setDiscoveryHintModalOpen: setDiscoveryHintModalOpen })),
        isMassChangeHostOpen && (React.createElement(common_1.MassChangeHostnameModal, { isOpen: isMassChangeHostOpen, hosts: hosts, selectedHostIDs: selectedHostIDs, onChangeHostname: (0, helpers_1.onAgentChangeHostname)(agents, bareMetalHosts, onChangeHostname, onChangeBMHHostname), onClose: () => setMassChangeHostOpen(false), canChangeHostname: (0, Agent_1.canChangeHostname)(agents, agentStatuses, bareMetalHosts, t) })),
        isMassApproveOpen && (React.createElement(MassApproveAgentModal_1.default, { isOpen: isMassApproveOpen, agents: selectedAgents, onApprove: onApprove, onClose: () => setMassApproveOpen(false) }))));
};
exports.default = ClusterDeploymentHostDiscoveryTable;
//# sourceMappingURL=ClusterDeploymentHostDiscoveryTable.js.map