"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const Yup = tslib_1.__importStar(require("yup"));
const react_core_1 = require("@patternfly/react-core");
const formik_1 = require("formik");
const ui_1 = require("../../../common/components/ui");
const GridGap_1 = tslib_1.__importDefault(require("../../../common/components/ui/GridGap"));
const common_1 = require("../../../common");
const use_translation_wrapper_1 = require("../../../common/hooks/use-translation-wrapper");
const UpdateDay2ApiVipForm = ({ onUpdateDay2ApiVip, onClose, currentApiVip: originApiVip, }) => {
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const apiVipInputRef = react_1.default.useRef();
    react_1.default.useEffect(() => {
        var _a, _b;
        (_a = apiVipInputRef.current) === null || _a === void 0 ? void 0 : _a.focus();
        (_b = apiVipInputRef.current) === null || _b === void 0 ? void 0 : _b.select();
    }, []);
    const initialValues = {
        apiVip: originApiVip,
    };
    const validationSchema = react_1.default.useMemo(() => Yup.object().shape({
        apiVip: (0, common_1.day2ApiVipValidationSchema)(t).required(t('ai:Required field')),
    }), [t]);
    return (react_1.default.createElement(formik_1.Formik, { initialValues: initialValues, validationSchema: validationSchema, initialStatus: { error: null }, onSubmit: (values, formikHelpers) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
            if (values.apiVip) {
                const onError = (message) => {
                    const error = {
                        error: {
                            title: 'Failed to update API IP',
                            message,
                        },
                    };
                    formikHelpers.setStatus(error);
                };
                yield onUpdateDay2ApiVip(values.apiVip, onError);
                onClose();
            }
        }) }, ({ handleSubmit, status, setStatus, isSubmitting, isValid, dirty }) => {
        return (react_1.default.createElement(react_core_1.Form, { onSubmit: handleSubmit },
            react_1.default.createElement(react_core_1.ModalBody, null,
                react_1.default.createElement(GridGap_1.default, null,
                    react_1.default.createElement(ui_1.AlertFormikError, { status: status, onClose: () => setStatus({ error: null }) }),
                    react_1.default.createElement(ui_1.InputField, { label: "Set the IP or domain used to reach the cluster", name: "apiVip", ref: apiVipInputRef, isRequired: true }))),
            react_1.default.createElement(react_core_1.ModalFooter, null,
                react_1.default.createElement(react_core_1.Button, { key: "submit", type: react_core_1.ButtonType.submit, isDisabled: isSubmitting || !isValid || !dirty }, "Update"),
                react_1.default.createElement(react_core_1.Button, { key: "cancel", variant: react_core_1.ButtonVariant.link, onClick: onClose }, "Cancel"))));
    }));
};
exports.default = UpdateDay2ApiVipForm;
//# sourceMappingURL=UpdateDay2ApiVipForm.js.map