"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _reactRouterDom = require("react-router-dom");
var _NavLink = _interopRequireDefault(require("../components/NavLink/NavLink"));
var _PageNavigation = _interopRequireDefault(require("../components/PageNavigation/PageNavigation"));
var _RegistrationsList = _interopRequireDefault(require("../components/RegistrationsList/RegistrationsList"));
var _Toast = _interopRequireDefault(require("../components/Toast/Toast"));
var _navigation = require("../constants/navigation");
var _useToast2 = require("../hooks/useToast");
var _module = require("../module/module");
require("./ListPage.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; }
const HIGHLIGHT_DURATION = 4000;
function ListPage() {
  const location = (0, _reactRouterDom.useLocation)();
  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];
  const _useState3 = (0, _react.useState)(() => {
      var _location$state;
      return typeof ((_location$state = location.state) === null || _location$state === void 0 ? void 0 : _location$state.highlightIndex) === 'number' ? location.state.highlightIndex : null;
    }),
    _useState4 = _slicedToArray(_useState3, 2),
    highlightedIndex = _useState4[0],
    setHighlightedIndex = _useState4[1];
  (0, _react.useEffect)(() => {
    try {
      setRegistrations((0, _module.getRegistrations)());
    } catch (error) {
      showToast(error.message || 'Une erreur est survenue', 'error');
    }
  }, [showToast]);
  (0, _react.useEffect)(() => {
    if (highlightedIndex === null || registrations.length === 0) {
      return undefined;
    }
    const highlightedElement = document.querySelector("[data-highlight-index=\"".concat(highlightedIndex, "\"]"));
    if (typeof (highlightedElement === null || highlightedElement === void 0 ? void 0 : highlightedElement.scrollIntoView) === 'function') {
      highlightedElement.scrollIntoView({
        behavior: 'smooth',
        block: 'nearest'
      });
    }
    const timeoutId = setTimeout(() => {
      setHighlightedIndex(null);
    }, HIGHLIGHT_DURATION);
    return () => clearTimeout(timeoutId);
  }, [highlightedIndex, registrations]);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "list-page",
      "data-testid": "list-page",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_RegistrationsList.default, {
        registrations: registrations,
        title: "Liste des inscrits",
        headingLevel: "h1",
        highlightedIndex: highlightedIndex,
        testId: "list-registrations-section"
      }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_PageNavigation.default, {
        variant: "card",
        ariaLabel: "Navigation liste",
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_NavLink.default, {
          to: "/",
          testId: "go-to-home",
          children: "Accueil"
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_NavLink.default, {
          to: "/register",
          variant: "primary",
          testId: "go-to-registration",
          children: "Nouvelle inscription"
        }), /*#__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 = ListPage;