/**
 * ldesign v0.3.13
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

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

var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var vue = require('vue');
var isNaN = require('lodash/isNaN');
var iconsVueNext = require('@ldesign/icons-vue-next');
var isObject = require('lodash/isObject');
var hooks_useConfig = require('../hooks/useConfig.js');
var hooks_useGlobalIcon = require('../hooks/useGlobalIcon.js');
var inputNumber_index = require('../input-number/index.js');
var select_index = require('../select/index.js');
var inputAdornment_index = require('../input-adornment/index.js');
var hooks_useVModel = require('../hooks/useVModel.js');
var hooks_useDefaultValue = require('../hooks/useDefaultValue.js');
var hooks_tnode = require('../hooks/tnode.js');
var pagination_props = require('./props.js');
var pagination_usePaginationClasses = require('./usePaginationClasses.js');
var pagination_useMoreAction = require('./useMoreAction.js');
var configProvider_useConfig = require('../config-provider/useConfig.js');
require('../utils/withInstall.js');
require('../input-number/input-number.js');
require('@babel/runtime/helpers/defineProperty');
require('../button/index.js');
require('../button/button.js');
require('@babel/runtime/helpers/toConsumableArray');
require('../loading/index.js');
require('../loading/directive.js');
require('../loading/plugin.js');
require('../utils/dom.js');
require('../_chunks/dep-f4db2590.js');
require('lodash/isString');
require('lodash/isFunction');
require('lodash/isArray');
require('../utils/easing.js');
require('../loading/loading.js');
require('../utils/render-tnode.js');
require('lodash/isEmpty');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../hooks/useTeleport.js');
require('../loading/props.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('lodash/isNull');
require('lodash/isUndefined');
require('lodash/isNumber');
require('lodash/cloneDeep');
require('../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/typeof');
require('dayjs');
require('../hooks/useRipple.js');
require('../utils/set-style.js');
require('../hooks/useKeepAnimation.js');
require('../form/hooks.js');
require('../button/props.js');
require('../input/index.js');
require('../input/input.js');
require('../input/props.js');
require('../input/useInput.js');
require('../form/const.js');
require('../input/useLengthLimit.js');
require('../_common/js/log/log.js');
require('../input/useInputEventHandler.js');
require('../input/useInputWidth.js');
require('../input/input-group.js');
require('../input/input-group-props.js');
require('../input-number/props.js');
require('../input-number/useInputNumber.js');
require('../hooks/useCommonClassName.js');
require('../_common/js/input-number/number.js');
require('lodash/isNil');
require('../_common/js/input-number/large-number.js');
require('../select/select.js');
require('lodash/pick');
require('lodash/debounce');
require('lodash/get');
require('lodash/intersection');
require('../common-components/fake-arrow.js');
require('../select-input/index.js');
require('../select-input/select-input.js');
require('../popup/index.js');
require('../popup/popup.js');
require('@popperjs/core');
require('../popup/props.js');
require('../popup/container.js');
require('../hooks/useResizeObserver.js');
require('../select-input/props.js');
require('../select-input/useSingle.js');
require('../select-input/useMultiple.js');
require('../tag-input/index.js');
require('../tag-input/tag-input.js');
require('../tag-input/props.js');
require('../tag-input/hooks/useTagScroll.js');
require('../tag-input/useTagList.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('../tag-input/hooks/useHover.js');
require('../tag-input/hooks/useDragSorter.js');
require('../select-input/useOverlayInnerStyle.js');
require('../select/props.js');
require('../select/select-panel.js');
require('lodash/omit');
require('../select/option.js');
require('../checkbox/index.js');
require('../checkbox/checkbox.js');
require('../checkbox/props.js');
require('../checkbox/constants.js');
require('../checkbox/group.js');
require('../hooks/slot.js');
require('../checkbox/checkbox-group-props.js');
require('../select/option-props.js');
require('../select/helper.js');
require('../select/optionGroup.js');
require('../select/option-group-props.js');
require('../select/hooks/usePanelVirtualScroll.js');
require('../hooks/useVirtualScroll.js');
require('../select/hooks/useSelectOptions.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 _objectWithoutProperties__default = /*#__PURE__*/_interopDefaultLegacy(_objectWithoutProperties);
var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
var isNaN__default = /*#__PURE__*/_interopDefaultLegacy(isNaN);
var isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject);

