/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import { h as helper } from '../../chunks/helper.js';
import { PageFirstIcon, PageLastIcon, ChevronLeftIcon, ChevronRightIcon, ChevronLeftDoubleIcon, ChevronRightDoubleIcon, EllipsisIcon } from '@ldesign/icons-vue';
import mixins from '../../utils/mixins.js';
import getConfigReceiverMixins, { getGlobalIconMixins } from '../config-provider/config-receiver.js';
import { InputNumber } from '../input-number/index.js';
import { Select, Option } from '../select/index.js';
import { InputAdornment } from '../input-adornment/index.js';
import { renderLNodeJSX } from '../../utils/render-node.js';
import { emitEvent } from '../../utils/event.js';
import props from './props.js';
import '../../chunks/_commonjsHelpers.js';
import 'vue';
import 'lodash/mergeWith';
import 'lodash';
import '../config-provider/context.js';
import '../../chunks/zh_CN.js';
import '../config-provider/type.js';
import '../../utils/map-props.js';
import '../../utils/with-install.js';
import '../../config.js';
import '../input-number/input-number.js';
import '@vue/composition-api';
import '../button/index.js';
import '../button/button.js';
import '../loading/index.js';
import '../loading/loading.js';
import '../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../chunks/index.js';
import '../../chunks/log.js';
import '../../chunks/_commonjsHelpers-2e6815bd.js';
import '../../utils/easing.js';
import '../../utils/transfer-dom.js';
import '../loading/props.js';
import '../loading/icon/gradient.js';
import '../../chunks/defineProperty-838e4192.js';
import '../../chunks/set-style.js';
import '../../chunks/helper2.js';
import '../loading/style/css.js';
import '../loading/type.js';
import '../loading/plugin.js';
import '../../utils/ripple.js';
import '../button/props.js';
import '../button/style/css.js';
import '../button/type.js';
import '../input/index.js';
import '../input/input.js';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../utils/helper.js';
import '../input/props.js';
import '../input/group.js';
import '../input/style/css.js';
import '../input/type.js';
import '../../hooks/use-global-icon.js';
import '../../hooks/use-config.js';
import '../config-provider/useConfig.js';
import '../input-number/props.js';
import '../input-number/hooks/use-input-number.js';
import '../../chunks/isObjectLike-c22c7e3b.js';
import '../../chunks/isArray-cd7357b9.js';
import '../../chunks/isNumber-c4eb560e.js';
import '../../hooks/use-common-classname.js';
import '../../hooks/use-vmodel.js';
import '../../hooks/use-form-disabled.js';
import '../input-number/style/css.js';
import '../input-number/type.js';
import '../select/select.js';
import 'lodash/isFunction';
import 'lodash/debounce';
import 'lodash/get';
import 'lodash/cloneDeep';
import 'lodash/isArray';
import 'lodash/isObject';
import '../../hooks/use-default-value.js';
import '../../hooks/lnode.js';
import 'lodash/isEmpty';
import '../../hooks/render-lnode.js';
import '../popup/index.js';
import '../popup/popup.js';
import '@popperjs/core';
import '../popup/props.js';
import '../popup/container.js';
import '../popup/style/css.js';
import '../popup/type.js';
import '../tag/index.js';
import '../tag/tag.js';
import '../tag/props.js';
import '../tag/check-tag.js';
import '../tag/style/css.js';
import '../tag/type.js';
import '../select-input/index.js';
import '../select-input/select-input.js';
import '../select-input/props.js';
import '../select-input/hooks/use-single.js';
import 'lodash/pick';
import '../select-input/hooks/use-multiple.js';
import '../tag-input/index.js';
import '../tag-input/tag-input.js';
import '../tag-input/props.js';
import '../tag-input/hooks/use-tag-scroll.js';
import '../tag-input/hooks/use-tag-list.js';
import '../tag-input/hooks/use-hover.js';
import '../tag-input/hooks/use-drag-sorter.js';
import '../tag-input/style/css.js';
import '../tag-input/type.js';
import '../select-input/hooks/use-overlay-innerstyle.js';
import '../select-input/style/css.js';
import '../select-input/type.js';
import '../common/fake-arrow.js';
import '../select/option.js';
import '../checkbox/index.js';
import '../checkbox/group.js';
import 'lodash/intersection';
import '../checkbox/checkbox.js';
import '../checkbox/props.js';
import '../checkbox/style/css.js';
import '../checkbox/type.js';
import '../../hooks/use-lazy-load.js';
import '../../chunks/observe.js';
import '../select/utils.js';
import '../select/props.js';
import '../select/select-panel.js';
import '../../hooks/use-virtual-scroll.js';
import '../select/hooks/use-select-options.js';
import '../select/option-group.js';
import '../select/style/css.js';
import '../select/type.js';
import '../input-adornment/input-adornment.js';
import '../input-adornment/props.js';
import '../input-adornment/style/css.js';
import '../input-adornment/type.js';

