"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 helpers_1 = require("../helpers");
const HostsTable_1 = tslib_1.__importDefault(require("../../../common/components/hosts/HostsTable"));
const common_1 = require("../../../common");
const agentStatus_1 = require("../helpers/agentStatus");
const usePagination_1 = require("../../../common/components/hosts/usePagination");
const utils_1 = require("../../../common/utils");
const use_translation_wrapper_1 = require("../../../common/hooks/use-translation-wrapper");
const ApproveTableRow = ({ agent, children, }) => React.createElement("div", { className: (agent === null || agent === void 0 ? void 0 : agent.spec.approved) ? 'pf-v6-u-color-200' : undefined }, children);
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 agent = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
            const hostname = ((_a = agent === null || agent === void 0 ? void 0 : agent.spec) === null || _a === void 0 ? void 0 : _a.hostname) || ((_c = (_b = agent === null || agent === void 0 ? void 0 : agent.status) === null || _b === void 0 ? void 0 : _b.inventory) === null || _c === void 0 ? void 0 : _c.hostname);
            return {
                title: React.createElement(ApproveTableRow, { agent: agent }, hostname),
                props: { 'data-testid': 'hostname' },
                sortableValue: hostname,
            };
        },
    };
};
const statusColumn = (agents, agentStatuses, t) => {
    return {
        header: {
            title: t('ai:Status'),
            props: {
                id: 'col-header-status', // ACM jest tests require id over testId
            },
            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 status = (agent === null || agent === void 0 ? void 0 : agent.spec.approved)
                ? t('ai:Already approved')
                : agentStatuses.discovered.title;
            const icon = agentStatuses.discovered.icon;
            return {
                title: (React.createElement(ApproveTableRow, { agent: agent }, (agent === null || agent === void 0 ? void 0 : agent.spec.approved) ? (status) : (React.createElement(React.Fragment, null,
                    icon,
                    " ",
                    status)))),
                props: { 'data-testid': 'status' },
                sortableValue: status,
            };
        },
    };
};
const MassApproveAgentModal = ({ agents, onApprove, isOpen, onClose, }) => {
    const [progress, setProgress] = React.useState(null);
    const [error, setError] = React.useState();
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const agentStatuses = (0, agentStatus_1.agentStatus)(t);
    const onClick = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        setError(undefined);
        let i = 0;
        try {
            for (const agent of agents) {
                if (!agent.spec.approved) {
                    setProgress((100 * (i + 1)) / agents.length);
                    yield onApprove(agent);
                }
                i++;
            }
            setProgress(null);
            onClose();
        }
        catch (err) {
            setError({
                title: t('ai:An error occurred while approving agents'),
                message: (0, utils_1.getErrorMessage)(err),
            });
            setProgress(null);
        }
    });
    const { content, hosts } = React.useMemo(() => ({
        content: [hostnameColumn(agents, t), statusColumn(agents, agentStatuses, t)],
        hosts: (0, helpers_1.getAIHosts)(agents),
    }), [agentStatuses, agents, t]);
    const paginationProps = (0, usePagination_1.usePagination)(hosts.length);
    return (React.createElement(react_core_1.Modal, { "aria-label": t('ai:Approve hosts dialog'), isOpen: isOpen, onClose: onClose, id: "mass-approve-modal", variant: "medium" },
        React.createElement(react_core_1.ModalHeader, { title: t('ai:Approve hosts to join infrastructure environment') }),
        React.createElement(react_core_1.ModalBody, null,
            React.createElement(react_core_1.Stack, { hasGutter: true },
                React.createElement(react_core_1.StackItem, null,
                    React.createElement(react_core_1.Alert, { isInline: true, variant: "info", title: t('ai:You are approving multiple hosts. All hosts listed below will be approved to join the infrastructure environment if you continue. Make sure that you expect and recognize the hosts before approving.') })),
                React.createElement(react_core_1.StackItem, null,
                    React.createElement(HostsTable_1.default, Object.assign({ hosts: hosts, content: content }, paginationProps),
                        React.createElement("div", null, t('ai:No hosts selected')))),
                React.createElement(react_core_1.StackItem, null,
                    React.createElement(common_1.ModalProgress, { error: error, progress: progress })))),
        React.createElement(react_core_1.ModalFooter, null,
            React.createElement(react_core_1.Button, { onClick: onClick, isDisabled: progress !== null }, t('ai:Approve all hosts')),
            React.createElement(react_core_1.Button, { onClick: onClose, variant: react_core_1.ButtonVariant.secondary, isDisabled: progress !== null }, t('ai:Cancel')))));
};
exports.default = MassApproveAgentModal;
//# sourceMappingURL=MassApproveAgentModal.js.map