var _excluded = ["pageCount"];
function _isSlot(s) {
  return typeof s === 'function' || Object.prototype.toString.call(s) === '[object Object]' && !vue.isVNode(s);
}
var min = 1;
var _Pagination = vue.defineComponent({
  name: "TPagination",
  props: pagination_props["default"],
  setup: function setup(props2) {
    var _toRefs = vue.toRefs(props2),
      modelValue = _toRefs.modelValue,
      pageSize = _toRefs.pageSize,
      current = _toRefs.current;
    var renderTNodeJSX = hooks_tnode.useTNodeJSX();
    var _useVModel = hooks_useVModel["default"](current, modelValue, props2.defaultCurrent, props2.onCurrentChange, "current"),
      _useVModel2 = _slicedToArray__default["default"](_useVModel, 2),
      innerCurrent = _useVModel2[0],
      setInnerCurrent = _useVModel2[1];
    var _useDefaultValue = hooks_useDefaultValue["default"](pageSize, props2.defaultPageSize, props2.onPageSizeChange, "pageSize"),
      _useDefaultValue2 = _slicedToArray__default["default"](_useDefaultValue, 2),
      innerPageSize = _useDefaultValue2[0],
      setInnerPageSize = _useDefaultValue2[1];
    var _useConfig = configProvider_useConfig.useConfig("pagination"),
      t = _useConfig.t,
      globalConfig = _useConfig.globalConfig;
    var COMPONENT_NAME = hooks_useConfig.usePrefixClass("pagination");
    var _useGlobalIcon = hooks_useGlobalIcon.useGlobalIcon({
        PageFirstIcon: iconsVueNext.PageFirstIcon,
        PageLastIcon: iconsVueNext.PageLastIcon,
        ChevronLeftIcon: iconsVueNext.ChevronLeftIcon,
        ChevronRightIcon: iconsVueNext.ChevronRightIcon,
        ChevronLeftDoubleIcon: iconsVueNext.ChevronLeftDoubleIcon,
        ChevronRightDoubleIcon: iconsVueNext.ChevronRightDoubleIcon,
        EllipsisIcon: iconsVueNext.EllipsisIcon
      }),
      PageFirstIcon = _useGlobalIcon.PageFirstIcon,
      PageLastIcon = _useGlobalIcon.PageLastIcon,
      ChevronLeftIcon = _useGlobalIcon.ChevronLeftIcon,
      ChevronRightIcon = _useGlobalIcon.ChevronRightIcon,
      ChevronLeftDoubleIcon = _useGlobalIcon.ChevronLeftDoubleIcon,
      ChevronRightDoubleIcon = _useGlobalIcon.ChevronRightDoubleIcon,
      EllipsisIcon = _useGlobalIcon.EllipsisIcon;
    var _usePaginationClasses = pagination_usePaginationClasses["default"](props2, innerCurrent, innerPageSize, COMPONENT_NAME),
      pageCount = _usePaginationClasses.pageCount,
      CLASS_MAP = _objectWithoutProperties__default["default"](_usePaginationClasses, _excluded);
    var _useMoreAction = pagination_useMoreAction["default"](props2, pageCount, innerCurrent),
      prevMore = _useMoreAction.prevMore,
      isPrevMoreShow = _useMoreAction.isPrevMoreShow,
      curPageLeftCount = _useMoreAction.curPageLeftCount,
      nextMore = _useMoreAction.nextMore,
      isNextMoreShow = _useMoreAction.isNextMoreShow,
      curPageRightCount = _useMoreAction.curPageRightCount;
    var jumpIndex = vue.ref(innerCurrent.value);
    var isFolded = vue.computed(function () {
      return pageCount.value > props2.maxPageBtn;
    });
    var sizeOptions = vue.computed(function () {
      var pageSizeOptions = props2.pageSizeOptions;
      var options = pageSizeOptions.map(function (option) {
        return isObject__default["default"](option) ? option : {
          label: t(globalConfig.value.itemsPerPage, {
            size: option
          }),
          value: Number(option)
        };
      });
      return options.sort(function (a, b) {
        return a.value - b.value;
      });
    });
    var isMidEllipsis = vue.computed(function () {
      return props2.pageEllipsisMode === "mid";
    });
    var pages = vue.computed(function () {
      var array = [];
      var start;
      var end;
      if (isFolded.value) {
        if (isPrevMoreShow.value && isNextMoreShow.value) {
          start = innerCurrent.value - curPageLeftCount.value;
          end = innerCurrent.value + curPageRightCount.value;
        } else {
          var foldedStart = isMidEllipsis.value ? 2 : 1;
          var foldedEnd = isMidEllipsis.value ? pageCount.value - 1 : pageCount.value;
          start = isPrevMoreShow.value ? pageCount.value - props2.foldedMaxPageBtn + 1 : foldedStart;
          end = isPrevMoreShow.value ? foldedEnd : props2.foldedMaxPageBtn;
        }
      } else {
        start = 1;
        end = pageCount.value;
      }
      for (var i = start; i <= end; i++) array.push(i);
      return array;
    });
    vue.watch(function () {
      return pageCount.value;
    }, function () {
      if (innerCurrent.value > pageCount.value) innerCurrent.value = 1;
    });
    vue.watch(function () {
      return innerCurrent.value;
    }, function (val) {
      return jumpIndex.value = val;
    });
    var toPage = function toPage(pageIndex, isTriggerChange) {
      if (props2.disabled) return;
      var current2 = pageIndex;
      if (pageIndex < min) current2 = min;else if (pageIndex > pageCount.value) current2 = pageCount.value;
      if (innerCurrent.value !== current2) {
        var _props2$onChange;
        var prev = innerCurrent.value;
        var pageInfo = {
          current: current2,
          previous: prev,
          pageSize: innerPageSize.value
        };
        if (isTriggerChange !== false) (_props2$onChange = props2.onChange) === null || _props2$onChange === void 0 ? void 0 : _props2$onChange.call(props2, pageInfo);
        setInnerCurrent(current2, pageInfo);
      }
    };
    var handlePageChange = function handlePageChange(type) {
      var pageChangeMap = {
        prevPage: function prevPage() {
          return toPage(innerCurrent.value - 1);
        },
        nextPage: function nextPage() {
          return toPage(innerCurrent.value + 1);
        },
        prevMorePage: function prevMorePage() {
          return toPage(innerCurrent.value - props2.foldedMaxPageBtn);
        },
        nextMorePage: function nextMorePage() {
          return toPage(innerCurrent.value + props2.foldedMaxPageBtn);
        }
      };
      pageChangeMap[type]();
    };
    var onSelectorChange = function onSelectorChange(e) {
      var _props2$onChange2;
      if (props2.disabled) return;
      var pageSize2 = parseInt(e, 10);
      var pageCount2 = 1;
      if (pageSize2 > 0) pageCount2 = Math.ceil(props2.total / pageSize2);
      var isIndexChange = false;
      if (innerCurrent.value > pageCount2) isIndexChange = true;
      var pageInfo = {
        current: isIndexChange ? pageCount2 : innerCurrent.value,
        previous: innerCurrent.value,
        pageSize: pageSize2
      };
      (_props2$onChange2 = props2.onChange) === null || _props2$onChange2 === void 0 ? void 0 : _props2$onChange2.call(props2, pageInfo);
      setInnerPageSize(pageSize2, pageInfo);
      if (isIndexChange) toPage(pageCount2, false);
    };
    var onJumperChange = function onJumperChange(val) {
      var currentIndex = Math.trunc(+val);
      if (isNaN__default["default"](currentIndex)) return;
      jumpIndex.value = currentIndex;
      toPage(currentIndex);
    };
    return function () {
      var _slot;
      var total = props2.total,
        pageSizeOptions = props2.pageSizeOptions,
        size = props2.size,
        disabled = props2.disabled,
        showPageSize = props2.showPageSize;
      if (pageCount.value < 1) return null;
      var Jumper = vue.createVNode("div", {
        "class": CLASS_MAP.jumperClass.value
      }, [t(globalConfig.value.jumpTo), vue.createVNode(inputAdornment_index.InputAdornment, {
        "append": "/ ".concat(pageCount.value, " ").concat(t(globalConfig.value.page))
      }, {
        "default": function _default() {
          return [vue.createVNode(inputNumber_index.InputNumber, {
            "class": CLASS_MAP.jumperInputClass.value,
            "modelValue": jumpIndex.value,
            "onUpdate:modelValue": function onUpdateModelValue($event) {
              return jumpIndex.value = $event;
            },
            "onBlur": onJumperChange,
            "onEnter": onJumperChange,
            "max": pageCount.value,
            "min": min,
            "size": size,
            "disabled": disabled,
            "theme": "normal",
            "placeholder": ""
          }, null)];
        }
      })]);
      return vue.createVNode("div", {
        "class": CLASS_MAP.paginationClass.value
      }, [renderTNodeJSX("totalContent", vue.createVNode("div", {
        "class": CLASS_MAP.totalClass.value
      }, [t(globalConfig.value.total, {
        total: total
      })])), showPageSize && pageSizeOptions.length > 0 && vue.createVNode(select_index.Select, vue.mergeProps({
        "size": size,
        "value": innerPageSize,
        "disabled": disabled,
        "class": CLASS_MAP.sizerClass.value,
        "autoWidth": true,
        "onChange": onSelectorChange
      }, props2.selectProps), _isSlot(_slot = sizeOptions.value.map(function (item, index) {
        return vue.createVNode(select_index.Option, {
          "value": item.value,
          "label": item.label,
          "key": index
        }, null);
      })) ? _slot : {
        "default": function _default() {
          return [_slot];
        }
      }), props2.showFirstAndLastPageBtn ? vue.createVNode("div", vue.mergeProps({
        "class": CLASS_MAP.preBtnClass.value,
        "onClick": function onClick() {
          return toPage(1);
        }
      }, {
        disabled: props2.disabled || props2.current === min
      }), [vue.createVNode(PageFirstIcon, null, null)]) : null, props2.showPreviousAndNextBtn ? vue.createVNode("div", vue.mergeProps({
        "class": CLASS_MAP.preBtnClass.value,
        "onClick": function onClick() {
          return handlePageChange("prevPage");
        }
      }, {
        disabled: disabled || innerCurrent.value === min
      }), [vue.createVNode(ChevronLeftIcon, null, null)]) : null, props2.showPageNumber && props2.theme === "default" ? vue.createVNode("ul", {
        "class": CLASS_MAP.btnWrapClass.value
      }, [isFolded.value && isMidEllipsis.value && vue.createVNode("li", {
        "class": CLASS_MAP.getButtonClass(1),
        "onClick": function onClick() {
          return toPage(min);
        }
      }, [min]), isFolded.value && isPrevMoreShow.value && isMidEllipsis.value ? vue.createVNode("li", {
        "class": CLASS_MAP.btnMoreClass.value,
        "onClick": function onClick() {
          return handlePageChange("prevMorePage");
        },
        "onMouseover": function onMouseover() {
          return prevMore.value = true;
        },
        "onMouseout": function onMouseout() {
          return prevMore.value = false;
        }
      }, [prevMore.value ? vue.createVNode(ChevronLeftDoubleIcon, null, null) : vue.createVNode(EllipsisIcon, null, null)]) : null, pages.value.map(function (i) {
        return vue.createVNode("li", {
          "class": CLASS_MAP.getButtonClass(i),
          "key": i,
          "onClick": function onClick() {
            return toPage(i);
          }
        }, [i]);
      }), isFolded.value && isNextMoreShow.value && isMidEllipsis.value ? vue.createVNode("li", {
        "class": CLASS_MAP.btnMoreClass.value,
        "onClick": function onClick() {
          return handlePageChange("nextMorePage");
        },
        "onMouseover": function onMouseover() {
          return nextMore.value = true;
        },
        "onMouseout": function onMouseout() {
          return nextMore.value = false;
        }
      }, [nextMore.value ? vue.createVNode(ChevronRightDoubleIcon, null, null) : vue.createVNode(EllipsisIcon, null, null)]) : null, isFolded.value && isMidEllipsis.value ? vue.createVNode("li", {
        "class": CLASS_MAP.getButtonClass(pageCount.value),
        "onClick": function onClick() {
          return toPage(pageCount.value);
        }
      }, [pageCount.value]) : null]) : null, props2.theme === "simple" && Jumper, props2.showPreviousAndNextBtn ? vue.createVNode("div", vue.mergeProps({
        "class": CLASS_MAP.nextBtnClass.value,
        "onClick": function onClick() {
          return handlePageChange("nextPage");
        }
      }, {
        disabled: disabled || innerCurrent.value === pageCount.value
      }), [vue.createVNode(ChevronRightIcon, null, null)]) : null, props2.showFirstAndLastPageBtn ? vue.createVNode("div", vue.mergeProps({
        "class": CLASS_MAP.nextBtnClass.value,
        "onClick": function onClick() {
          return toPage(pageCount.value);
        }
      }, {
        disabled: disabled || innerCurrent.value === pageCount.value
      }), [vue.createVNode(PageLastIcon, null, null)]) : null, props2.theme === "default" && props2.showJumper && Jumper]);
    };
  }
});

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