import { objectWithoutProperties as _objectWithoutProperties, slicedToArray as _slicedToArray, extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
import React__default, { forwardRef, useState, useRef, useEffect, Fragment } from 'react';
import { cx } from '../../utils/index.js';
import { PaginationRoot, PaginationPages, PaginationButtonGroup, PaginationShorter, PaginationSection, PaginationButton, PaginationActions, PaginationActionsLeft, PaginationHelperText } from './Pagination.styles.js';
import { defaultValues, getSections } from './utils/index.js';
import { base } from './variations/_view/base.js';
import { base as base$2 } from './variations/_type/base.js';
import { base as base$1 } from './variations/_size/base.js';
import { classes } from './Pagination.tokens.js';
import { PaginationQuickJumpToPage } from './ui/PaginationQuickJumpToPage/PaginationQuickJumpToPage.js';
import { PaginationSelectPerPage } from './ui/PaginationSelectPerPage/PaginationSelectPerPage.js';
import { useForkRef } from '../../hooks/useForkRef.js';
import { isNumber } from '../../utils/isNumber.js';

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__*/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 ? defaultValues.slots : _ref$slots,
      view = _ref.view,
      viewCurrentPage = _ref.viewCurrentPage,
      _ref$type = _ref.type,
      type = _ref$type === void 0 ? defaultValues.type : _ref$type,
      size = _ref.size,
      _ref$pilled = _ref.pilled,
      pilled = _ref$pilled === void 0 ? defaultValues.pilled : _ref$pilled,
      _ref$square = _ref.square,
      square = _ref$square === void 0 ? defaultValues.square : _ref$square,
      _ref$disabledPages = _ref.disabledPages,
      disabledPages = _ref$disabledPages === void 0 ? defaultValues.disabledPages : _ref$disabledPages,
      _ref$count = _ref.count,
      count = _ref$count === void 0 ? defaultValues.value : _ref$count,
      _ref$hasQuickJump = _ref.hasQuickJump,
      hasQuickJump = _ref$hasQuickJump === void 0 ? defaultValues.hasQuickJump : _ref$hasQuickJump,
      _ref$hasPerPage = _ref.hasPerPage,
      hasPerPage = _ref$hasPerPage === void 0 ? defaultValues.hasPerPage : _ref$hasPerPage,
      _ref$perPageList = _ref.perPageList,
      perPageList = _ref$perPageList === void 0 ? defaultValues.perPageList : _ref$perPageList,
      _ref$isCommonButtonSt = _ref.isCommonButtonStyles,
      isCommonButtonStyles = _ref$isCommonButtonSt === void 0 ? defaultValues.isCommonButtonStyles : _ref$isCommonButtonSt,
      _ref$placeholderQuick = _ref.placeholderQuickJump,
      placeholderQuickJump = _ref$placeholderQuick === void 0 ? defaultValues.placeholderQuickJump : _ref$placeholderQuick,
      _ref$textQuickJump = _ref.textQuickJump,
      textQuickJump = _ref$textQuickJump === void 0 ? defaultValues.textQuickJump : _ref$textQuickJump,
      _ref$textPerPage = _ref.textPerPage,
      textPerPage = _ref$textPerPage === void 0 ? 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 = _objectWithoutProperties(_ref, _excluded);
    var _useState = useState(defaultValue || defaultValues.value),
      _useState2 = _slicedToArray(_useState, 2),
      pageInner = _useState2[0],
      setPageInner = _useState2[1];
    var pageValue = outerValue !== null && outerValue !== void 0 ? outerValue : pageInner;
    var _useState3 = useState(hasPerPage ? defaultPerPage || defaultValues.perPage : undefined),
      _useState4 = _slicedToArray(_useState3, 2),
      perPageInner = _useState4[0],
      setPerPageInner = _useState4[1];
    var perPageValue = outerPerPage !== null && outerPerPage !== void 0 ? outerPerPage : perPageInner;
    var pagesRef = useRef(null);
    var _useState5 = useState(0),
      _useState6 = _slicedToArray(_useState5, 2),
      pagesWidth = _useState6[0],
      setPagesWidth = _useState6[1];
    var _useState7 = useState(0),
      _useState8 = _slicedToArray(_useState7, 2),
      rootWidth = _useState8[0],
      setRootWidth = _useState8[1];
    var rootRef = useRef(null);
    var forkRef = useForkRef(rootRef, ref);
    var pages = Math.ceil(count / (perPageValue || 1));
    var sections = getSections(pageValue, pages, slots);
    var typeClass = classes["".concat(type, "Type")];
    var roundedClass = pilled ? 'circle-circle' : 'square-square';
    var withHasPerPageSelect = hasPerPage && !helperText ? classes.withHasPerPageSelect : undefined;
    var withHasQuickJump = hasQuickJump ? classes.withHasQuickJump : undefined;
    var disabled = disabledPages.map(function (el) {
      return isNumber(el) ? Number(el) : null;
    });
    var pageSetter = function pageSetter(pageValueCandidate) {
      var newPageValue = pageValueCandidate !== null && pageValueCandidate !== void 0 ? pageValueCandidate : 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 ? classes.paginationPageButtonActive : '';
    };
    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.createElement(Root, _extends({
      size: size,
      view: view,
      viewCurrentPage: viewCurrentPage,
      className: cx(classes.wrapper, typeClass),
      ref: forkRef
    }, rest), /*#__PURE__*/React__default.createElement(PaginationRoot, {
      className: typeClass
    }, /*#__PURE__*/React__default.createElement(PaginationPages, {
      ref: pagesRef
    }, leftContent && /*#__PURE__*/React__default.createElement(PaginationButtonGroup, {
      isCommonButtonStyles: isCommonButtonStyles
    }, leftContent), sections && sections.map(function (section, indexSection) {
      return /*#__PURE__*/React__default.createElement(Fragment, {
        key: indexSection
      }, indexSection !== 0 && /*#__PURE__*/React__default.createElement(PaginationShorter, {
        stretching: "fixed",
        disabled: true,
        className: classes.buttonDisabled
      }, "..."), /*#__PURE__*/React__default.createElement(PaginationSection, null, section.map(function (currentPage, index) {
        return /*#__PURE__*/React__default.createElement(PaginationButton, {
          key: "".concat(currentPage, "_").concat(index),
          className: cx(classes.paginationPageButton, isActiveButton(currentPage), disabled.includes(currentPage) ? classes.buttonDisabled : ''),
          pin: roundedClass,
          disabled: disabled.includes(currentPage),
          square: square,
          onClick: function onClick() {
            return handleClick(currentPage);
          }
        }, currentPage);
      })));
    }), rightContent && /*#__PURE__*/React__default.createElement(PaginationButtonGroup, {
      isCommonButtonStyles: isCommonButtonStyles
    }, rightContent)), /*#__PURE__*/React__default.createElement(PaginationActions, {
      style: type === 'compact' ? {
        minWidth: '0px'
      } : {
        width: "calc(100% - ".concat(pagesWidth, "px)")
      },
      className: cx(classes.actions, typeClass, withHasPerPageSelect, withHasQuickJump)
    }, /*#__PURE__*/React__default.createElement(PaginationActionsLeft, {
      className: typeClass
    }, hasQuickJump && /*#__PURE__*/React__default.createElement(PaginationQuickJumpToPage, {
      prevPageValue: pageValue,
      placeholderQuickJump: placeholderQuickJump,
      textQuickJump: textQuickJump,
      onChangeValue: handleSetPage,
      onFocusQuickJump: onFocusQuickJump,
      onBlurQuickJump: onBlurQuickJump
    }), helperText && /*#__PURE__*/React__default.createElement(PaginationHelperText, {
      className: cx(typeClass, singleLine ? classes.ellipsisText : ''),
      style: {
        maxWidth: rootWidth
      }
    }, helperText)), hasPerPage && /*#__PURE__*/React__default.createElement(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
    },
    size: {
      css: base$1
    },
    type: {
      css: base$2
    }
  },
  defaults: {
    view: 'clear',
    size: 'm'
  }
};

export { paginationConfig, paginationRoot };
