"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClusterDeploymentHostsSelectionAdvanced = void 0;
const tslib_1 = require("tslib");
const isMatch_js_1 = tslib_1.__importDefault(require("lodash-es/isMatch.js"));
const formik_1 = require("formik");
const react_1 = tslib_1.__importDefault(require("react"));
const resize_observer_1 = tslib_1.__importDefault(require("@react-hook/resize-observer"));
const react_core_1 = require("@patternfly/react-core");
const common_1 = require("../../common");
const LocationsSelector_1 = tslib_1.__importDefault(require("../LocationsSelector"));
const LabelsSelector_1 = tslib_1.__importStar(require("../LabelsSelector"));
const AgentsSelectionTable_1 = tslib_1.__importDefault(require("../../Agent/AgentsSelectionTable"));
const use_translation_wrapper_1 = require("../../../../common/hooks/use-translation-wrapper");
const ClusterDeploymentHostsSelectionAdvanced = ({ availableAgents, onEditRole, onSetInstallationDiskId, onEditHost, onHostSelect, cpuArchitecture, }) => {
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const { values } = (0, formik_1.useFormikContext)();
    const { locations, agentLabels } = values;
    const matchingAgents = react_1.default.useMemo(() => availableAgents.filter((agent) => {
        var _a, _b, _c, _d;
        const labels = agentLabels.reduce((acc, curr) => {
            const label = curr.split('=');
            acc[label[0]] = label[1];
            return acc;
        }, {});
        const matchesLocation = locations.length
            ? locations.includes(((_b = (_a = agent.metadata) === null || _a === void 0 ? void 0 : _a.labels) === null || _b === void 0 ? void 0 : _b[common_1.AGENT_LOCATION_LABEL_KEY]) || common_1.AGENT_NOLOCATION_VALUE)
            : true;
        const matchesLabels = ((_c = agent.metadata) === null || _c === void 0 ? void 0 : _c.labels)
            ? (0, isMatch_js_1.default)((_d = agent.metadata) === null || _d === void 0 ? void 0 : _d.labels, labels)
            : true;
        return matchesLocation && matchesLabels;
    }), [availableAgents, locations, agentLabels]);
    const tableRef = react_1.default.useRef(null);
    const [tableWidth, setTableWidth] = react_1.default.useState();
    (0, resize_observer_1.default)(tableRef, (entry) => {
        setTableWidth(entry.contentRect.width);
    });
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(react_core_1.Grid, { hasGutter: true },
            react_1.default.createElement(react_core_1.GridItem, null,
                react_1.default.createElement(LocationsSelector_1.default, { agents: availableAgents })),
            react_1.default.createElement(react_core_1.GridItem, null,
                react_1.default.createElement(LabelsSelector_1.default, { agents: matchingAgents, name: "agentLabels", labelKeysFilter: LabelsSelector_1.infraEnvLabelKeys })),
            cpuArchitecture && (react_1.default.createElement(react_core_1.Alert, { variant: react_core_1.AlertVariant.info, title: t('ai:Displaying only hosts with {{cpuArchitecture}} architecture in the table.', {
                    cpuArchitecture,
                }), isInline: true })),
            react_1.default.createElement(react_core_1.GridItem, null,
                react_1.default.createElement("div", { ref: tableRef },
                    react_1.default.createElement(AgentsSelectionTable_1.default, { matchingAgents: matchingAgents, onEditRole: onEditRole, onSetInstallationDiskId: onSetInstallationDiskId, width: tableWidth, onEditHost: onEditHost, onHostSelect: onHostSelect }))))));
};
exports.ClusterDeploymentHostsSelectionAdvanced = ClusterDeploymentHostsSelectionAdvanced;
//# sourceMappingURL=ClusterDeploymentHostsSelectionAdvanced.js.map