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

import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { defineComponent, toRefs, ref, computed, watch, createVNode, mergeProps, isVNode } from 'vue';
import isNaN from 'lodash/isNaN';
import { PageFirstIcon, PageLastIcon, ChevronLeftIcon, ChevronRightIcon, ChevronLeftDoubleIcon, ChevronRightDoubleIcon, EllipsisIcon } from '@ldesign/icons-vue-next';
import isObject from 'lodash/isObject';
import { usePrefixClass } from '../hooks/useConfig.js';
import { useGlobalIcon } from '../hooks/useGlobalIcon.js';
import { InputNumber } from '../input-number/index.js';
import { Select, Option } from '../select/index.js';
import { InputAdornment } from '../input-adornment/index.js';
import useVModel from '../hooks/useVModel.js';
import useDefaultValue from '../hooks/useDefaultValue.js';
import { useTNodeJSX } from '../hooks/tnode.js';
import props from './props.js';
import usePaginationClasses from './usePaginationClasses.js';
import useMoreAction from './useMoreAction.js';
import { useConfig } from '../config-provider/useConfig.js';
import '../utils/withInstall.js';
import '../input-number/input-number.js';
import '@babel/runtime/helpers/defineProperty';
import '../button/index.js';
import '../button/button.js';
import '@babel/runtime/helpers/toConsumableArray';
import '../loading/index.js';
import '../loading/directive.js';
import '../loading/plugin.js';
import '../utils/dom.js';
import '../_chunks/dep-c9c789a8.js';
import 'lodash/isString';
import 'lodash/isFunction';
import 'lodash/isArray';
import '../utils/easing.js';
import '../loading/loading.js';
import '../utils/render-tnode.js';
import 'lodash/isEmpty';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../hooks/useTeleport.js';
import '../loading/props.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 'lodash/isNull';
import 'lodash/isUndefined';
import 'lodash/isNumber';
import 'lodash/cloneDeep';
import '../config-provider/context.js';
import 'lodash/mergeWith';
import 'lodash/merge';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '@babel/runtime/helpers/typeof';
import 'dayjs';
import '../hooks/useRipple.js';
import '../utils/set-style.js';
import '../hooks/useKeepAnimation.js';
import '../form/hooks.js';
import '../button/props.js';
import '../input/index.js';
import '../input/input.js';
import '../input/props.js';
import '../input/useInput.js';
import '../form/const.js';
import '../input/useLengthLimit.js';
import '../_common/js/log/log.js';
import '../input/useInputEventHandler.js';
import '../input/useInputWidth.js';
import '../input/input-group.js';
import '../input/input-group-props.js';
import '../input-number/props.js';
import '../input-number/useInputNumber.js';
import '../hooks/useCommonClassName.js';
import '../_common/js/input-number/number.js';
import 'lodash/isNil';
import '../_common/js/input-number/large-number.js';
import '../select/select.js';
import 'lodash/pick';
import 'lodash/debounce';
import 'lodash/get';
import 'lodash/intersection';
import '../common-components/fake-arrow.js';
import '../select-input/index.js';
import '../select-input/select-input.js';
import '../popup/index.js';
import '../popup/popup.js';
import '@popperjs/core';
import '../popup/container.js';
import '../hooks/useResizeObserver.js';
import '../popup/props.js';
import '../select-input/props.js';
import '../select-input/useSingle.js';
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/index.js';
import '../tag/tag.js';
import '../tag/props.js';
import '../tag/check-tag.js';
import '../tag/check-tag-props.js';
import '../tag-input/hooks/useHover.js';
import '../tag-input/hooks/useDragSorter.js';
import '../select-input/useOverlayInnerStyle.js';
import '../select/props.js';
import '../select/select-panel.js';
import 'lodash/omit';
import '../select/option.js';
import '../checkbox/index.js';
import '../checkbox/checkbox.js';
import '../checkbox/props.js';
import '../checkbox/constants.js';
import '../checkbox/group.js';
import '../hooks/slot.js';
import '../checkbox/checkbox-group-props.js';
import '../select/option-props.js';
import '../select/helper.js';
import '../select/optionGroup.js';
import '../select/option-group-props.js';
import '../select/hooks/usePanelVirtualScroll.js';
import '../hooks/useVirtualScroll.js';
import '../select/hooks/useSelectOptions.js';
import '../input-adornment/input-adornment.js';
import '../input-adornment/props.js';

