"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SshPublicKeyHelperText = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const formik_1 = require("formik");
const react_core_1 = require("@patternfly/react-core");
const RenderIf_1 = require("../ui/RenderIf");
const ui_1 = require("../ui");
const config_1 = require("../../config");
const use_translation_wrapper_1 = require("../../hooks/use-translation-wrapper");
const SshPublicKeyHelperText = ({ fieldId = 'sshPublicKey', allowMultipleKeys = false }) => {
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    return (React.createElement(react_core_1.HelperText, { id: fieldId },
        React.createElement(react_core_1.HelperTextItem, null,
            allowMultipleKeys
                ? t('ai:Paste the content of public SSH keys you want to use to connect to the hosts into this field. Multiple keys can be separated by newlines.')
                : t('ai:Paste the content of a public SSH key you want to use to connect to the hosts into this field.'),
            ' ',
            React.createElement(ui_1.ExternalLink, { href: config_1.SSH_GENERATION_DOC_LINK }, t('ai:Learn more')))));
};
exports.SshPublicKeyHelperText = SshPublicKeyHelperText;
const SecurityFields = ({ clusterSshKey, imageSshKey, isDisabled = false, allowMultipleKeys = false, }) => {
    //shareSshKey shouldn't response to changes. imageSshKey stays the same, there's a loading state while it's requested
    //clusterSshKey updating causes the textarea to disappear when the user clears it to edit it
    const defaultShareSshKey = !!imageSshKey && (clusterSshKey === imageSshKey || !clusterSshKey);
    const [shareSshKey, setShareSshKey] = React.useState(defaultShareSshKey);
    const { values, setFieldValue } = (0, formik_1.useFormikContext)();
    const handleSshKeyBlur = () => {
        if (values.sshPublicKey) {
            setFieldValue('sshPublicKey', (0, ui_1.trimSshPublicKey)(values.sshPublicKey));
        }
    };
    React.useEffect(() => {
        if (!isDisabled && shareSshKey) {
            setFieldValue('sshPublicKey', imageSshKey);
        }
    }, [shareSshKey, imageSshKey, setFieldValue, isDisabled]);
    const fieldId = (0, ui_1.getFieldId)('shareDiscoverySshKey', 'checkbox');
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    return (React.createElement(React.Fragment, null,
        React.createElement(react_core_1.FormGroup, { fieldId: fieldId, label: t('ai:Host SSH Public Key for troubleshooting after installation') },
            React.createElement(RenderIf_1.RenderIf, { condition: Boolean(imageSshKey) },
                React.createElement(react_core_1.Checkbox, { name: "shareDiscoverySshKey", id: fieldId, label: t('ai:Use the same host discovery SSH key'), "aria-describedby": `${fieldId}-helper`, isChecked: shareSshKey, isDisabled: isDisabled, onChange: (_event, value) => setShareSshKey(value) })),
            React.createElement(RenderIf_1.RenderIf, { condition: !shareSshKey },
                React.createElement(ui_1.TextAreaField, { name: "sshPublicKey", helperText: React.createElement(exports.SshPublicKeyHelperText, { allowMultipleKeys: allowMultipleKeys }), onBlur: handleSshKeyBlur, isDisabled: isDisabled })))));
};
exports.default = SecurityFields;
//# sourceMappingURL=SecurityFields.js.map