const min = 1;
var PaginationBase = mixins(getConfigReceiverMixins("pagination"), getGlobalIconMixins()).extend({
  name: "LPagination",
  components: {
    LInputNumber: InputNumber,
    LInputAdornment: InputAdornment,
    LSelect: Select,
    LOption: Option
  },
  props: {
    ...props,
    current: {
      type: Number,
      default: 1,
      validator(v) {
        return v > 0;
      }
    },
    pageSize: {
      type: Number,
      default: 10,
      validator(v) {
        return v > 0;
      }
    }
  },
  data() {
    return {
      prevMore: false,
      nextMore: false,
      jumpIndex: this.current
    };
  },
  watch: {
    current(val) {
      this.jumpIndex = val;
    }
  },
  computed: {
    classes() {
      return [`${this.componentName}`, this.commonSizeClassName[this.size], {
        [this.commonStatusClassName.disabled]: this.disabled
      }];
    },
    totalClass() {
      return [`${this.componentName}__total`];
    },
    sizerClass() {
      return [`${this.componentName}__select`];
    },
    preBtnClass() {
      return [`${this.componentName}__btn`, `${this.componentName}__btn-prev`, {
        [this.commonStatusClassName.disabled]: this.disabled || this.current === 1
      }];
    },
    nextBtnClass() {
      return [`${this.componentName}__btn`, `${this.componentName}__btn-next`, {
        [this.commonStatusClassName.disabled]: this.disabled || this.current === this.pageCount
      }];
    },
    btnWrapClass() {
      return [`${this.componentName}__pager`];
    },
    btnMoreClass() {
      return [`${this.componentName}__number`, `${this.componentName}__number--more`, {
        [this.commonStatusClassName.disabled]: this.disabled
      }];
    },
    jumperClass() {
      return [`${this.componentName}__jump`];
    },
    jumperInputClass() {
      return [`${this.componentName}__input`];
    },
    simpleClass() {
      return [`${this.componentName}__select`];
    },
    pageCount() {
      const c = Math.ceil(this.total / this.pageSize);
      return c > 0 ? c : 1;
    },
    pageCountOption() {
      const ans = [];
      for (let i = 1; i <= this.pageCount; i++) ans.push({
        value: i,
        label: `${i}/${this.pageCount}`
      });
      return ans;
    },
    sizeOptions() {
      const options = this.pageSizeOptions.map(option => typeof option === "object" ? option : {
        label: this.t(this.global.itemsPerPage, {
          size: option
        }),
        value: Number(option)
      });
      return options.sort((a, b) => a.value - b.value);
    },
    curPageLeftCount() {
      return Math.ceil((this.foldedMaxPageBtn - 1) / 2);
    },
    curPageRightCount() {
      return Math.ceil((this.foldedMaxPageBtn - 1) / 2);
    },
    isPrevMoreShow() {
      return 2 + this.curPageLeftCount < this.current;
    },
    isNextMoreShow() {
      return this.pageCount - 1 - this.curPageRightCount > this.current;
    },
    pages() {
      const array = [];
      let start;
      let end;
      if (this.isFolded) {
        if (this.isPrevMoreShow && this.isNextMoreShow) {
          start = this.current - this.curPageLeftCount;
          end = this.current + this.curPageRightCount;
        } else {
          const foldedStart = this.isMidEllipsis ? 2 : 1;
          const 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 (let i = start; i <= end; i++) array.push(i);
      return array;
    },
    isFolded() {
      return this.pageCount > this.maxPageBtn;
    },
    isMidEllipsis() {
      return this.pageEllipsisMode === "mid";
    }
  },
  methods: {
    toPage(pageIndex, isTriggerChange) {
      if (this.disabled) return;
      let current = pageIndex;
      if (pageIndex < min) current = min;else if (pageIndex > this.pageCount) current = this.pageCount;
      if (this.current !== current) {
        const prev = this.current;
        const pageInfo = {
          current,
          previous: prev,
          pageSize: this.pageSize
        };
        emitEvent(this, "current-change", current, pageInfo);
        if (isTriggerChange !== false) emitEvent(this, "change", pageInfo);
      }
    },
    prevPage() {
      this.toPage(this.current - 1);
    },
    nextPage() {
      this.toPage(this.current + 1);
    },
    prevMorePage() {
      this.toPage(this.current - this.foldedMaxPageBtn);
    },
    nextMorePage() {
      this.toPage(this.current + this.foldedMaxPageBtn);
    },
    getButtonClass(index) {
      return [`${this.componentName}__number`, {
        [this.commonStatusClassName.disabled]: this.disabled,
        [this.commonStatusClassName.current]: this.current === index
      }];
    },
    onSelectorChange(e) {
      if (this.disabled) return;
      const pageSize = parseInt(e, 10);
      let pageCount = 1;
      if (pageSize > 0) pageCount = Math.ceil(this.total / pageSize);
      let isIndexChange = false;
      if (this.current > pageCount) isIndexChange = true;
      const pageInfo = {
        current: isIndexChange ? pageCount : this.current,
        previous: this.current,
        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(val) {
      const currentIndex = Number(val);
      if (isNaN(currentIndex)) return;
      this.toPage(currentIndex);
    }
  },
  render() {
    const h = arguments[0];
    const {
      PageFirstIcon: PageFirstIcon$1,
      PageLastIcon: PageLastIcon$1,
      ChevronLeftIcon: ChevronLeftIcon$1,
      ChevronRightIcon: ChevronRightIcon$1,
      ChevronLeftDoubleIcon: ChevronLeftDoubleIcon$1,
      ChevronRightDoubleIcon: ChevronRightDoubleIcon$1,
      EllipsisIcon: EllipsisIcon$1
    } = this.useGlobalIcon({
      PageFirstIcon: PageFirstIcon,
      PageLastIcon: PageLastIcon,
      ChevronLeftIcon: ChevronLeftIcon,
      ChevronRightIcon: ChevronRightIcon,
      ChevronLeftDoubleIcon: ChevronLeftDoubleIcon,
      ChevronRightDoubleIcon: ChevronRightDoubleIcon,
      EllipsisIcon: EllipsisIcon
    });
    const Jumper = h("div", {
      "class": this.jumperClass
    }, [this.t(this.global.jumpTo), h("l-input-adornment", {
      "attrs": {
        "append": `/ ${this.pageCount} ${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: this.jumpIndex,
        callback: $$v => {
          this.jumpIndex = $$v;
        }
      }
    })])]);
    return h("div", {
      "class": this.classes
    }, [renderLNodeJSX(this, "totalContent", h("div", {
      "class": this.totalClass
    }, [this.t(this.global.total, {
      total: this.total
    })])), this.showPageSize && this.pageSizeOptions.length ? h("l-select", helper([{
      "attrs": {
        "size": this.size,
        "value": this.pageSize,
        "disabled": this.disabled,
        "autoWidth": true
      },
      "class": this.sizerClass,
      "on": {
        "change": this.onSelectorChange
      }
    }, {
      "props": this.selectProps || {}
    }]), [this.sizeOptions.map((item, index) => h("l-option", {
      "attrs": {
        "value": item.value,
        "label": item.label
      },
      "key": index
    }))]) : null, this.showFirstAndLastPageBtn ? h("div", {
      "class": this.preBtnClass,
      "on": {
        "click": () => this.toPage(1)
      },
      "attrs": {
        "disabled": this.disabled || this.current === min
      }
    }, [h(PageFirstIcon$1)]) : null, this.showPreviousAndNextBtn ? h("div", {
      "class": this.preBtnClass,
      "on": {
        "click": this.prevPage
      },
      "attrs": {
        "disabled": this.disabled || this.current === min
      }
    }, [h(ChevronLeftIcon$1)]) : null, this.showPageNumber && this.theme === "default" ? h("ul", {
      "class": this.btnWrapClass
    }, [this.isFolded && this.isMidEllipsis ? h("li", {
      "class": this.getButtonClass(1),
      "on": {
        "click": () => this.toPage(min)
      }
    }, [min]) : null, this.isFolded && this.isPrevMoreShow && this.isMidEllipsis ? h("li", {
      "class": this.btnMoreClass,
      "on": {
        "click": this.prevMorePage,
        "mouseover": () => this.prevMore = true,
        "mouseout": () => this.prevMore = false
      }
    }, [this.prevMore ? h(ChevronLeftDoubleIcon$1) : h(EllipsisIcon$1)]) : null, this.pages.map(i => h("li", {
      "class": this.getButtonClass(i),
      "key": i,
      "on": {
        "click": () => this.toPage(i)
      }
    }, [i])), this.isFolded && this.isNextMoreShow && this.isMidEllipsis ? h("li", {
      "class": this.btnMoreClass,
      "on": {
        "click": this.nextMorePage,
        "mouseover": () => this.nextMore = true,
        "mouseout": () => this.nextMore = false
      }
    }, [this.nextMore ? h(ChevronRightDoubleIcon$1) : h(EllipsisIcon$1)]) : null, this.isFolded && this.isMidEllipsis ? h("li", {
      "class": this.getButtonClass(this.pageCount),
      "on": {
        "click": () => this.toPage(this.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$1)]) : null, this.showFirstAndLastPageBtn ? h("div", {
      "class": this.nextBtnClass,
      "on": {
        "click": () => this.toPage(this.pageCount)
      },
      "attrs": {
        "disabled": this.disabled || this.current === this.pageCount
      }
    }, [h(PageLastIcon$1)]) : null, this.theme === "default" && this.showJumper && Jumper]);
  }
});

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