"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 utils_2 = require("../../../common/utils");
const use_translation_wrapper_1 = require("../../../common/hooks/use-translation-wrapper");
const validationSchema = (t) => Yup.object({
    pullSecret: (0, common_1.pullSecretValidationSchema)(t).required(t('ai:Pull secret is a required field.')),
});
const EditPullSecretForm = ({ pullSecret, pullSecretError, pullSecretLoading, onClose, error: submitError, hasAgents, hasBMHs, }) => {
    const { isSubmitting, isValid, submitForm, setFieldValue } = (0, formik_1.useFormikContext)();
    React.useEffect(() => {
        var _a;
        if (!pullSecretLoading && !pullSecretError) {
            const pullSecretValue = atob(((_a = pullSecret === null || pullSecret === void 0 ? void 0 : pullSecret.data) === null || _a === void 0 ? void 0 : _a['.dockerconfigjson']) || '');
            setFieldValue('pullSecret', pullSecretValue);
            if (!pullSecret) {
                setFieldValue('createSecret', true);
            }
        }
    }, [pullSecret, pullSecretLoading, pullSecretError, setFieldValue]);
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const warningMsg = (0, utils_1.getWarningMessage)(hasAgents, hasBMHs, t);
    let body = (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.PullSecretField, { isOcm: false }))));
    if (pullSecretLoading) {
        body = React.createElement(common_1.LoadingState, null);
    }
    else if (pullSecretError) {
        body = React.createElement(react_core_1.Alert, { title: pullSecretError, variant: "danger", isInline: true });
    }
    return (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, body)),
            submitError && (React.createElement(react_core_1.StackItem, null,
                React.createElement(react_core_1.Alert, { title: submitError, variant: "danger", isInline: true })))),
        React.createElement(react_core_1.ModalFooter, null,
            React.createElement(react_core_1.Button
            // eslint-disable-next-line @typescript-eslint/no-misused-promises
            , { 
                // eslint-disable-next-line @typescript-eslint/no-misused-promises
                onClick: submitForm, isDisabled: pullSecretLoading || !!pullSecretError || isSubmitting || !isValid }, t('ai:Save')),
            React.createElement(react_core_1.Button, { onClick: onClose, variant: react_core_1.ButtonVariant.secondary }, t('ai:Cancel')))));
};
const EditPullSecretModal = (_a) => {
    var { isOpen, onClose, onSubmit, infraEnv } = _a, rest = tslib_1.__rest(_a, ["isOpen", "onClose", "onSubmit", "infraEnv"]);
    const [error, setError] = React.useState();
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    return (React.createElement(react_core_1.Modal, { "aria-label": t('ai:Edit pull secret dialog'), isOpen: isOpen, onClose: onClose, variant: react_core_1.ModalVariant.small, id: "edit-pull-secret-modal" },
        React.createElement(react_core_1.ModalHeader, { title: t('ai:Edit pull secret') }),
        React.createElement(formik_1.Formik, { initialValues: {
                pullSecret: undefined,
                createSecret: false,
            }, validationSchema: validationSchema(t), onSubmit: (values) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
                try {
                    yield onSubmit(values, infraEnv);
                    onClose();
                }
                catch (err) {
                    setError((0, utils_2.getErrorMessage)(err));
                }
            }), validateOnMount: true },
            React.createElement(EditPullSecretForm, Object.assign({}, rest, { onClose: onClose, error: error })))));
};
exports.default = EditPullSecretModal;
//# sourceMappingURL=EditPullSecretModal.js.map