"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

var _create = _interopRequireDefault(require("../utils/create"));

function makePage(number, text, active) {
  return {
    number: number,
    text: text,
    active: active
  };
}

var _default = (0, _create.default)({
  render: function render() {
    var _vm = this;

    var _h = _vm.$createElement;

    var _c = _vm._self._c || _h;

    return _c('ul', {
      class: _vm.b({
        simple: _vm.simple
      })
    }, [_c('li', {
      class: [_vm.b('item', {
        disabled: _vm.value === 1
      }), _vm.b('prev'), 'vm-hairline'],
      on: {
        "click": function click($event) {
          return _vm.onSelect(_vm.value - 1);
        }
      }
    }, [_vm._t("prev-text", [_vm._v("\n      " + _vm._s(_vm.prevText || "上一页") + "\n    ")])], 2), _vm._l(_vm.pages, function (page, index) {
      return _c('li', {
        key: index,
        class: [_vm.b('item', {
          active: page.active
        }), _vm.b('page'), 'vm-hairline'],
        on: {
          "click": function click($event) {
            return _vm.onSelect(page.number);
          }
        }
      }, [_vm._t("page", [_vm._v("\n      " + _vm._s(page.text) + "\n    ")], null, page)], 2);
    }), _vm.simple ? _c('li', {
      class: _vm.b('page-desc')
    }, [_vm._t("pageDesc", [_vm._v("\n      " + _vm._s(_vm.value + " / " + _vm.count) + "\n    ")])], 2) : _vm._e(), _c('li', {
      class: [_vm.b('item', {
        disabled: _vm.value === _vm.count
      }), _vm.b('next'), 'vm-hairline'],
      on: {
        "click": function click($event) {
          return _vm.onSelect(_vm.value + 1);
        }
      }
    }, [_vm._t("next-text", [_vm._v("\n      " + _vm._s(_vm.nextText || "下一页") + "\n    ")])], 2)], 2);
  },
  name: "pagination",
  props: {
    prevText: String,
    nextText: String,
    forceEllipses: Boolean,
    mode: {
      type: String,
      default: "multi"
    },
    value: {
      type: Number,
      default: 0
    },
    pageCount: {
      type: [Number, String],
      default: 0
    },
    totalItems: {
      type: [Number, String],
      default: 0
    },
    itemsPerPage: {
      type: [Number, String],
      default: 10
    },
    showPageSize: {
      type: [Number, String],
      default: 5
    }
  },
  computed: {
    simple: function simple() {
      return this.mode !== "multi";
    },
    count: function count() {
      var count = this.pageCount || Math.ceil(this.totalItems / this.itemsPerPage);
      return Math.max(1, count);
    },
    pages: function pages() {
      var pages = [];
      var pageCount = this.count;
      var showPageSize = +this.showPageSize;

      if (this.mode !== "multi") {
        return pages;
      } // Default page limits


      var startPage = 1;
      var endPage = pageCount;
      var isMaxSized = showPageSize < pageCount; // recompute if showPageSize

      if (isMaxSized) {
        // Current page is displayed in the middle of the visible ones
        startPage = Math.max(this.value - Math.floor(showPageSize / 2), 1);
        endPage = startPage + showPageSize - 1; // Adjust if limit is exceeded

        if (endPage > pageCount) {
          endPage = pageCount;
          startPage = endPage - showPageSize + 1;
        }
      } // Add page number links


      for (var number = startPage; number <= endPage; number++) {
        var page = makePage(number, number, number === this.value);
        pages.push(page);
      } // Add links to move between page sets


      if (isMaxSized && showPageSize > 0 && this.forceEllipses) {
        if (startPage > 1) {
          var previousPageSet = makePage(startPage - 1, "...", false);
          pages.unshift(previousPageSet);
        }

        if (endPage < pageCount) {
          var nextPageSet = makePage(endPage + 1, "...", false);
          pages.push(nextPageSet);
        }
      }

      return pages;
    }
  },
  watch: {
    value: {
      handler: function handler(page) {
        this.select(page || this.value);
      },
      immediate: true
    }
  },
  methods: {
    select: function select(page, emitChange) {
      page = Math.min(this.count, Math.max(1, page));

      if (this.value !== page) {
        this.$emit("input", page);

        if (emitChange) {
          this.$emit("change", page);
        }
      }
    },
    onSelect: function onSelect(value) {
      this.select(value, true);
    }
  }
});

exports.default = _default;