"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 formik_1 = require("formik");
const react_redux_1 = require("react-redux");
const selectors_1 = require("../../../store/slices/current-cluster/selectors");
const NewFeatureSupportLevelBadge_1 = tslib_1.__importDefault(require("../../../../common/components/newFeatureSupportLevels/NewFeatureSupportLevelBadge"));
const common_1 = require("../../../../common");
const newFeatureSupportLevels_1 = require("../../../../common/components/newFeatureSupportLevels");
const utils_1 = require("./utils");
const operatorSpecs_1 = require("../../../../common/components/operators/operatorSpecs");
const OperatorRequirements = ({ operatorId, preflightRequirements, Requirements, cluster, }) => {
    var _a, _b, _c, _d, _e;
    const isSingleNode = (0, react_redux_1.useSelector)(selectors_1.selectIsCurrentClusterSNO);
    const operator = (_a = preflightRequirements === null || preflightRequirements === void 0 ? void 0 : preflightRequirements.operators) === null || _a === void 0 ? void 0 : _a.find((operatorRequirements) => operatorRequirements.operatorName === operatorId);
    const workerRequirements = (_c = (_b = operator === null || operator === void 0 ? void 0 : operator.requirements) === null || _b === void 0 ? void 0 : _b.worker) === null || _c === void 0 ? void 0 : _c.quantitative;
    const masterRequirements = (_e = (_d = operator === null || operator === void 0 ? void 0 : operator.requirements) === null || _d === void 0 ? void 0 : _d.master) === null || _e === void 0 ? void 0 : _e.quantitative;
    const hasWorkerRequirements = !!Object.keys(workerRequirements || {}).length;
    const hasMasterRequirements = !!Object.keys(masterRequirements || {}).length;
    if (!hasWorkerRequirements && !hasMasterRequirements && !Requirements) {
        return (React.createElement(common_1.PopoverIcon, { component: 'a', id: operatorId, bodyContent: "No additional requirements needed" }));
    }
    return (React.createElement(common_1.PopoverIcon, { component: 'a', headerContent: "Additional requirements", id: operatorId, bodyContent: React.createElement(react_core_1.Stack, { hasGutter: true },
            Requirements && (React.createElement(react_core_1.StackItem, null,
                React.createElement(Requirements, { cluster: cluster }))),
            React.createElement(react_core_1.StackItem, null,
                React.createElement(react_core_1.List, null,
                    !isSingleNode && hasWorkerRequirements && (React.createElement(react_core_1.ListItem, null,
                        "Each worker node requires an additional ",
                        (workerRequirements === null || workerRequirements === void 0 ? void 0 : workerRequirements.ramMib) || 360,
                        " MiB of memory ",
                        (workerRequirements === null || workerRequirements === void 0 ? void 0 : workerRequirements.diskSizeGb) ? ',' : ' and',
                        ' ',
                        (workerRequirements === null || workerRequirements === void 0 ? void 0 : workerRequirements.cpuCores) || 2,
                        " CPU cores",
                        (workerRequirements === null || workerRequirements === void 0 ? void 0 : workerRequirements.diskSizeGb)
                            ? ` and ${workerRequirements === null || workerRequirements === void 0 ? void 0 : workerRequirements.diskSizeGb} storage space`
                            : '')),
                    hasMasterRequirements && (React.createElement(react_core_1.ListItem, null,
                        "Each control plane node requires an additional ",
                        (masterRequirements === null || masterRequirements === void 0 ? void 0 : masterRequirements.ramMib) || 150,
                        ' ',
                        "MiB of memory ",
                        (masterRequirements === null || masterRequirements === void 0 ? void 0 : masterRequirements.diskSizeGb) ? ',' : ' and',
                        ' ',
                        (masterRequirements === null || masterRequirements === void 0 ? void 0 : masterRequirements.cpuCores) || 4,
                        " CPU cores",
                        (masterRequirements === null || masterRequirements === void 0 ? void 0 : masterRequirements.diskSizeGb)
                            ? ` and ${masterRequirements === null || masterRequirements === void 0 ? void 0 : masterRequirements.diskSizeGb} storage space`
                            : ''))))) }));
};
const OperatorCheckbox = ({ bundles, cluster, operatorId, title, featureId, notStandalone, Description, Requirements, openshiftVersion, preflightRequirements, searchTerm, }) => {
    var _a, _b;
    const { getFeatureSupportLevel, getFeatureDisabledReason } = (0, newFeatureSupportLevels_1.useNewFeatureSupportLevel)();
    const { byKey: opSpecs } = (0, operatorSpecs_1.useOperatorSpecs)();
    const { isViewerMode } = (0, react_redux_1.useSelector)(selectors_1.selectCurrentClusterPermissionsState);
    const { values, setFieldValue } = (0, formik_1.useFormikContext)();
    const fieldId = (0, common_1.getFieldId)(operatorId, 'input');
    const supportLevel = getFeatureSupportLevel(featureId);
    const isInBundle = values.selectedBundles.some((sb) => { var _a, _b; return !!((_b = (_a = bundles.find((b) => b.id === sb)) === null || _a === void 0 ? void 0 : _a.operators) === null || _b === void 0 ? void 0 : _b.includes(operatorId)); });
    const isChecked = values.selectedOperators.includes(operatorId) || isInBundle;
    const parentOperator = (_a = preflightRequirements === null || preflightRequirements === void 0 ? void 0 : preflightRequirements.operators) === null || _a === void 0 ? void 0 : _a.find((op) => { var _a; return (_a = op.dependencies) === null || _a === void 0 ? void 0 : _a.includes(operatorId); });
    let parentOperatorName = '';
    if ((parentOperator === null || parentOperator === void 0 ? void 0 : parentOperator.operatorName) &&
        values.selectedOperators.includes(parentOperator.operatorName)) {
        parentOperatorName = ((_b = opSpecs[parentOperator.operatorName]) === null || _b === void 0 ? void 0 : _b.title) || parentOperator.operatorName;
    }
    const disabledReason = isInBundle
        ? 'This operator is part of a bundle and cannot be deselected.'
        : notStandalone
            ? 'This operator cannot be installed as a standalone'
            : parentOperatorName
                ? `This operator is a dependency of ${parentOperatorName}`
                : getFeatureDisabledReason(featureId);
    return (React.createElement(react_core_1.FormGroup, { fieldId: fieldId, id: `form-control__${fieldId}` },
        React.createElement(react_core_1.Checkbox, { id: fieldId, label: React.createElement(React.Fragment, null,
                React.createElement(react_core_1.Tooltip, { hidden: !disabledReason, content: disabledReason },
                    React.createElement("span", null,
                        (0, operatorSpecs_1.highlightMatch)(title, searchTerm),
                        " ")),
                React.createElement(OperatorRequirements, { operatorId: operatorId, preflightRequirements: preflightRequirements, Requirements: Requirements, cluster: cluster }),
                React.createElement(NewFeatureSupportLevelBadge_1.default, { featureId: featureId, supportLevel: supportLevel })), "aria-describedby": `${fieldId}-helper`, isChecked: isChecked, onChange: (_, checked) => {
                setFieldValue('selectedOperators', (0, utils_1.getNewOperators)(values.selectedOperators, operatorId, preflightRequirements, checked, opSpecs));
            }, isDisabled: isViewerMode || !!disabledReason, description: !!Description && (React.createElement(react_core_1.HelperText, null,
                React.createElement(react_core_1.HelperTextItem, { "data-testid": `operator-checkbox-description-${operatorId}` },
                    React.createElement(Description, { openshiftVersion: openshiftVersion, searchTerm: searchTerm })))), "data-testid": `operator-checkbox-${operatorId}` })));
};
exports.default = OperatorCheckbox;
//# sourceMappingURL=OperatorCheckbox.js.map