/**
 * tdesign v0.46.4
 * (c) 2022 tdesign
 * @license MIT
 */

import _typeof from '@babel/runtime/helpers/typeof';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { PageFirstIcon, PageLastIcon, ChevronLeftIcon, ChevronRightIcon, ChevronLeftDoubleIcon, ChevronRightDoubleIcon, EllipsisIcon } from 'tdesign-icons-vue';
import mixins from '../utils/mixins.js';
import getConfigReceiverMixins, { getGlobalIconMixins } from '../config-provider/config-receiver.js';
import { InputNumber } from '../input-number/index.js';
import { Select, Option } from '../select/index.js';
import { InputAdornment } from '../input-adornment/index.js';
import { renderTNodeJSX } from '../utils/render-tnode.js';
import props from './props.js';
import { emitEvent } from '../utils/event.js';
import 'vue';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import 'lodash/merge';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '../config-provider/type.js';
import '@vue/composition-api';
import '../input-number/input-number.js';
import '../_chunks/dep-a4bd011a.js';
import '../button/index.js';
import '../button/button.js';
import '../loading/index.js';
import '../loading/loading.js';
import '../loading/icon/gradient.js';
import '../_common/js/loading/circle-adapter.js';
import '../_common/js/utils/set-style.js';
import '../_common/js/utils/helper.js';
import '@babel/runtime/helpers/toConsumableArray';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import '../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../utils/easing.js';
import '../utils/transfer-dom.js';
import '../loading/props.js';
import '../utils/withInstall.js';
import 'lodash/capitalize';
import './style/index.js';
import '../loading/type.js';
import '../loading/plugin.js';
import '../config.js';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../button/props.js';
import '../utils/ripple.js';
import '../button/type.js';
import '../input/index.js';
import '../input/input.js';
import '../utils/helper.js';
import '../input/props.js';
import '../input/input-group.js';
import '../utils/map-props.js';
import '../input/type.js';
import '../input-number/props.js';
import '../input-number/useInputNumber.js';
import '../hooks/useCommonClassName.js';
import '../config-provider/useConfig.js';
import '../hooks/useVModel.js';
import '../_common/js/input-number/number.js';
import 'lodash/isNumber';
import '../_common/js/input-number/large-number.js';
import '../_common/js/log/log.js';
import '../_common/js/log/index.js';
import '../hooks/useFormDisabled.js';
import '../hooks/useGlobalIcon.js';
import '../hooks/useConfig.js';
import '../input-number/type.js';
import '../select/select.js';
import 'lodash/isFunction';
import 'lodash/debounce';
import 'lodash/get';
import 'lodash/cloneDeep';
import 'lodash/isArray';
import '../hooks/useDefaultValue.js';
import '../hooks/tnode.js';
import 'lodash/isEmpty';
import '../hooks/render-tnode.js';
import 'lodash/isObject';
import '../select/props.js';
import '../popup/index.js';
import '../popup/popup.js';
import '@popperjs/core';
import '../popup/props.js';
import '../popup/container.js';
import '../popup/type.js';
import '../tag/index.js';
import '../tag/tag.js';
import '../tag/props.js';
import '../tag/check-tag.js';
import '../tag/check-tag-props.js';
import '../tag/type.js';
import '../select-input/index.js';
import '../select-input/select-input.js';
import '../select-input/props.js';
import '../select-input/useSingle.js';
import 'lodash/pick';
import '../select-input/useMultiple.js';
import '../tag-input/index.js';
import '../tag-input/tag-input.js';
import '../tag-input/props.js';
import '../tag-input/hooks/useTagScroll.js';
import '../tag-input/useTagList.js';
import '../tag-input/hooks/useHover.js';
import '../tag-input/hooks/useDragSorter.js';
import '../tag-input/type.js';
import '../select-input/useOverlayInnerStyle.js';
import '../select-input/type.js';
import '../common-components/fake-arrow.js';
import '../select/option.js';
import '../select/option-props.js';
import '../checkbox/index.js';
import '../checkbox/group.js';
import 'lodash/intersection';
import '../checkbox/checkbox.js';
import '../checkbox/props.js';
import '../checkbox/checkbox-group-props.js';
import '../checkbox/type.js';
import '../hooks/useLazyLoad.js';
import '../_common/js/utils/observe.js';
import '../select/util.js';
import '../select/select-panel.js';
import '../hooks/useVirtualScroll.js';
import '../select/hooks/useSelectOptions.js';
import '../select/optionGroup.js';
import '../select/option-group-props.js';
import '../select/type.js';
import '../input-adornment/input-adornment.js';
import '../input-adornment/props.js';
import '../input-adornment/type.js';

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; }
var min = 1;
var PaginationBase = mixins(getConfigReceiverMixins("pagination"), getGlobalIconMixins()).extend({
  name: "TPagination",
  components: {
    TInputNumber: InputNumber,
    TInputAdornment: InputAdornment,
    TSelect: Select,
    TOption: Option
  },
  props: _objectSpread(_objectSpread({}, props), {}, {
    current: {
      type: Number,
      "default": 1,
      validator: function validator(v) {
        return v > 0;
      }
    },
    pageSize: {
      type: Number,
      "default": 10,
      validator: function validator(v) {
        return v > 0;
      }
    }
  }),
  data: function data() {
    return {
      prevMore: false,
      nextMore: false,
      jumpIndex: this.current
    };
  },
  watch: {
    current: function current(val) {
      this.jumpIndex = val;
    }
  },
  computed: {
    paginationClass: function paginationClass() {
      return ["".concat(this.componentName), this.commonSizeClassName[this.size], _defineProperty({}, this.commonStatusClassName.disabled, this.disabled)];
    },
    totalClass: function totalClass() {
      return ["".concat(this.componentName, "__total")];
    },
    sizerClass: function sizerClass() {
      return ["".concat(this.componentName, "__select")];
    },
    preBtnClass: function preBtnClass() {
      return ["".concat(this.componentName, "__btn"), "".concat(this.componentName, "__btn-prev"), _defineProperty({}, this.commonStatusClassName.disabled, this.disabled || this.current === 1)];
    },
    nextBtnClass: function nextBtnClass() {
      return ["".concat(this.componentName, "__btn"), "".concat(this.componentName, "__btn-next"), _defineProperty({}, this.commonStatusClassName.disabled, this.disabled || this.current === this.pageCount)];
    },
    btnWrapClass: function btnWrapClass() {
      return ["".concat(this.componentName, "__pager")];
    },
    btnMoreClass: function btnMoreClass() {
      return ["".concat(this.componentName, "__number"), "".concat(this.componentName, "__number--more"), _defineProperty({}, this.commonStatusClassName.disabled, this.disabled)];
    },
    jumperClass: function jumperClass() {
      return ["".concat(this.componentName, "__jump")];
    },
    jumperInputClass: function jumperInputClass() {
      return ["".concat(this.componentName, "__input")];
    },
    simpleClass: function simpleClass() {
      return ["".concat(this.componentName, "__select")];
    },
    pageCount: function pageCount() {
      var c = Math.ceil(this.total / this.pageSize);
      return c > 0 ? c : 1;
    },
    pageCountOption: function pageCountOption() {
      var ans = [];

      for (var i = 1; i <= this.pageCount; i++) {
        ans.push({
          value: i,
          label: "".concat(i, "/").concat(this.pageCount)
        });
      }

      return ans;
    },
    sizeOptions: function sizeOptions() {
      var _this = this;

      var options = this.pageSizeOptions.map(function (option) {
        return _typeof(option) === "object" ? option : {
          label: _this.t(_this.global.itemsPerPage, {
            size: option
          }),
          value: Number(option)
        };
      });
      return options.sort(function (a, b) {
        return a.value - b.value;
      });
    },
    curPageLeftCount: function curPageLeftCount() {
      return Math.ceil((this.foldedMaxPageBtn - 1) / 2);
    },
    curPageRightCount: function curPageRightCount() {
      return Math.ceil((this.foldedMaxPageBtn - 1) / 2);
    },
    isPrevMoreShow: function isPrevMoreShow() {
      return 2 + this.curPageLeftCount < this.current;
    },
    isNextMoreShow: function isNextMoreShow() {
      return this.pageCount - 1 - this.curPageRightCount > this.current;
    },
    pages: function pages() {
      var array = [];
      var start;
      var end;

      if (this.isFolded) {
        if (this.isPrevMoreShow && this.isNextMoreShow) {
          start = this.current - this.curPageLeftCount;
          end = this.current + this.curPageRightCount;
        } else {
          var foldedStart = this.isMidEllipsis ? 2 : 1;
          var foldedEnd = this.isMidEllipsis ? this.pageCount - 1 : this.pageCount;
          start = this.isPrevMoreShow ? this.pageCount - this.foldedMaxPageBtn + 1 : foldedStart;
          end = this.isPrevMoreShow ? foldedEnd : this.foldedMaxPageBtn;
        }
      } else {
        start = 1;
        end = this.pageCount;
      }

      for (var i = start; i <= end; i++) {
        array.push(i);
      }

      return array;
    },
    isFolded: function isFolded() {
      return this.pageCount > this.maxPageBtn;
    },
    isMidEllipsis: function isMidEllipsis() {
      return this.pageEllipsisMode === "mid";
    }
  },
  methods: {
    toPage: function toPage(pageIndex, isTriggerChange) {
      if (this.disabled) {
        return;
      }

      var current = pageIndex;

      if (pageIndex < min) {
        current = min;
      } else if (pageIndex > this.pageCount) {
        current = this.pageCount;
      }

      if (this.current !== current) {
        var prev = this.current;
        var pageInfo = {
          current: current,
          previous: prev,
          pageSize: this.pageSize
        };
        emitEvent(this, "current-change", current, pageInfo);

        if (isTriggerChange !== false) {
          emitEvent(this, "change", pageInfo);
        }
      }
    },
    prevPage: function prevPage() {
      this.toPage(this.current - 1);
    },
    nextPage: function nextPage() {
      this.toPage(this.current + 1);
    },
    prevMorePage: function prevMorePage() {
      this.toPage(this.current - this.foldedMaxPageBtn);
    },
    nextMorePage: function nextMorePage() {
      this.toPage(this.current + this.foldedMaxPageBtn);
    },
    getButtonClass: function getButtonClass(index) {
      var _ref5;

      return ["".concat(this.componentName, "__number"), (_ref5 = {}, _defineProperty(_ref5, this.commonStatusClassName.disabled, this.disabled), _defineProperty(_ref5, this.commonStatusClassName.current, this.current === index), _ref5)];
    },
    onSelectorChange: function onSelectorChange(e) {
      if (this.disabled) {
        return;
      }

      var pageSize = parseInt(e, 10);
      var pageCount = 1;

      if (pageSize > 0) {
        pageCount = Math.ceil(this.total / pageSize);
      }

      var isIndexChange = false;

      if (this.current > pageCount) {
        isIndexChange = true;
      }

      var pageInfo = {
        current: isIndexChange ? pageCount : this.current,
        previous: this.current,
        pageSize: pageSize
      };
      this.$emit("update:pageSize", pageSize, pageInfo);
      emitEvent(this, "page-size-change", pageSize, pageInfo);
      emitEvent(this, "change", pageInfo);

      if (isIndexChange) {
        this.toPage(pageCount, false);
      }
    },
    onJumperChange: function onJumperChange(val) {
      var currentIndex = Number(val);
      if (isNaN(currentIndex)) return;
      this.toPage(currentIndex);
    }
  },
  render: function render() {
    var _this2 = this;

    var h = arguments[0];

    var _this$useGlobalIcon = this.useGlobalIcon({
      PageFirstIcon: PageFirstIcon,
      PageLastIcon: PageLastIcon,
      ChevronLeftIcon: ChevronLeftIcon,
      ChevronRightIcon: ChevronRightIcon,
      ChevronLeftDoubleIcon: ChevronLeftDoubleIcon,
      ChevronRightDoubleIcon: ChevronRightDoubleIcon,
      EllipsisIcon: EllipsisIcon
    }),
        PageFirstIcon$1 = _this$useGlobalIcon.PageFirstIcon,
        PageLastIcon$1 = _this$useGlobalIcon.PageLastIcon,
        ChevronLeftIcon$1 = _this$useGlobalIcon.ChevronLeftIcon,
        ChevronRightIcon$1 = _this$useGlobalIcon.ChevronRightIcon,
        ChevronLeftDoubleIcon$1 = _this$useGlobalIcon.ChevronLeftDoubleIcon,
        ChevronRightDoubleIcon$1 = _this$useGlobalIcon.ChevronRightDoubleIcon,
        EllipsisIcon$1 = _this$useGlobalIcon.EllipsisIcon;

    var Jumper = h("div", {
      "class": this.jumperClass
    }, [this.t(this.global.jumpTo), h("t-input-adornment", {
      "attrs": {
        "append": "/ ".concat(this.pageCount, " ").concat(this.t(this.global.page))
      }
    }, [h("t-input-number", {
      "class": this.jumperInputClass,
      "on": {
        "blur": this.onJumperChange,
        "enter": this.onJumperChange
      },
      "attrs": {
        "max": this.pageCount,
        "min": min,
        "size": this.size,
        "theme": "normal",
        "placeholder": ""
      },
      "model": {
        value: _this2.jumpIndex,
        callback: function callback($$v) {
          _this2.jumpIndex = $$v;
        }
      }
    })])]);
    return h("div", {
      "class": this.paginationClass
    }, [renderTNodeJSX(this, "totalContent", h("div", {
      "class": this.totalClass
    }, [this.t(this.global.total, {
      total: this.total
    })])), this.showPageSize && this.pageSizeOptions.length ? h("t-select", {
      "attrs": {
        "size": this.size,
        "value": this.pageSize,
        "disabled": this.disabled,
        "autoWidth": true
      },
      "class": this.sizerClass,
      "on": {
        "change": this.onSelectorChange
      }
    }, [this.sizeOptions.map(function (item, index) {
      return h("t-option", {
        "attrs": {
          "value": item.value,
          "label": item.label
        },
        "key": index
      });
    })]) : null, this.showFirstAndLastPageBtn ? h("div", {
      "class": this.preBtnClass,
      "on": {
        "click": function click() {
          return _this2.toPage(1);
        }
      },
      "attrs": {
        "disabled": this.disabled || this.current === min
      }
    }, [h(PageFirstIcon$1)]) : null, this.showPreviousAndNextBtn ? h("div", {
      "class": this.preBtnClass,
      "on": {
        "click": this.prevPage
      },
      "attrs": {
        "disabled": this.disabled || this.current === min
      }
    }, [h(ChevronLeftIcon$1)]) : null, this.showPageNumber && this.theme === "default" ? h("ul", {
      "class": this.btnWrapClass
    }, [this.isFolded && this.isMidEllipsis ? h("li", {
      "class": this.getButtonClass(1),
      "on": {
        "click": function click() {
          return _this2.toPage(min);
        }
      }
    }, [min]) : null, this.isFolded && this.isPrevMoreShow && this.isMidEllipsis ? h("li", {
      "class": this.btnMoreClass,
      "on": {
        "click": this.prevMorePage,
        "mouseover": function mouseover() {
          return _this2.prevMore = true;
        },
        "mouseout": function mouseout() {
          return _this2.prevMore = false;
        }
      }
    }, [this.prevMore ? h(ChevronLeftDoubleIcon$1) : h(EllipsisIcon$1)]) : null, this.pages.map(function (i) {
      return h("li", {
        "class": _this2.getButtonClass(i),
        "key": i,
        "on": {
          "click": function click() {
            return _this2.toPage(i);
          }
        }
      }, [i]);
    }), this.isFolded && this.isNextMoreShow && this.isMidEllipsis ? h("li", {
      "class": this.btnMoreClass,
      "on": {
        "click": this.nextMorePage,
        "mouseover": function mouseover() {
          return _this2.nextMore = true;
        },
        "mouseout": function mouseout() {
          return _this2.nextMore = false;
        }
      }
    }, [this.nextMore ? h(ChevronRightDoubleIcon$1) : h(EllipsisIcon$1)]) : null, this.isFolded && this.isMidEllipsis ? h("li", {
      "class": this.getButtonClass(this.pageCount),
      "on": {
        "click": function click() {
          return _this2.toPage(_this2.pageCount);
        }
      }
    }, [this.pageCount]) : null]) : null, this.theme === "simple" && Jumper, this.showPreviousAndNextBtn ? h("div", {
      "class": this.nextBtnClass,
      "on": {
        "click": this.nextPage
      },
      "attrs": {
        "disabled": this.disabled || this.current === this.pageCount
      }
    }, [h(ChevronRightIcon$1)]) : null, this.showFirstAndLastPageBtn ? h("div", {
      "class": this.nextBtnClass,
      "on": {
        "click": function click() {
          return _this2.toPage(_this2.pageCount);
        }
      },
      "attrs": {
        "disabled": this.disabled || this.current === this.pageCount
      }
    }, [h(PageLastIcon$1)]) : null, this.theme === "default" && this.showJumper && Jumper]);
  }
});

export { PaginationBase as default };
//# sourceMappingURL=pagination.js.map
