var _excluded = ["className", "onChange", "total", "pageSize", "current", "pageSizeList", "pageSizeSelector", "onPageSizeChange", "responsivePaginationType"];
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _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(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
// 自适应宽度的翻页器
import React, { useState, useEffect } from 'react';
import { Pagination, Balloon, Select, Button, Input, Icon } from '@alicloudfe/components';
import { getMessage, cls, usePrefixCls } from '@teamix/utils';
import { useResponsiveProps } from '@teamix/hooks';
import './index.scss';
export default (function (props) {
  var className = props.className,
    propsOnChange = props.onChange,
    total = props.total,
    pageSize = props.pageSize,
    current = props.current,
    pageSizeList = props.pageSizeList,
    pageSizeSelector = props.pageSizeSelector,
    onPageSizeChange = props.onPageSizeChange,
    responsivePaginationType = props.responsivePaginationType,
    otherProps = _objectWithoutProperties(props, _excluded);
  var basePrefix = usePrefixCls('', {
    base: true
  });
  var prefixCls = usePrefixCls('teamix-pro-pagination');
  var _useResponsiveProps = useResponsiveProps([480, 590, 700], ['mini', 'small', 'simple', 'normal']),
    responsiveProps = _useResponsiveProps.responsiveProps,
    paginationWrapperRef = _useResponsiveProps.ref;
  useEffect(function () {
    // size='mini' 模式下 current 受控
    if (responsiveProps === 'mini') {
      setCurrentPage(current);
    }
  }, [current]);
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    visible = _useState2[0],
    setVisible = _useState2[1];
  var _useState3 = useState(current),
    _useState4 = _slicedToArray(_useState3, 2),
    currentPage = _useState4[0],
    setCurrentPage = _useState4[1];
  var commonPaginationProps = {
    className: className,
    onChange: propsOnChange,
    total: total,
    pageSize: pageSize,
    current: current,
    shape: 'arrow-only',
    pageSizeList: pageSizeList,
    pageSizeSelector: pageSizeSelector,
    pageSizePosition: 'end',
    onPageSizeChange: onPageSizeChange
  };
  // 自定义总数，用于 small、mini
  var customTotalRender = function customTotalRender(total) {
    return /*#__PURE__*/React.createElement(Balloon, {
      visible: visible,
      closable: false,
      onVisibleChange: function onVisibleChange(visible) {
        return setVisible(visible);
      },
      align: "t",
      // triggerType="click"
      trigger: /*#__PURE__*/React.createElement(Button, {
        text: true,
        onMouseEnter: function onMouseEnter() {
          return setVisible(true);
        },
        style: {
          lineHeight: '18px'
        }
      }, getMessage('total', {
        total: new Intl.NumberFormat().format(total)
      }))
    }, /*#__PURE__*/React.createElement("div", {
      className: prefixCls('custom-total-content')
    }, /*#__PURE__*/React.createElement("div", {
      className: "title"
    }, getMessage('pageSize')), /*#__PURE__*/React.createElement(Select, {
      className: prefixCls('custom-page-size-selector'),
      dataSource: pageSizeList,
      defaultValue: pageSize,
      followTrigger: true,
      menuProps: {
        hasSelectedIcon: false
      },
      onChange: function onChange(value) {
        setVisible(false);
        setCurrentPage(1);
        onPageSizeChange === null || onPageSizeChange === void 0 ? void 0 : onPageSizeChange(value);
      }
    })));
  };
  // 获取页总数
  function getTotalPage(total, currentPageSize) {
    var totalPage = Math.ceil(total / currentPageSize);
    return totalPage <= 0 ? 1 : totalPage;
  }
  // 自定义翻页，用于 mini
  var customJumpRender = function customJumpRender() {
    var handleInputChange = function handleInputChange(value, e) {
      setCurrentPage(value);
    };
    // 回车触发跳转
    var handleJump = function handleJump(e) {
      var totalPage = getTotalPage(total, pageSize);
      var value = Number(currentPage);
      if (isNaN(value)) {
        value = '';
      } else if (value < 1) {
        value = 1;
      } else if (value > totalPage) {
        value = totalPage;
      }
      if (value && value !== current) {
        setCurrentPage(value);
        propsOnChange === null || propsOnChange === void 0 ? void 0 : propsOnChange(value, e);
      }
    };
    var handlePrevClick = function handlePrevClick(e) {
      setCurrentPage(currentPage - 1);
      propsOnChange === null || propsOnChange === void 0 ? void 0 : propsOnChange(currentPage - 1, e);
    };
    var handleNextClick = function handleNextClick(e) {
      setCurrentPage(currentPage + 1);
      propsOnChange === null || propsOnChange === void 0 ? void 0 : propsOnChange(currentPage + 1, e);
    };
    return /*#__PURE__*/React.createElement("div", {
      className: prefixCls('custom-jumper')
    }, /*#__PURE__*/React.createElement(Button, {
      className: basePrefix('prev', 'pagination-item'),
      onClick: handlePrevClick,
      disabled: Number(currentPage) <= 1
    }, /*#__PURE__*/React.createElement(Icon, {
      size: "small",
      type: "arrow-left"
    })), /*#__PURE__*/React.createElement(Input, {
      className: "jump-input",
      value: currentPage,
      onChange: handleInputChange,
      onKeyDown: function onKeyDown(e) {
        if (e.keyCode === 13) {
          handleJump(e);
        }
      }
    }), /*#__PURE__*/React.createElement("div", {
      className: "total"
    }, /*#__PURE__*/React.createElement("span", {
      style: {
        marginRight: 4
      }
    }, "/"), getTotalPage(total, pageSize)), /*#__PURE__*/React.createElement(Button, {
      className: basePrefix('next', 'pagination-item'),
      onClick: handleNextClick,
      //@ts-ignore
      disabled: total && Number(currentPage) >= getTotalPage(total, pageSize)
    }, /*#__PURE__*/React.createElement(Icon, {
      size: "small",
      type: "arrow-right"
    })));
  };
  var renderMiniPagination = function renderMiniPagination() {
    if (total && pageSize) {
      if (total / pageSize <= 2) {
        return /*#__PURE__*/React.createElement(Pagination, _objectSpread(_objectSpread({}, commonPaginationProps), {}, {
          type: "simple"
        }, otherProps));
      } else {
        return /*#__PURE__*/React.createElement("div", {
          className: cls(basePrefix('pagination'), prefixCls('custom-mini-pagination'))
        }, customTotalRender(total), customJumpRender());
      }
    }
  };
  var renderPagination = function renderPagination() {
    switch (responsivePaginationType || responsiveProps) {
      case 'normal':
        return /*#__PURE__*/React.createElement(Pagination, _objectSpread(_objectSpread({}, commonPaginationProps), {}, {
          totalRender: function totalRender(total) {
            return getMessage('total', {
              total: new Intl.NumberFormat().format(total)
            });
          }
        }, otherProps));
      case 'simple':
        return /*#__PURE__*/React.createElement(Pagination, _objectSpread(_objectSpread({}, commonPaginationProps), {}, {
          pageShowCount: 3,
          totalRender: function totalRender(total) {
            return getMessage('total', {
              total: new Intl.NumberFormat().format(total)
            });
          }
        }, otherProps));
      case 'small':
        return /*#__PURE__*/React.createElement(Pagination, _objectSpread(_objectSpread({}, commonPaginationProps), {}, {
          pageShowCount: 3,
          pageSizeSelector: false,
          totalRender: function totalRender(total) {
            return customTotalRender(total);
          }
        }, otherProps));
      case 'mini':
        return renderMiniPagination();
      default:
        return /*#__PURE__*/React.createElement(Pagination, _objectSpread(_objectSpread({}, commonPaginationProps), otherProps));
    }
  };
  return (
    /*#__PURE__*/
    // 用于获取翻页器响应式 ref
    React.createElement("div", {
      ref: paginationWrapperRef,
      className: prefixCls('wrapper')
    }, renderPagination())
  );
});