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

'use strict';

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

var helper = require('../../chunks/helper.js');
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 utils_event = require('../../utils/event.js');
var components_pagination_props = require('./props.js');
require('vue');
require('lodash/mergeWith');
require('lodash');
require('../config-provider/context.js');
require('../../chunks/zh_CN.js');
require('../../utils/map-props.js');
require('../../utils/with-install.js');
require('../../config.js');
require('../input-number/input-number.js');
require('@vue/composition-api');
require('../button/index.js');
require('../button/button.js');
require('../loading/index.js');
require('../loading/loading.js');
require('../../utils/dom.js');
require('raf');
require('lodash/isString');
require('../../chunks/log.js');
require('../../chunks/_commonjsHelpers-2e6815bd.js');
require('../../utils/easing.js');
require('../../utils/transfer-dom.js');
require('../loading/props.js');
require('../loading/icon/gradient.js');
require('../../chunks/defineProperty-838e4192.js');
require('../../chunks/set-style.js');
require('../../chunks/helper2.js');
require('../loading/plugin.js');
require('../../utils/ripple.js');
require('../button/props.js');
require('../input/index.js');
require('../input/input.js');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../../utils/helper.js');
require('../input/props.js');
require('../input/group.js');
require('../../hooks/use-global-icon.js');
require('../config-provider/useConfig.js');
require('../input-number/props.js');
require('../input-number/hooks/use-input-number.js');
require('../../chunks/isObjectLike-c22c7e3b.js');
require('../../chunks/isArray-cd7357b9.js');
require('../../chunks/isNumber-c4eb560e.js');
require('../../hooks/use-common-classname.js');
require('../../hooks/use-vmodel.js');
require('../../hooks/use-form-disabled.js');
require('../select/select.js');
require('lodash/isFunction');
require('lodash/debounce');
require('lodash/get');
require('lodash/cloneDeep');
require('lodash/isArray');
require('lodash/isObject');
require('../../hooks/use-default-value.js');
require('../../hooks/lnode.js');
require('lodash/isEmpty');
require('../../hooks/render-lnode.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('../tag-input/index.js');
require('../tag-input/tag-input.js');
require('../tag-input/props.js');
require('../tag-input/hooks/use-tag-scroll.js');
require('../tag-input/hooks/use-tag-list.js');
require('../tag-input/hooks/use-hover.js');
require('../tag-input/hooks/use-drag-sorter.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/props.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');

const 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: {
    ...components_pagination_props["default"],
    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
        };
        utils_event.emitEvent(this, "current-change", current, pageInfo);
        if (isTriggerChange !== false) utils_event.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);
      utils_event.emitEvent(this, "page-size-change", pageSize, pageInfo);
      utils_event.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,
      PageLastIcon,
      ChevronLeftIcon,
      ChevronRightIcon,
      ChevronLeftDoubleIcon,
      ChevronRightDoubleIcon,
      EllipsisIcon
    } = this.useGlobalIcon({
      PageFirstIcon: iconsVue.PageFirstIcon,
      PageLastIcon: iconsVue.PageLastIcon,
      ChevronLeftIcon: iconsVue.ChevronLeftIcon,
      ChevronRightIcon: iconsVue.ChevronRightIcon,
      ChevronLeftDoubleIcon: iconsVue.ChevronLeftDoubleIcon,
      ChevronRightDoubleIcon: iconsVue.ChevronRightDoubleIcon,
      EllipsisIcon: iconsVue.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
    }, [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", helper.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)]) : 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": () => 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) : h(EllipsisIcon)]) : 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) : h(EllipsisIcon)]) : 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)]) : null, this.showFirstAndLastPageBtn ? h("div", {
      "class": this.nextBtnClass,
      "on": {
        "click": () => this.toPage(this.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
