/**
 * tdesign v0.39.1-alpha.1
 * (c) 2022 tdesign
 * @license MIT
 */

import _typeof from '@babel/runtime/helpers/typeof';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { ChevronLeftIcon, ChevronRightIcon, ChevronRightDoubleIcon, ChevronLeftDoubleIcon, EllipsisIcon } from 'tdesign-icons-vue';
import { prefix } from '../config.js';
import mixins from '../utils/mixins.js';
import getConfigReceiverMixins from '../config-provider/config-receiver.js';
import { InputNumber } from '../input-number/index.js';
import { Select, Option } from '../select/index.js';
import ClASSNAMES from '../utils/classnames.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 '../input-number/input-number.js';
import '../_chunks/dep-1ec74b8b.js';
import '@babel/runtime/helpers/asyncToGenerator';
import '@babel/runtime/regenerator';
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/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/css.js';
import '../loading/type.js';
import '../loading/plugin.js';
import 'lodash/isObject';
import '../button/props.js';
import '../utils/ripple.js';
import '../button/type.js';
import '../input/index.js';
import '../input/addon.js';
import '../input/input.js';
import '@babel/runtime/helpers/toConsumableArray';
import 'lodash/camelCase';
import 'lodash/kebabCase';
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/type.js';
import '../select/select.js';
import 'lodash/isFunction';
import 'lodash/debounce';
import 'lodash/get';
import 'lodash/set';
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 '../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 '../select/props.js';
import '../select/optionGroup.js';
import '../select/option-group-props.js';
import '../select/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 name = "".concat(prefix, "-pagination");
var min = 1;
var PaginationBase = mixins(getConfigReceiverMixins("pagination")).extend({
  name: "TPagination",
  components: {
    ChevronLeftIcon: ChevronLeftIcon,
    ChevronRightIcon: ChevronRightIcon,
    ChevronRightDoubleIcon: ChevronRightDoubleIcon,
    ChevronLeftDoubleIcon: ChevronLeftDoubleIcon,
    EllipsisIcon: EllipsisIcon,
    TInputNumber: InputNumber,
    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(name), ClASSNAMES.SIZE[this.size], _defineProperty({}, ClASSNAMES.STATUS.disabled, this.disabled)];
    },
    totalClass: function totalClass() {
      return ["".concat(name, "__total")];
    },
    sizerClass: function sizerClass() {
      return ["".concat(name, "__select")];
    },
    preBtnClass: function preBtnClass() {
      return ["".concat(name, "__btn"), "".concat(name, "__btn-prev"), _defineProperty({}, ClASSNAMES.STATUS.disabled, this.disabled || this.current === 1)];
    },
    nextBtnClass: function nextBtnClass() {
      return ["".concat(name, "__btn"), "".concat(name, "__btn-next"), _defineProperty({}, ClASSNAMES.STATUS.disabled, this.disabled || this.current === this.pageCount)];
    },
    btnWrapClass: function btnWrapClass() {
      return ["".concat(name, "__pager")];
    },
    btnMoreClass: function btnMoreClass() {
      return ["".concat(name, "__number"), "".concat(name, "__number--more"), _defineProperty({}, ClASSNAMES.STATUS.disabled, this.disabled)];
    },
    jumperClass: function jumperClass() {
      return ["".concat(name, "__jump")];
    },
    jumperInputClass: function jumperInputClass() {
      return ["".concat(name, "__input")];
    },
    simpleClass: function simpleClass() {
      return ["".concat(name, "__select")];
    },
    isSimple: function isSimple() {
      return this.theme === "simple";
    },
    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 {
          start = this.isPrevMoreShow ? this.pageCount - this.foldedMaxPageBtn + 1 : 2;
          end = this.isPrevMoreShow ? this.pageCount - 1 : 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;
    }
  },
  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(name, "__number"), (_ref5 = {}, _defineProperty(_ref5, ClASSNAMES.STATUS.disabled, this.disabled), _defineProperty(_ref5, ClASSNAMES.STATUS.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];
    return h("div", {
      "class": this.paginationClass
    }, [renderTNodeJSX(this, "totalContent", h("div", {
      "class": this.totalClass
    }, [this.t(this.global.total, {
      total: this.total
    })])), this.pageSizeOptions.length ? h("t-select", {
      "attrs": {
        "size": this.size,
        "value": this.pageSize,
        "disabled": this.disabled
      },
      "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, h("div", {
      "class": this.preBtnClass,
      "on": {
        "click": this.prevPage
      },
      "attrs": {
        "disabled": this.disabled || this.current === min
      }
    }, [h("chevron-left-icon")]), !this.isSimple ? h("ul", {
      "class": this.btnWrapClass
    }, [this.isFolded ? h("li", {
      "class": this.getButtonClass(1),
      "on": {
        "click": function click() {
          return _this2.toPage(min);
        }
      }
    }, [min]) : null, this.isFolded && this.isPrevMoreShow ? 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("chevron-left-double-icon") : h("ellipsis-icon")]) : 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 ? 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("chevron-right-double-icon") : h("ellipsis-icon")]) : null, this.isFolded ? h("li", {
      "class": this.getButtonClass(this.pageCount),
      "on": {
        "click": function click() {
          return _this2.toPage(_this2.pageCount);
        }
      }
    }, [this.pageCount]) : null]) : h("t-select", {
      "attrs": {
        "size": this.size,
        "value": this.current,
        "disabled": this.disabled,
        "options": this.pageCountOption
      },
      "class": this.simpleClass,
      "on": {
        "change": this.toPage
      }
    }), h("div", {
      "class": this.nextBtnClass,
      "on": {
        "click": this.nextPage
      },
      "attrs": {
        "disabled": this.disabled || this.current === this.pageCount
      }
    }, [h("chevron-right-icon")]), this.showJumper ? h("div", {
      "class": this.jumperClass
    }, [this.t(this.global.jumpTo), h("t-input-number", {
      "class": this.jumperInputClass,
      "on": {
        "blur": this.onJumperChange,
        "enter": this.onJumperChange
      },
      "attrs": {
        "max": this.pageCount,
        "min": min,
        "theme": "normal",
        "placeholder": ""
      },
      "model": {
        value: _this2.jumpIndex,
        callback: function callback($$v) {
          _this2.jumpIndex = $$v;
        }
      }
    }), this.t(this.global.page)]) : null]);
  }
});

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