"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const isEqual_js_1 = tslib_1.__importDefault(require("lodash-es/isEqual.js"));
const react_core_1 = require("@patternfly/react-core");
const SwitchField_1 = tslib_1.__importDefault(require("../../ui/formik/SwitchField"));
const DiskEncryptionMode_1 = require("./DiskEncryptionMode");
const ui_1 = require("../../ui");
const formik_1 = require("formik");
const use_translation_wrapper_1 = require("../../../hooks/use-translation-wrapper");
const hasFilledTangServers = (tangServers) => {
    if (!tangServers || tangServers.length === 0) {
        return false;
    }
    const emptyServer = {
        url: '',
        thumbprint: '',
    };
    return (tangServers.find((server) => !(0, isEqual_js_1.default)(server, emptyServer) && !(0, isEqual_js_1.default)(server, {})) !== undefined);
};
const DiskEncryptionControlGroup = ({ values, isSNO = false, isDisabled = false, docVersion, }) => {
    const { enableDiskEncryptionOnMasters, enableDiskEncryptionOnWorkers, enableDiskEncryptionOnArbiters, diskEncryptionMode, diskEncryptionTangServers, } = values;
    const fieldId = 'diskEncryption';
    const hasEnabledDiskEncryption = enableDiskEncryptionOnMasters ||
        enableDiskEncryptionOnWorkers ||
        enableDiskEncryptionOnArbiters;
    const { setFieldValue, setFieldTouched } = (0, formik_1.useFormikContext)();
    react_1.default.useEffect(() => {
        if (!hasEnabledDiskEncryption) {
            if (diskEncryptionMode !== 'tpmv2') {
                setFieldValue('diskEncryptionMode', 'tpmv2');
            }
            if (hasFilledTangServers(diskEncryptionTangServers)) {
                setFieldTouched('diskEncryptionTangServers', false, false);
                setFieldValue('diskEncryptionTangServers', [{}], false);
            }
        }
    }, [
        diskEncryptionMode,
        diskEncryptionTangServers,
        setFieldTouched,
        setFieldValue,
        hasEnabledDiskEncryption,
    ]);
    react_1.default.useEffect(() => {
        if (isSNO) {
            setFieldValue('enableDiskEncryptionOnWorkers', false);
        }
    }, [isSNO, setFieldValue]);
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const disableMessage = t('ai:This option is not editable after the draft cluster is created');
    const tooltipProps = {
        hidden: !isDisabled,
        content: disableMessage,
    };
    return (react_1.default.createElement(react_core_1.FormGroup, { id: `form-control__${fieldId}`, label: "Encryption of installation disks" },
        react_1.default.createElement(react_core_1.Stack, { hasGutter: true },
            react_1.default.createElement(react_core_1.StackItem, null,
                react_1.default.createElement(SwitchField_1.default, { tooltipProps: tooltipProps, name: "enableDiskEncryptionOnMasters", label: isSNO ? t('ai:Control plane node, worker') : t('ai:Control plane nodes'), isDisabled: isDisabled })),
            react_1.default.createElement(ui_1.RenderIf, { condition: !isSNO },
                react_1.default.createElement(react_core_1.StackItem, null,
                    react_1.default.createElement(SwitchField_1.default, { tooltipProps: tooltipProps, name: "enableDiskEncryptionOnWorkers", isDisabled: isDisabled, label: t('ai:Workers') }))),
            react_1.default.createElement(react_core_1.StackItem, null,
                react_1.default.createElement(SwitchField_1.default, { tooltipProps: tooltipProps, name: "enableDiskEncryptionOnArbiters", label: t('ai:Arbiter'), isDisabled: isDisabled })),
            react_1.default.createElement(ui_1.RenderIf, { condition: enableDiskEncryptionOnMasters ||
                    (enableDiskEncryptionOnWorkers && !isSNO) ||
                    enableDiskEncryptionOnArbiters },
                react_1.default.createElement(react_core_1.StackItem, null,
                    react_1.default.createElement(DiskEncryptionMode_1.DiskEncryptionMode, { diskEncryptionMode: diskEncryptionMode, isDisabled: isDisabled, tooltipProps: tooltipProps, docVersion: docVersion || '' }))),
            react_1.default.createElement(ui_1.RenderIf, { condition: hasEnabledDiskEncryption },
                react_1.default.createElement(react_core_1.StackItem, null,
                    react_1.default.createElement(react_core_1.Alert, { variant: react_core_1.AlertVariant.warning, "data-testid": "alert-tpmv2-bios", isInline: true, title: react_1.default.createElement(react_core_1.FlexItem, null,
                            diskEncryptionMode === 'tpmv2' && (react_1.default.createElement(react_1.default.Fragment, null, "To use this encryption method, enable TPMv2 encryption in the BIOS of each selected host.")),
                            diskEncryptionMode === 'tang' && (react_1.default.createElement(react_1.default.Fragment, null, "The use of Tang encryption mode to encrypt your disks is only supported for bare metal or vSphere installations on user-provisioned infrastructure."))) }))))));
};
exports.default = DiskEncryptionControlGroup;
//# sourceMappingURL=DiskEncryptionControlGroup.js.map