"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _uniqueId = _interopRequireDefault(require("lodash-es/uniqueId"));
var _faChevronLeft = require("@fortawesome/free-solid-svg-icons/faChevronLeft");
var _faChevronRight = require("@fortawesome/free-solid-svg-icons/faChevronRight");
var _pagesButtons = require("./pages-buttons");
var _button = require("../button");
var _dropdownMenu = require("../dropdown-menu");
var _truncateWithTooltip = require("../truncate-with-tooltip");
var _skeleton = require("../skeleton");
var _useTranslation = require("../hooks/use-translation");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
/**
 * New Pagination component to be used with new table component and likely in other places where pagination is needed
 * Designed for UXR only UI.
 * @midnight-gray-supported
 * @alpine-light-supported
 * @uxr-only-supported
 */
function PaginationNew({
  selectedPage,
  total,
  totalLimit,
  isExceededTotalLimit = !!totalLimit && total >= totalLimit,
  nextPage,
  itemsPerPage,
  pageSizeOptions = [10, 20, 50],
  onPageSelect,
  onItemsPerPageChange,
  isLoading = false,
  nextBtnText: nextBtnTextParams,
  previousBtnText: previousBtnTextParams,
  paginationGroupText,
  getSelectedPageSizeText,
  getPaginationSummaryText
}) {
  const {
    t
  } = (0, _useTranslation.useTranslation)();
  const nextBtnText = nextBtnTextParams ?? t('Next page');
  const previousBtnText = previousBtnTextParams ?? t('Previous page');
  (0, _react.useEffect)(() => {
    if (totalLimit && nextPage === undefined && process.env.NODE_ENV === 'development') {
      // eslint-disable-next-line no-console
      console.error('[PaginationNew] PaginationNew must be used with defined nextPage if totalLimit is used');
    }
  }, []);
  const pageSizeOpts = (0, _react.useMemo)(() => {
    if (total === 0) {
      return [];
    }
    const filteredSizes = pageSizeOptions.filter(option => option < total);
    if (filteredSizes.length < pageSizeOptions.length) {
      return [...filteredSizes, total];
    }
    return pageSizeOptions;
  }, [pageSizeOptions, total]);
  const itemsPerPageOptions = (0, _react.useMemo)(() => pageSizeOpts.map(item => ({
    key: item,
    title: item,
    testId: `page-size-option-${item}`
  })), [pageSizeOpts]);
  const numberOfPages = (0, _react.useMemo)(() => {
    if (isExceededTotalLimit) {
      if (!nextPage) {
        return selectedPage + 1;
      }
      return selectedPage >= Math.ceil(totalLimit / itemsPerPage) ? selectedPage + 1 : Math.ceil(totalLimit / itemsPerPage);
    }
    return Math.ceil(total / itemsPerPage);
  }, [itemsPerPage, total, totalLimit, nextPage, selectedPage, isExceededTotalLimit]);
  const isNextDisabled = isExceededTotalLimit ? !nextPage : selectedPage + 1 >= numberOfPages;
  const [perPageTriggerId] = (0, _react.useState)(`pagination-items-per-page-trigger-${(0, _uniqueId.default)()}`);
  const getDefaultPaginationSummaryText = () => {
    const start = total === 0 ? 0 : itemsPerPage * selectedPage + 1;
    const endNumber = itemsPerPage * (selectedPage + 1);
    const endNumberToTotalItems = total < endNumber ? total : endNumber;
    const end = total === 0 ? 0 : endNumberToTotalItems;
    const endLimited = selectedPage * itemsPerPage + itemsPerPage;
    return isExceededTotalLimit ? t('{{start}}-{{endLimited}} of {{totalLimit}}+ items', {
      start,
      endLimited,
      totalLimit: totalLimit
    }) : t('{{start}}-{{end}} of {{total}} items', {
      start,
      end,
      total
    });
  };
  if (isLoading) {
    return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "pagination-container",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "pagination-buttons-container",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_skeleton.Skeleton, {
          type: _skeleton.SKELETON_TYPES.ROUNDED,
          width: 300,
          className: "pagination-loading-skeleton",
          testId: "pagination-loading-skeleton"
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_skeleton.Skeleton, {
        type: _skeleton.SKELETON_TYPES.ROUNDED,
        width: 90,
        className: "pagination-loading-skeleton",
        testId: "pagination-loading-skeleton"
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "pagination-summary-container",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_skeleton.Skeleton, {
          type: _skeleton.SKELETON_TYPES.ROUNDED,
          width: 120,
          className: "pagination-loading-skeleton",
          testId: "pagination-loading-skeleton"
        })
      })]
    });
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: "pagination-container",
    "aria-label": paginationGroupText ?? t('Pagination'),
    role: "group",
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "pagination-buttons-container",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_button.Button, {
        accentType: _button.ACCENT_TYPES.GHOST,
        leftIcon: _faChevronLeft.faChevronLeft,
        className: "pagination-prev-btn",
        isDisabled: selectedPage === 0,
        onClick: () => onPageSelect(selectedPage - 1),
        testId: "pagination-previous-btn",
        "aria-label": previousBtnText,
        children: previousBtnText
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_pagesButtons.PagesButtons, {
        selectedPage: selectedPage,
        pageCount: numberOfPages,
        onPageSelect: onPageSelect
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.Button, {
        accentType: _button.ACCENT_TYPES.GHOST,
        rightIcon: _faChevronRight.faChevronRight,
        className: "pagination-next-btn",
        isDisabled: isNextDisabled,
        onClick: () => onPageSelect(selectedPage + 1),
        testId: "pagination-next-btn",
        "aria-label": nextBtnText,
        children: nextBtnText
      })]
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropdownMenu.DropdownMenu, {
      options: itemsPerPageOptions,
      selectedKey: itemsPerPage,
      onSelect: selectedOption => {
        if (selectedOption) {
          onItemsPerPageChange(selectedOption.key);
        }
      },
      triggerDynamicContent: () => /*#__PURE__*/(0, _jsxRuntime.jsx)(_truncateWithTooltip.TruncateWithTooltip, {
        children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
            className: "long-page-size-trigger-name",
            children: getSelectedPageSizeText?.(itemsPerPage, total) ?? t('{{pages}} per page', {
              pages: total < itemsPerPage ? total : itemsPerPage
            })
          }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
            className: "short-page-size-trigger-name",
            children: itemsPerPage
          })]
        })
      }),
      isDisabled: total === 0,
      triggerClassName: "pagination-items-per-page-dropdown",
      triggerId: perPageTriggerId,
      triggerTestId: "pagination-items-per-page-trigger",
      contentTestId: "pagination-items-per-page-dropdown-content"
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: "pagination-summary-container",
      "test-id": "pagination-summary-container",
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_truncateWithTooltip.TruncateWithTooltip, {
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
          children: getPaginationSummaryText?.({
            selectedPage,
            itemsPerPage,
            total,
            totalLimit,
            isExceededTotalLimit
          }) ?? getDefaultPaginationSummaryText()
        })
      })
    })]
  });
}
var _default = exports.default = /*#__PURE__*/(0, _react.memo)(PaginationNew);