var _excluded = ["value", "onChange", "page", "pageSize"];
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 = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) 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; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import React, { useEffect, useMemo, useState } from "react";
// import { ArrowLeft, ArrowRight } from "@pisell/icon/es";
import ArrowRight from "@pisell/icon/es/ArrowRight";
import ArrowLeft from "@pisell/icon/es/ArrowLeft";
import { Pagination as OriginPagination } from "antd";
import classNames from "classnames";
import "./index.less";
import { getText } from "../../locales";
import useIsMobileTable from "../table/hooks/useIsMobileTable";

/**
 * @title: 自定义渲染
 * @description:
 * @return {*}
 * @Author: zhiwei.Wang
 * @Date: 2023-07-20 17:01
 */
var itemRender = function itemRender(_, type, originalElement) {
  if (type === "prev") {
    return /*#__PURE__*/React.createElement("div", {
      className: classNames("paginator-page-btn", "left")
    }, /*#__PURE__*/React.createElement(ArrowLeft, {
      className: "paginator-page-btn-left-icon"
    }), /*#__PURE__*/React.createElement("span", {
      className: "paginator-page-btn-text"
    }, getText("table-pagination-previous")));
  }
  if (type === "next") {
    return /*#__PURE__*/React.createElement("div", {
      className: classNames("paginator-page-btn", "right")
    }, /*#__PURE__*/React.createElement("span", {
      className: "paginator-page-btn-text"
    }, getText("table-pagination-next")), /*#__PURE__*/React.createElement(ArrowRight, {
      className: "paginator-page-btn-right-icon"
    }));
  }
  return originalElement;
};

// 分页配置
export var paginationConfig = {
  className: "materials-grid-paginator",
  defaultCurrent: 1,
  showTotal: function showTotal(total, range) {
    return "Page ".concat(range[0], " of ").concat(range[1]);
  },
  itemRender: itemRender,
  responsive: true,
  size: "small"
};
var Pagination = function Pagination(props) {
  var show = useMemo(function () {
    return Object.keys(props).length;
  }, [props]);
  var isMiniPagination = useIsMobileTable();
  var content = useMemo(function () {
    if (!show) {
      return null;
    }
    var Com = OriginPagination;
    // 移动端使用简单分页
    if (isMiniPagination) {
      Com = MiniPagination;
    }
    return /*#__PURE__*/React.createElement(Com, _extends({}, paginationConfig, props));
  }, [show, paginationConfig, props, isMiniPagination]);
  return content;
};
export default Pagination;
var MiniPagination = function MiniPagination(props) {
  var _props$current = props.current,
    current = _props$current === void 0 ? 1 : _props$current,
    _props$pageSize = props.pageSize,
    pageSize = _props$pageSize === void 0 ? 10 : _props$pageSize,
    _props$total = props.total,
    total = _props$total === void 0 ? 0 : _props$total;

  // 最大翻页
  var MAX = useMemo(function () {
    return Math.ceil(total / pageSize);
  }, [total, pageSize]);

  // 最小翻页
  var MIN = useMemo(function () {
    return 1;
  }, []);

  /**
   * @title: 翻页回调
   * @description:
   * @param {*} type
   * @return {*}
   * @Author: zhiwei.Wang
   * @Date: 2023-07-20 17:02
   */
  var onChange = function onChange(type) {
    var currentPage = current;
    if (type === "prev") {
      currentPage = current - 1;
    }
    if (type === "next") {
      currentPage = current + 1;
    }
    props.onChange && props.onChange(currentPage, pageSize || 10);
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "materials-grid-paginator materials-grid-paginator-mini"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode-pagination-prev",
    onClick: function onClick() {
      if (current <= MIN) {
        return;
      }
      onChange("prev");
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: classNames("paginator-page-btn", "left")
  }, /*#__PURE__*/React.createElement(ArrowLeft, {
    className: "paginator-page-btn-left-icon"
  }))), /*#__PURE__*/React.createElement("div", {
    className: "materials-grid-paginator-center"
  }, /*#__PURE__*/React.createElement("span", {
    className: "materials-grid-paginator-center-text"
  }, getText("table-pagination-total")(current, MAX))), /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode-pagination-next",
    onClick: function onClick() {
      if (current >= MAX) {
        return;
      }
      onChange("next");
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: classNames("paginator-page-btn", "right")
  }, /*#__PURE__*/React.createElement(ArrowRight, {
    className: "paginator-page-btn-right-icon"
  }))));
};
export var FormPagination = function FormPagination(_ref) {
  var _ref$value = _ref.value,
    value = _ref$value === void 0 ? {} : _ref$value,
    onChange = _ref.onChange,
    page = _ref.page,
    pageSize = _ref.pageSize,
    other = _objectWithoutProperties(_ref, _excluded);
  var _useState = useState(page || 1),
    _useState2 = _slicedToArray(_useState, 2),
    _p = _useState2[0],
    _setP = _useState2[1];
  var _useState3 = useState(pageSize || 10),
    _useState4 = _slicedToArray(_useState3, 2),
    _ps = _useState4[0],
    _setPs = _useState4[1];
  var _onChange = function _onChange(p, ps) {
    onChange && onChange({
      page: ps === _ps ? p : 1,
      size: ps
    });
  };
  useEffect(function () {
    var _page = (value === null || value === void 0 ? void 0 : value.page) || page || 1;
    var _size = (value === null || value === void 0 ? void 0 : value.size) || pageSize || 10;
    _setP(_page);
    _setPs(_size);
  }, [value, page, pageSize]);
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Pagination, _extends({}, other, {
    pageSize: _ps,
    current: _p,
    onChange: _onChange,
    onShowSizeChange: _onChange
  })));
};