var _excluded = ["pageCount"];
function _isSlot(s) {
  return typeof s === 'function' || Object.prototype.toString.call(s) === '[object Object]' && !isVNode(s);
}
var min = 1;
var _Pagination = defineComponent({
  name: "TPagination",
  props: props,
  setup: function setup(props2) {
    var _toRefs = toRefs(props2),
      modelValue = _toRefs.modelValue,
      pageSize = _toRefs.pageSize,
      current = _toRefs.current;
    var renderTNodeJSX = useTNodeJSX();
    var _useVModel = useVModel(current, modelValue, props2.defaultCurrent, props2.onCurrentChange, "current"),
      _useVModel2 = _slicedToArray(_useVModel, 2),
      innerCurrent = _useVModel2[0],
      setInnerCurrent = _useVModel2[1];
    var _useDefaultValue = useDefaultValue(pageSize, props2.defaultPageSize, props2.onPageSizeChange, "pageSize"),
      _useDefaultValue2 = _slicedToArray(_useDefaultValue, 2),
      innerPageSize = _useDefaultValue2[0],
      setInnerPageSize = _useDefaultValue2[1];
    var _useConfig = useConfig("pagination"),
      t = _useConfig.t,
      globalConfig = _useConfig.globalConfig;
    var COMPONENT_NAME = usePrefixClass("pagination");
    var _useGlobalIcon = useGlobalIcon({
        PageFirstIcon: PageFirstIcon,
        PageLastIcon: PageLastIcon,
        ChevronLeftIcon: ChevronLeftIcon,
        ChevronRightIcon: ChevronRightIcon,
        ChevronLeftDoubleIcon: ChevronLeftDoubleIcon,
        ChevronRightDoubleIcon: ChevronRightDoubleIcon,
        EllipsisIcon: EllipsisIcon
      }),
      PageFirstIcon$1 = _useGlobalIcon.PageFirstIcon,
      PageLastIcon$1 = _useGlobalIcon.PageLastIcon,
      ChevronLeftIcon$1 = _useGlobalIcon.ChevronLeftIcon,
      ChevronRightIcon$1 = _useGlobalIcon.ChevronRightIcon,
      ChevronLeftDoubleIcon$1 = _useGlobalIcon.ChevronLeftDoubleIcon,
      ChevronRightDoubleIcon$1 = _useGlobalIcon.ChevronRightDoubleIcon,
      EllipsisIcon$1 = _useGlobalIcon.EllipsisIcon;
    var _usePaginationClasses = usePaginationClasses(props2, innerCurrent, innerPageSize, COMPONENT_NAME),
      pageCount = _usePaginationClasses.pageCount,
      CLASS_MAP = _objectWithoutProperties(_usePaginationClasses, _excluded);
    var _useMoreAction = useMoreAction(props2, pageCount, innerCurrent),
      prevMore = _useMoreAction.prevMore,
      isPrevMoreShow = _useMoreAction.isPrevMoreShow,
      curPageLeftCount = _useMoreAction.curPageLeftCount,
      nextMore = _useMoreAction.nextMore,
      isNextMoreShow = _useMoreAction.isNextMoreShow,
      curPageRightCount = _useMoreAction.curPageRightCount;
    var jumpIndex = ref(innerCurrent.value);
    var isFolded = computed(function () {
      return pageCount.value > props2.maxPageBtn;
    });
    var sizeOptions = computed(function () {
      var pageSizeOptions = props2.pageSizeOptions;
      var options = pageSizeOptions.map(function (option) {
        return isObject(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 = computed(function () {
      return props2.pageEllipsisMode === "mid";
    });
    var pages = 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;
    });
    watch(function () {
      return pageCount.value;
    }, function () {
      if (innerCurrent.value > pageCount.value) innerCurrent.value = 1;
    });
    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(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 = createVNode("div", {
        "class": CLASS_MAP.jumperClass.value
      }, [t(globalConfig.value.jumpTo), createVNode(InputAdornment, {
        "append": "/ ".concat(pageCount.value, " ").concat(t(globalConfig.value.page))
      }, {
        "default": function _default() {
          return [createVNode(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 createVNode("div", {
        "class": CLASS_MAP.paginationClass.value
      }, [renderTNodeJSX("totalContent", createVNode("div", {
        "class": CLASS_MAP.totalClass.value
      }, [t(globalConfig.value.total, {
        total: total
      })])), showPageSize && pageSizeOptions.length > 0 && createVNode(Select, 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 createVNode(Option, {
          "value": item.value,
          "label": item.label,
          "key": index
        }, null);
      })) ? _slot : {
        "default": function _default() {
          return [_slot];
        }
      }), props2.showFirstAndLastPageBtn ? createVNode("div", mergeProps({
        "class": CLASS_MAP.preBtnClass.value,
        "onClick": function onClick() {
          return toPage(1);
        }
      }, {
        disabled: props2.disabled || props2.current === min
      }), [createVNode(PageFirstIcon$1, null, null)]) : null, props2.showPreviousAndNextBtn ? createVNode("div", mergeProps({
        "class": CLASS_MAP.preBtnClass.value,
        "onClick": function onClick() {
          return handlePageChange("prevPage");
        }
      }, {
        disabled: disabled || innerCurrent.value === min
      }), [createVNode(ChevronLeftIcon$1, null, null)]) : null, props2.showPageNumber && props2.theme === "default" ? createVNode("ul", {
        "class": CLASS_MAP.btnWrapClass.value
      }, [isFolded.value && isMidEllipsis.value && createVNode("li", {
        "class": CLASS_MAP.getButtonClass(1),
        "onClick": function onClick() {
          return toPage(min);
        }
      }, [min]), isFolded.value && isPrevMoreShow.value && isMidEllipsis.value ? 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 ? createVNode(ChevronLeftDoubleIcon$1, null, null) : createVNode(EllipsisIcon$1, null, null)]) : null, pages.value.map(function (i) {
        return createVNode("li", {
          "class": CLASS_MAP.getButtonClass(i),
          "key": i,
          "onClick": function onClick() {
            return toPage(i);
          }
        }, [i]);
      }), isFolded.value && isNextMoreShow.value && isMidEllipsis.value ? 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 ? createVNode(ChevronRightDoubleIcon$1, null, null) : createVNode(EllipsisIcon$1, null, null)]) : null, isFolded.value && isMidEllipsis.value ? 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 ? createVNode("div", mergeProps({
        "class": CLASS_MAP.nextBtnClass.value,
        "onClick": function onClick() {
          return handlePageChange("nextPage");
        }
      }, {
        disabled: disabled || innerCurrent.value === pageCount.value
      }), [createVNode(ChevronRightIcon$1, null, null)]) : null, props2.showFirstAndLastPageBtn ? createVNode("div", mergeProps({
        "class": CLASS_MAP.nextBtnClass.value,
        "onClick": function onClick() {
          return toPage(pageCount.value);
        }
      }, {
        disabled: disabled || innerCurrent.value === pageCount.value
      }), [createVNode(PageLastIcon$1, null, null)]) : null, props2.theme === "default" && props2.showJumper && Jumper]);
    };
  }
});

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