'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var index$1 = require('../../utils/index.js');
var Pagination_styles = require('./Pagination.styles.js');
var index = require('./utils/index.js');
var base = require('./variations/_view/base.js');
var base$2 = require('./variations/_type/base.js');
var base$1 = require('./variations/_size/base.js');
var Pagination_tokens = require('./Pagination.tokens.js');
var PaginationQuickJumpToPage = require('./ui/PaginationQuickJumpToPage/PaginationQuickJumpToPage.js');
var PaginationSelectPerPage = require('./ui/PaginationSelectPerPage/PaginationSelectPerPage.js');
var useForkRef = require('../../hooks/useForkRef.js');
var isNumber = require('../../utils/isNumber.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);

var _excluded = ["value", "defaultValue", "helperText", "singleLine", "perPage", "defaultPerPage", "slots", "view", "viewCurrentPage", "type", "size", "pilled", "square", "disabledPages", "count", "hasQuickJump", "hasPerPage", "perPageList", "isCommonButtonStyles", "placeholderQuickJump", "textQuickJump", "textPerPage", "leftContent", "rightContent", "listWidth", "onChangePageValue", "onChangePerPageValue", "onChange", "onFocusQuickJump", "onBlurQuickJump"];
var paginationRoot = function paginationRoot(Root) {
  return /*#__PURE__*/React.forwardRef(function (_ref, ref) {
    var outerValue = _ref.value,
      defaultValue = _ref.defaultValue,
      helperText = _ref.helperText,
      singleLine = _ref.singleLine,
      outerPerPage = _ref.perPage,
      defaultPerPage = _ref.defaultPerPage,
      _ref$slots = _ref.slots,
      slots = _ref$slots === void 0 ? index.defaultValues.slots : _ref$slots,
      view = _ref.view,
      viewCurrentPage = _ref.viewCurrentPage,
      _ref$type = _ref.type,
      type = _ref$type === void 0 ? index.defaultValues.type : _ref$type,
      size = _ref.size,
      _ref$pilled = _ref.pilled,
      pilled = _ref$pilled === void 0 ? index.defaultValues.pilled : _ref$pilled,
      _ref$square = _ref.square,
      square = _ref$square === void 0 ? index.defaultValues.square : _ref$square,
      _ref$disabledPages = _ref.disabledPages,
      disabledPages = _ref$disabledPages === void 0 ? index.defaultValues.disabledPages : _ref$disabledPages,
      _ref$count = _ref.count,
      count = _ref$count === void 0 ? index.defaultValues.value : _ref$count,
      _ref$hasQuickJump = _ref.hasQuickJump,
      hasQuickJump = _ref$hasQuickJump === void 0 ? index.defaultValues.hasQuickJump : _ref$hasQuickJump,
      _ref$hasPerPage = _ref.hasPerPage,
      hasPerPage = _ref$hasPerPage === void 0 ? index.defaultValues.hasPerPage : _ref$hasPerPage,
      _ref$perPageList = _ref.perPageList,
      perPageList = _ref$perPageList === void 0 ? index.defaultValues.perPageList : _ref$perPageList,
      _ref$isCommonButtonSt = _ref.isCommonButtonStyles,
      isCommonButtonStyles = _ref$isCommonButtonSt === void 0 ? index.defaultValues.isCommonButtonStyles : _ref$isCommonButtonSt,
      _ref$placeholderQuick = _ref.placeholderQuickJump,
      placeholderQuickJump = _ref$placeholderQuick === void 0 ? index.defaultValues.placeholderQuickJump : _ref$placeholderQuick,
      _ref$textQuickJump = _ref.textQuickJump,
      textQuickJump = _ref$textQuickJump === void 0 ? index.defaultValues.textQuickJump : _ref$textQuickJump,
      _ref$textPerPage = _ref.textPerPage,
      textPerPage = _ref$textPerPage === void 0 ? index.defaultValues.textPerPage : _ref$textPerPage,
      leftContent = _ref.leftContent,
      rightContent = _ref.rightContent,
      _ref$listWidth = _ref.listWidth,
      listWidth = _ref$listWidth === void 0 ? 'fit-content' : _ref$listWidth,
      onChangePageValue = _ref.onChangePageValue,
      onChangePerPageValue = _ref.onChangePerPageValue,
      onChange = _ref.onChange,
      onFocusQuickJump = _ref.onFocusQuickJump,
      onBlurQuickJump = _ref.onBlurQuickJump,
      rest = _rollupPluginBabelHelpers.objectWithoutProperties(_ref, _excluded);
    var _useState = React.useState(defaultValue || index.defaultValues.value),
      _useState2 = _rollupPluginBabelHelpers.slicedToArray(_useState, 2),
      pageInner = _useState2[0],
      setPageInner = _useState2[1];
    var pageValue = outerValue !== null && outerValue !== void 0 ? outerValue : pageInner;
    var _useState3 = React.useState(hasPerPage ? defaultPerPage || index.defaultValues.perPage : undefined),
      _useState4 = _rollupPluginBabelHelpers.slicedToArray(_useState3, 2),
      perPageInner = _useState4[0],
      setPerPageInner = _useState4[1];
    var perPageValue = outerPerPage !== null && outerPerPage !== void 0 ? outerPerPage : perPageInner;
    var pagesRef = React.useRef(null);
    var _useState5 = React.useState(0),
      _useState6 = _rollupPluginBabelHelpers.slicedToArray(_useState5, 2),
      pagesWidth = _useState6[0],
      setPagesWidth = _useState6[1];
    var _useState7 = React.useState(0),
      _useState8 = _rollupPluginBabelHelpers.slicedToArray(_useState7, 2),
      rootWidth = _useState8[0],
      setRootWidth = _useState8[1];
    var rootRef = React.useRef(null);
    var forkRef = useForkRef.useForkRef(rootRef, ref);
    var pages = Math.ceil(count / (perPageValue || 1));
    var sections = index.getSections(pageValue, pages, slots);
    var typeClass = Pagination_tokens.classes["".concat(type, "Type")];
    var roundedClass = pilled ? 'circle-circle' : 'square-square';
    var withHasPerPageSelect = hasPerPage && !helperText ? Pagination_tokens.classes.withHasPerPageSelect : undefined;
    var withHasQuickJump = hasQuickJump ? Pagination_tokens.classes.withHasQuickJump : undefined;
    var disabled = disabledPages.map(function (el) {
      return isNumber.isNumber(el) ? Number(el) : null;
    });
    var pageSetter = function pageSetter(pageValueCandidate) {
      var newPageValue = pageValueCandidate !== null && pageValueCandidate !== void 0 ? pageValueCandidate : index.defaultValues.value;
      if (newPageValue === pageValue) {
        return;
      }
      if (newPageValue > pages) {
        newPageValue = pages;
      }
      if (newPageValue < 1) {
        newPageValue = 1;
      }
      if (disabled.includes(newPageValue)) {
        return;
      }
      setPageInner(newPageValue);
      return newPageValue;
    };
    var handleSetPage = function handleSetPage(pageValueCandidate) {
      var newValue = pageSetter(pageValueCandidate);
      onChange === null || onChange === void 0 || onChange(newValue, perPageValue);
      onChangePageValue === null || onChangePageValue === void 0 || onChangePageValue(newValue);
    };
    var handlerSetPerPage = function handlerSetPerPage(newPerPageValue) {
      setPerPageInner(newPerPageValue);
      onChange === null || onChange === void 0 || onChange(pageValue, newPerPageValue);
      onChangePerPageValue === null || onChangePerPageValue === void 0 || onChangePerPageValue(newPerPageValue);
    };
    var handleClick = function handleClick(currentPage) {
      if (isActiveButton(currentPage)) {
        return;
      }
      handleSetPage(currentPage);
    };
    var isActiveButton = function isActiveButton(checkPage) {
      return pageValue === checkPage ? Pagination_tokens.classes.paginationPageButtonActive : '';
    };
    React.useEffect(function () {
      var observers = [];
      if (pagesRef.current) {
        var pagesObserver = new ResizeObserver(function (entries) {
          var newWidth = entries[0].contentRect.width;
          setPagesWidth(newWidth);
        });
        pagesObserver.observe(pagesRef.current);
        observers.push(pagesObserver);
      }
      if (rootRef.current) {
        var rootObserver = new ResizeObserver(function (entries) {
          var newWidth = entries[0].contentRect.width;
          setRootWidth(newWidth);
        });
        rootObserver.observe(rootRef.current);
        observers.push(rootObserver);
      }
      return function () {
        observers.forEach(function (observer) {
          return observer.disconnect();
        });
      };
    }, [type]);
    return /*#__PURE__*/React__default.default.createElement(Root, _rollupPluginBabelHelpers.extends({
      size: size,
      view: view,
      viewCurrentPage: viewCurrentPage,
      className: index$1.cx(Pagination_tokens.classes.wrapper, typeClass),
      ref: forkRef
    }, rest), /*#__PURE__*/React__default.default.createElement(Pagination_styles.PaginationRoot, {
      className: typeClass
    }, /*#__PURE__*/React__default.default.createElement(Pagination_styles.PaginationPages, {
      ref: pagesRef
    }, leftContent && /*#__PURE__*/React__default.default.createElement(Pagination_styles.PaginationButtonGroup, {
      isCommonButtonStyles: isCommonButtonStyles
    }, leftContent), sections && sections.map(function (section, indexSection) {
      return /*#__PURE__*/React__default.default.createElement(React.Fragment, {
        key: indexSection
      }, indexSection !== 0 && /*#__PURE__*/React__default.default.createElement(Pagination_styles.PaginationShorter, {
        stretching: "fixed",
        disabled: true,
        className: Pagination_tokens.classes.buttonDisabled
      }, "..."), /*#__PURE__*/React__default.default.createElement(Pagination_styles.PaginationSection, null, section.map(function (currentPage, index) {
        return /*#__PURE__*/React__default.default.createElement(Pagination_styles.PaginationButton, {
          key: "".concat(currentPage, "_").concat(index),
          className: index$1.cx(Pagination_tokens.classes.paginationPageButton, isActiveButton(currentPage), disabled.includes(currentPage) ? Pagination_tokens.classes.buttonDisabled : ''),
          pin: roundedClass,
          disabled: disabled.includes(currentPage),
          square: square,
          onClick: function onClick() {
            return handleClick(currentPage);
          }
        }, currentPage);
      })));
    }), rightContent && /*#__PURE__*/React__default.default.createElement(Pagination_styles.PaginationButtonGroup, {
      isCommonButtonStyles: isCommonButtonStyles
    }, rightContent)), /*#__PURE__*/React__default.default.createElement(Pagination_styles.PaginationActions, {
      style: type === 'compact' ? {
        minWidth: '0px'
      } : {
        width: "calc(100% - ".concat(pagesWidth, "px)")
      },
      className: index$1.cx(Pagination_tokens.classes.actions, typeClass, withHasPerPageSelect, withHasQuickJump)
    }, /*#__PURE__*/React__default.default.createElement(Pagination_styles.PaginationActionsLeft, {
      className: typeClass
    }, hasQuickJump && /*#__PURE__*/React__default.default.createElement(PaginationQuickJumpToPage.PaginationQuickJumpToPage, {
      prevPageValue: pageValue,
      placeholderQuickJump: placeholderQuickJump,
      textQuickJump: textQuickJump,
      onChangeValue: handleSetPage,
      onFocusQuickJump: onFocusQuickJump,
      onBlurQuickJump: onBlurQuickJump
    }), helperText && /*#__PURE__*/React__default.default.createElement(Pagination_styles.PaginationHelperText, {
      className: index$1.cx(typeClass, singleLine ? Pagination_tokens.classes.ellipsisText : ''),
      style: {
        maxWidth: rootWidth
      }
    }, helperText)), hasPerPage && /*#__PURE__*/React__default.default.createElement(PaginationSelectPerPage.PaginationSelectPerPage, {
      textPerPage: textPerPage,
      perPageValue: perPageValue,
      perPageList: perPageList,
      onChangeValue: handlerSetPerPage,
      size: size,
      listWidth: listWidth
    }))));
  });
};
var paginationConfig = {
  name: 'Pagination',
  tag: 'div',
  layout: paginationRoot,
  base: '',
  variations: {
    view: {
      css: base.base
    },
    size: {
      css: base$1.base
    },
    type: {
      css: base$2.base
    }
  },
  defaults: {
    view: 'clear',
    size: 'm'
  }
};

exports.paginationConfig = paginationConfig;
exports.paginationRoot = paginationRoot;
