"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const Yup = tslib_1.__importStar(require("yup"));
const react_core_1 = require("@patternfly/react-core");
const formik_1 = require("formik");
const common_1 = require("../../../common");
const utils_1 = require("./utils");
const use_translation_wrapper_1 = require("../../../common/hooks/use-translation-wrapper");
const validationSchema = (t) => Yup.object({
    sshPublicKey: (0, common_1.sshPublicKeyValidationSchema)(t).required(t('ai:An SSH key is required to debug hosts as they register.')),
});
const EditSSHKeyModal = ({ isOpen, onClose, infraEnv, onSubmit, hasAgents, hasBMHs, }) => {
    var _a;
    const [error, setError] = React.useState();
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const warningMsg = (0, utils_1.getWarningMessage)(hasAgents, hasBMHs, t);
    return (React.createElement(react_core_1.Modal, { "aria-label": t('ai:Edit SSH public key dialog'), isOpen: isOpen, onClose: onClose, variant: react_core_1.ModalVariant.small, id: "edit-ssh-key-modal" },
        React.createElement(react_core_1.ModalHeader, { title: t('ai:Edit SSH public key') }),
        React.createElement(formik_1.Formik, { initialValues: {
                sshPublicKey: ((_a = infraEnv.spec) === null || _a === void 0 ? void 0 : _a.sshAuthorizedKey) || '',
            }, validationSchema: validationSchema(t), onSubmit: (values) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
                try {
                    yield onSubmit(values, infraEnv);
                    onClose();
                }
                catch (err) {
                    const error = err;
                    setError((error === null || error === void 0 ? void 0 : error.message) || t('ai:An error occurred'));
                }
            }), validateOnMount: true }, ({ isSubmitting, isValid, submitForm }) => (React.createElement(React.Fragment, null,
            React.createElement(react_core_1.ModalBody, null,
                React.createElement(react_core_1.Stack, { hasGutter: true },
                    React.createElement(react_core_1.StackItem, null,
                        React.createElement(react_core_1.Alert, { isInline: true, variant: "warning", title: warningMsg })),
                    React.createElement(react_core_1.StackItem, null,
                        React.createElement(common_1.UploadSSH, null)),
                    error && (React.createElement(react_core_1.StackItem, null,
                        React.createElement(react_core_1.Alert, { title: error, variant: "danger", isInline: true }))))),
            React.createElement(react_core_1.ModalFooter, null,
                React.createElement(react_core_1.Button, { onClick: () => void submitForm(), isDisabled: isSubmitting || !isValid }, t('ai:Save')),
                React.createElement(react_core_1.Button, { onClick: onClose, variant: react_core_1.ButtonVariant.secondary }, t('ai:Cancel'))))))));
};
exports.default = EditSSHKeyModal;
//# sourceMappingURL=EditSSHKeyModal.js.map