"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdditionalNTPSourcesDialog = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const formik_1 = require("formik");
const Yup = tslib_1.__importStar(require("yup"));
const react_core_1 = require("@patternfly/react-core");
const common_1 = require("../../../common");
const ntpValidation_1 = require("../../validationSchemas/ntpValidation");
const ui_1 = require("../../../common/components/ui");
const use_translation_wrapper_1 = require("../../hooks/use-translation-wrapper");
const AdditionalNTPSourcesForm = ({ additionalNtpSource, onClose, onAdditionalNtpSource, }) => {
    const initialValues = {
        additionalNtpSource: additionalNtpSource || '',
    };
    const getValidationSchema = (t) => Yup.object().shape({
        additionalNtpSource: (0, ntpValidation_1.ntpSourceValidationSchema)(t, false),
    });
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const handleSubmit = (values, formikHelpers) => {
        formikHelpers.setStatus({ error: null });
        if (onAdditionalNtpSource && values.additionalNtpSource) {
            const onError = (message) => formikHelpers.setStatus({
                error: {
                    title: 'Failed to add NTP sources',
                    message,
                },
            });
            void onAdditionalNtpSource(values.additionalNtpSource, onError);
        }
        onClose();
    };
    return (react_1.default.createElement(formik_1.Formik, { initialValues: initialValues, initialTouched: { additionalNtpSource: true }, initialStatus: { error: null }, validationSchema: getValidationSchema(t), onSubmit: handleSubmit }, ({ submitForm, status, setStatus, isSubmitting, isValid, dirty }) => {
        return (react_1.default.createElement(react_1.default.Fragment, null,
            react_1.default.createElement(react_core_1.ModalBody, null,
                react_1.default.createElement(react_core_1.Form, null,
                    react_1.default.createElement(ui_1.AlertFormikError, { status: status, onClose: () => setStatus({ error: null }) }),
                    react_1.default.createElement(common_1.AdditionalNTPSourcesField, { name: "additionalNtpSource", label: t('ai:Additional NTP Sources'), helperText: t('ai:A comma separated list of IP or domain names of the NTP pools or servers. Additional NTP sources are added to all hosts to ensure all hosts clocks are synchronized with a valid NTP server. It may take a few minutes for the new NTP sources to sync.'), isRequired: true }))),
            react_1.default.createElement(react_core_1.ModalFooter, null,
                react_1.default.createElement(react_core_1.Button, { key: "submit", 
                    // eslint-disable-next-line @typescript-eslint/no-misused-promises
                    onClick: submitForm, isDisabled: isSubmitting || !isValid || !dirty, isLoading: isSubmitting }, isSubmitting ? t('ai:Adding...') : t('ai:Add')),
                react_1.default.createElement(react_core_1.Button, { key: "cancel", variant: "link", onClick: onClose }, t('ai:Cancel')))));
    }));
};
const AdditionalNTPSourcesDialog = ({ additionalNtpSource, isOpen, onClose, onAdditionalNtpSource, }) => (react_1.default.createElement(react_core_1.Modal, { "aria-label": "Add NTP sources", isOpen: isOpen, onClose: onClose, variant: react_core_1.ModalVariant.small },
    react_1.default.createElement(react_core_1.ModalHeader, { title: "Add NTP sources" }),
    react_1.default.createElement(AdditionalNTPSourcesForm, { additionalNtpSource: additionalNtpSource, onClose: onClose, onAdditionalNtpSource: onAdditionalNtpSource })));
exports.AdditionalNTPSourcesDialog = AdditionalNTPSourcesDialog;
//# sourceMappingURL=AdditionalNTPSourcesDialog.js.map