/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 swimly
 * @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 iconsVue = require('@ldesign/icons-vue');
var utils_mixins = require('../../utils/mixins.js');
var components_configProvider_configReceiver = require('../config-provider/config-receiver.js');
var components_inputNumber_index = require('../input-number/index.js');
var components_select_index = require('../select/index.js');
var components_inputAdornment_index = require('../input-adornment/index.js');
var utils_renderNode = require('../../utils/render-node.js');
var components_pagination_props = require('./props.js');
var utils_event = require('../../utils/event.js');
require('vue');
require('lodash/mergeWith');
require('lodash');
require('../config-provider/context.js');
require('../../chunks/zh_CN.js');
require('../input-number/input-number.js');
require('../../chunks/helper.js');
require('@vue/composition-api');
require('../button/index.js');
require('../button/button.js');
require('../loading/index.js');
require('../loading/loading.js');
require('../loading/icon/gradient.js');
require('../../chunks/defineProperty-28395935.js');
require('../../chunks/set-style.js');
require('../../chunks/helper2.js');
require('@babel/runtime/helpers/toConsumableArray');
require('@babel/runtime/helpers/slicedToArray');
require('../../config.js');
require('../../utils/dom.js');
require('raf');
require('lodash/isString');
require('../../utils/easing.js');
require('../../utils/transfer-dom.js');
require('../loading/props.js');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../../utils/with-install.js');
require('../loading/plugin.js');
require('../button/props.js');
require('../../utils/ripple.js');
require('../input/index.js');
require('../input/input.js');
require('../../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('../input/props.js');
require('../input/group.js');
require('../../utils/map-props.js');
require('../input-number/props.js');
require('../input-number/hooks/use-input-number.js');
require('../../chunks/isObjectLike-2443c6ff.js');
require('../../chunks/_commonjsHelpers-ea2bee21.js');
require('../../chunks/isArray-b0f0e351.js');
require('../../chunks/log.js');
require('../../hooks/use-common-classname.js');
require('../config-provider/useConfig.js');
require('../../hooks/use-vmodel.js');
require('../../hooks/use-form-disabled.js');
require('../../hooks/use-global-icon.js');
require('../select/select.js');
require('lodash/isFunction');
require('lodash/debounce');
require('lodash/get');
require('lodash/cloneDeep');
require('lodash/isArray');
require('../../hooks/use-default-value.js');
require('../../hooks/lnode.js');
require('lodash/isEmpty');
require('../../hooks/render-lnode.js');
require('lodash/isObject');
require('../select/props.js');
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('../select-input/index.js');
require('../select-input/select-input.js');
require('../select-input/props.js');
require('../select-input/hooks/use-single.js');
require('lodash/pick');
require('../../hooks/use-config.js');
require('../select-input/hooks/use-multiple.js');
require('../select-input/hooks/use-overlay-innerstyle.js');
require('../common/fake-arrow.js');
require('../select/option.js');
require('../checkbox/index.js');
require('../checkbox/group.js');
require('lodash/intersection');
require('../checkbox/checkbox.js');
require('../checkbox/props.js');
require('../../hooks/use-lazy-load.js');
require('../../chunks/observe.js');
require('../select/utils.js');
require('../select/select-panel.js');
require('../../hooks/use-virtual-scroll.js');
require('../select/hooks/use-select-options.js');
require('../select/option-group.js');
require('../input-adornment/input-adornment.js');
require('../input-adornment/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 min = 1;
var PaginationBase = utils_mixins["default"](components_configProvider_configReceiver["default"]("pagination"), components_configProvider_configReceiver.getGlobalIconMixins()).extend({
  name: "LPagination",
  components: {
    LInputNumber: components_inputNumber_index.InputNumber,
    LInputAdornment: components_inputAdornment_index.InputAdornment,
    LSelect: components_select_index.Select,
    LOption: components_select_index.Option
  },
  props: _objectSpread(_objectSpread({}, components_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: {
    classes: function classes() {
      return ["".concat(this.componentName), this.commonSizeClassName[this.size], _defineProperty__default["default"]({}, 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__default["default"]({}, this.commonStatusClassName.disabled, this.disabled || this.current === 1)];
    },
    nextBtnClass: function nextBtnClass() {
      return ["".concat(this.componentName, "__btn"), "".concat(this.componentName, "__btn-next"), _defineProperty__default["default"]({}, 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__default["default"]({}, 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__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 {
          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
        };
        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(this.componentName, "__number"), (_ref5 = {}, _defineProperty__default["default"](_ref5, this.commonStatusClassName.disabled, this.disabled), _defineProperty__default["default"](_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);
      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];

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

    var Jumper = h("div", {
      "class": this.jumperClass
    }, [this.t(this.global.jumpTo), h("l-input-adornment", {
      "attrs": {
        "append": "/ ".concat(this.pageCount, " ").concat(this.t(this.global.page))
      }
    }, [h("l-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.classes
    }, [utils_renderNode.renderLNodeJSX(this, "totalContent", h("div", {
      "class": this.totalClass
    }, [this.t(this.global.total, {
      total: this.total
    })])), this.showPageSize && this.pageSizeOptions.length ? h("l-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("l-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)]) : null, this.showPreviousAndNextBtn ? h("div", {
      "class": this.preBtnClass,
      "on": {
        "click": this.prevPage
      },
      "attrs": {
        "disabled": this.disabled || this.current === min
      }
    }, [h(ChevronLeftIcon)]) : 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) : h(EllipsisIcon)]) : 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) : h(EllipsisIcon)]) : 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)]) : 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)]) : null, this.theme === "default" && this.showJumper && Jumper]);
  }
});

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