"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _NavLink = _interopRequireDefault(require("../components/NavLink/NavLink"));
var _PageNavigation = _interopRequireDefault(require("../components/PageNavigation/PageNavigation"));
var _RegistrationForm = _interopRequireDefault(require("../components/RegistrationForm/RegistrationForm"));
var _RegistrationsList = _interopRequireDefault(require("../components/RegistrationsList/RegistrationsList"));
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("./LegacyPage.css");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
function LegacyPage() {
  const _useRegistrationForm = (0, _useRegistrationForm2.useRegistrationForm)(),
    formValues = _useRegistrationForm.formValues,
    fieldErrors = _useRegistrationForm.fieldErrors,
    setFieldErrors = _useRegistrationForm.setFieldErrors,
    onChange = _useRegistrationForm.onChange,
    resetForm = _useRegistrationForm.resetForm,
    validateAllFields = _useRegistrationForm.validateAllFields,
    isSubmitDisabled = _useRegistrationForm.isSubmitDisabled;
  const _useToast = (0, _useToast2.useToast)(),
    toastMessage = _useToast.toastMessage,
    toastType = _useToast.toastType,
    showToast = _useToast.showToast;
  const _useState = (0, _react.useState)([]),
    _useState2 = _slicedToArray(_useState, 2),
    registrations = _useState2[0],
    setRegistrations = _useState2[1];
  (0, _react.useEffect)(() => {
    try {
      setRegistrations((0, _module.getRegistrations)());
    } catch (error) {
      showToast(error.message || 'Une erreur est survenue', 'error');
    }
  }, [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 {
      const savedRegistration = (0, _module.handleSubmit)(e);
      setRegistrations(previousRegistrations => [...previousRegistrations, savedRegistration]);
      resetForm();
      showToast('Formulaire valide et enregistre.', 'success');
    } catch (error) {
      e.preventDefault();
      showToast(error.message || 'Une erreur est survenue', 'error');
    }
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: "legacy-page",
    "data-testid": "legacy-page",
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("header", {
      className: "legacy-page-header",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("h1", {
        children: "Mode legacy"
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("p", {
        children: "Formulaire et liste des inscrits sur une seule page (comportement initial)."
      }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_PageNavigation.default, {
        variant: "on-dark",
        ariaLabel: "Navigation legacy",
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_NavLink.default, {
          to: "/",
          theme: "dark",
          testId: "go-to-home",
          children: "Accueil"
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_NavLink.default, {
          to: "/register",
          theme: "dark",
          variant: "primary",
          testId: "go-to-registration",
          children: "Inscription"
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_NavLink.default, {
          to: "/list",
          theme: "dark",
          testId: "go-to-list",
          children: "Liste"
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_NavLink.default, {
          href: _navigation.DOCS_URL,
          external: true,
          theme: "dark",
          testId: "go-to-docs",
          children: "Documentation"
        })]
      })]
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_RegistrationForm.default, {
      formValues: formValues,
      fieldErrors: fieldErrors,
      isSubmitDisabled: isSubmitDisabled,
      onChange: onChange,
      onSubmit: onSubmit
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_RegistrationsList.default, {
      registrations: registrations,
      title: "Liste des inscrits",
      headingLevel: "h2",
      testId: "legacy-registrations-list"
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Toast.default, {
      message: toastMessage,
      type: toastType
    })]
  });
}
var _default = exports.default = LegacyPage;