"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _reactRouterDom = require("react-router-dom");
var _NavLink = _interopRequireDefault(require("../components/NavLink/NavLink"));
var _PageNavigation = _interopRequireDefault(require("../components/PageNavigation/PageNavigation"));
var _RegistrationForm = _interopRequireDefault(require("../components/RegistrationForm/RegistrationForm"));
var _Toast = _interopRequireDefault(require("../components/Toast/Toast"));
var _navigation = require("../constants/navigation");
var _useRegistrationForm2 = require("../hooks/useRegistrationForm");
var _useToast2 = require("../hooks/useToast");
var _module = require("../module/module");
require("./RegistrationPage.css");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function RegistrationPage() {
  const navigate = (0, _reactRouterDom.useNavigate)();
  const _useRegistrationForm = (0, _useRegistrationForm2.useRegistrationForm)(),
    formValues = _useRegistrationForm.formValues,
    fieldErrors = _useRegistrationForm.fieldErrors,
    setFieldErrors = _useRegistrationForm.setFieldErrors,
    onChange = _useRegistrationForm.onChange,
    validateAllFields = _useRegistrationForm.validateAllFields,
    isSubmitDisabled = _useRegistrationForm.isSubmitDisabled;
  const _useToast = (0, _useToast2.useToast)(),
    toastMessage = _useToast.toastMessage,
    toastType = _useToast.toastType,
    showToast = _useToast.showToast;
  const onSubmit = e => {
    const nextErrors = validateAllFields();
    setFieldErrors(nextErrors);
    if (Object.keys(nextErrors).length > 0) {
      e.preventDefault();
      showToast('Veuillez corriger les erreurs du formulaire.', 'error');
      return;
    }
    try {
      (0, _module.handleSubmit)(e);
      const highlightIndex = (0, _module.getRegistrations)().length - 1;
      navigate('/list', {
        state: {
          highlightIndex
        }
      });
    } catch (error) {
      e.preventDefault();
      showToast(error.message || 'Une erreur est survenue', 'error');
    }
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: "registration-page",
    "data-testid": "registration-page",
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_RegistrationForm.default, {
      formValues: formValues,
      fieldErrors: fieldErrors,
      isSubmitDisabled: isSubmitDisabled,
      onChange: onChange,
      onSubmit: onSubmit
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_PageNavigation.default, {
      variant: "below-form",
      ariaLabel: "Navigation inscription",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_NavLink.default, {
        to: "/",
        testId: "go-to-home",
        children: "Accueil"
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_NavLink.default, {
        to: "/list",
        testId: "go-to-list",
        children: "Liste des inscrits"
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_NavLink.default, {
        to: "/legacy",
        testId: "go-to-legacy",
        children: "Mode legacy"
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_NavLink.default, {
        href: _navigation.DOCS_URL,
        external: true,
        testId: "go-to-docs",
        children: "Documentation"
      })]
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Toast.default, {
      message: toastMessage,
      type: toastType
    })]
  });
}
var _default = exports.default = RegistrationPage;