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

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _typeof = require('@babel/runtime/helpers/typeof');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var tdesignIconsVue = require('tdesign-icons-vue');
var config = require('../config.js');
var utils_mixins = require('../utils/mixins.js');
var configProvider_configReceiver = require('../config-provider/config-receiver.js');
var inputNumber_index = require('../input-number/index.js');
var select_index = require('../select/index.js');
var utils_classnames = require('../utils/classnames.js');
var utils_renderTnode = require('../utils/render-tnode.js');
var pagination_props = require('./props.js');
var utils_event = require('../utils/event.js');
require('vue');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('../input-number/input-number.js');
require('../_chunks/dep-33ee4bce.js');
require('@babel/runtime/helpers/asyncToGenerator');
require('@babel/runtime/regenerator');
require('../button/index.js');
require('../button/button.js');
require('../loading/index.js');
require('../loading/loading.js');
require('../loading/icon/gradient.js');
require('../_common/js/loading/circle-adapter.js');
require('../_common/js/utils/set-style.js');
require('../_common/js/utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('../utils/dom.js');
require('raf');
require('lodash/isString');
require('../utils/easing.js');
require('../utils/transfer-dom.js');
require('../loading/props.js');
require('../utils/withInstall.js');
require('lodash/capitalize');
require('../loading/plugin.js');
require('lodash/isObject');
require('../button/props.js');
require('../utils/ripple.js');
require('../input/index.js');
require('../input/addon.js');
require('../input/input.js');
require('@babel/runtime/helpers/toConsumableArray');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../utils/helper.js');
require('../input/props.js');
require('../input/input-group.js');
require('../utils/map-props.js');
require('../input-number/props.js');
require('../select/select.js');
require('lodash/isFunction');
require('lodash/debounce');
require('lodash/get');
require('lodash/set');
require('../popup/index.js');
require('../popup/popup.js');
require('@popperjs/core');
require('../popup/props.js');
require('../popup/container.js');
require('../tag/index.js');
require('../tag/tag.js');
require('../tag/props.js');
require('../tag/check-tag.js');
require('../tag/check-tag-props.js');
require('../common-components/fake-arrow.js');
require('../select/option.js');
require('../select/option-props.js');
require('../checkbox/index.js');
require('../checkbox/group.js');
require('lodash/intersection');
require('../checkbox/checkbox.js');
require('../checkbox/props.js');
require('../checkbox/checkbox-group-props.js');
require('../select/props.js');
require('../select/optionGroup.js');
require('../select/option-group-props.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);

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__default["default"](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(config.prefix, "-pagination");
var min = 1;
var PaginationBase = utils_mixins["default"](configProvider_configReceiver["default"]("pagination")).extend({
  name: "TPagination",
  components: {
    ChevronLeftIcon: tdesignIconsVue.ChevronLeftIcon,
    ChevronRightIcon: tdesignIconsVue.ChevronRightIcon,
    ChevronRightDoubleIcon: tdesignIconsVue.ChevronRightDoubleIcon,
    ChevronLeftDoubleIcon: tdesignIconsVue.ChevronLeftDoubleIcon,
    EllipsisIcon: tdesignIconsVue.EllipsisIcon,
    TInputNumber: inputNumber_index.InputNumber,
    TSelect: select_index.Select,
    TOption: select_index.Option
  },
  props: _objectSpread(_objectSpread({}, pagination_props["default"]), {}, {
    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), utils_classnames["default"].SIZE[this.size], _defineProperty__default["default"]({}, utils_classnames["default"].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__default["default"]({}, utils_classnames["default"].STATUS.disabled, this.disabled || this.current === 1)];
    },
    nextBtnClass: function nextBtnClass() {
      return ["".concat(name, "__btn"), "".concat(name, "__btn-next"), _defineProperty__default["default"]({}, utils_classnames["default"].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__default["default"]({}, utils_classnames["default"].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__default["default"](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
        };
        utils_event.emitEvent(this, "current-change", current, pageInfo);

        if (isTriggerChange !== false) {
          utils_event.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__default["default"](_ref5, utils_classnames["default"].STATUS.disabled, this.disabled), _defineProperty__default["default"](_ref5, utils_classnames["default"].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);
      utils_event.emitEvent(this, "page-size-change", pageSize, pageInfo);
      utils_event.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
    }, [utils_renderTnode.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]);
  }
});

exports["default"] = PaginationBase;
//# sourceMappingURL=pagination.js.map
