"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const formik_1 = require("formik");
const react_core_1 = require("@patternfly/react-core");
const common_1 = require("../../../../common");
const ClusterDeploymentHostsSelectionBasic_1 = tslib_1.__importDefault(require("./ClusterDeploymentHostsSelectionBasic"));
const ClusterDeploymentHostsSelectionAdvanced_1 = require("./ClusterDeploymentHostsSelectionAdvanced");
const helpers_1 = require("../../helpers");
const MinimalHWRequirements_1 = tslib_1.__importDefault(require("../../Agent/MinimalHWRequirements"));
const NoAgentsAlert_1 = tslib_1.__importDefault(require("../../Agent/NoAgentsAlert"));
const use_translation_wrapper_1 = require("../../../../common/hooks/use-translation-wrapper");
const getHostRequirementText = (isSNO, isTNA, t) => {
    if (isSNO) {
        return t('ai:Exactly 1 host is required, capable of functioning both as control plane and worker node.');
    }
    else if (isTNA) {
        return t('ai:Exactly 2 hosts capable of functioning as control plane nodes, and one arbiter, are required.');
    }
    return t('ai:At least 3 hosts are required, capable of functioning as control plane nodes.');
};
const ClusterDeploymentHostsSelection = ({ agentClusterInstall, clusterDeployment, agents, aiConfigMap, onEditRole, onAutoSelectChange, onHostSelect, onSetInstallationDiskId, isNutanix, hostsBinding, }) => {
    var _a, _b;
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const { values } = (0, formik_1.useFormikContext)();
    const { autoSelectHosts } = values;
    const isSNOCluster = (0, helpers_1.getIsSNOCluster)(agentClusterInstall);
    const isTNA = (0, helpers_1.getIsTNACluster)(agentClusterInstall);
    const cdName = (_a = clusterDeployment === null || clusterDeployment === void 0 ? void 0 : clusterDeployment.metadata) === null || _a === void 0 ? void 0 : _a.name;
    const cdNamespace = (_b = clusterDeployment === null || clusterDeployment === void 0 ? void 0 : clusterDeployment.metadata) === null || _b === void 0 ? void 0 : _b.namespace;
    const cpuArchitecture = (0, helpers_1.getClusterDeploymentCpuArchitecture)(clusterDeployment);
    const availableAgents = react_1.default.useMemo(() => {
        const filtered = (0, helpers_1.getAgentsForSelection)(agents, hostsBinding).filter((agent) => {
            var _a, _b, _c, _d, _e, _f;
            return (((_a = agent.spec.clusterDeploymentName) === null || _a === void 0 ? void 0 : _a.name) === cdName &&
                ((_b = agent.spec.clusterDeploymentName) === null || _b === void 0 ? void 0 : _b.namespace) === cdNamespace) ||
                (!((_c = agent.spec.clusterDeploymentName) === null || _c === void 0 ? void 0 : _c.name) &&
                    !((_d = agent.spec.clusterDeploymentName) === null || _d === void 0 ? void 0 : _d.namespace) &&
                    ((_f = (_e = agent.status) === null || _e === void 0 ? void 0 : _e.inventory.cpu) === null || _f === void 0 ? void 0 : _f.architecture) === cpuArchitecture);
        });
        return isNutanix
            ? filtered.filter((a) => { var _a, _b, _c; return ((_c = (_b = (_a = a.status) === null || _a === void 0 ? void 0 : _a.inventory) === null || _b === void 0 ? void 0 : _b.systemVendor) === null || _c === void 0 ? void 0 : _c.manufacturer) === 'Nutanix'; })
            : filtered;
    }, [agents, hostsBinding, isNutanix, cdName, cdNamespace, cpuArchitecture]);
    const hostRequirementText = getHostRequirementText(isSNOCluster, isTNA, t);
    return (react_1.default.createElement(react_core_1.Grid, { hasGutter: true },
        react_1.default.createElement(react_core_1.GridItem, null,
            react_1.default.createElement(react_core_1.Content, null, hostRequirementText)),
        aiConfigMap && (react_1.default.createElement(react_core_1.GridItem, null,
            react_1.default.createElement(MinimalHWRequirements_1.default, { aiConfigMap: aiConfigMap, isSNOCluster: isSNOCluster }))),
        react_1.default.createElement(react_core_1.GridItem, null, availableAgents.length ? (react_1.default.createElement(react_core_1.Form, null,
            react_1.default.createElement(common_1.SwitchField, { name: "autoSelectHosts", label: t('ai:Auto-select hosts'), onChange: onAutoSelectChange }),
            autoSelectHosts && (react_1.default.createElement(ClusterDeploymentHostsSelectionBasic_1.default, { availableAgents: availableAgents, isSNOCluster: isSNOCluster })),
            !autoSelectHosts && (react_1.default.createElement(ClusterDeploymentHostsSelectionAdvanced_1.ClusterDeploymentHostsSelectionAdvanced, { availableAgents: availableAgents, onEditRole: onEditRole, onHostSelect: onHostSelect, onSetInstallationDiskId: onSetInstallationDiskId, cpuArchitecture: cpuArchitecture })))) : (react_1.default.createElement(NoAgentsAlert_1.default, { cpuArchitecture: cpuArchitecture })))));
};
exports.default = ClusterDeploymentHostsSelection;
//# sourceMappingURL=ClusterDeploymentHostsSelection.js.map