'use strict';

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

var normalizeComponent = require('./normalize-component-6fa50395.js');
var ThemeMixin = require('./ThemeMixin-a2f817cd.js');

//
var script = {
  name: 'VPagination',
  props: {
    title: {
      type: String,
      default: 'Page'
    },
    offset: {
      type: Number,
      required: true
    },
    limit: {
      required: true,
      validator: function validator(value) {
        return !!value && Number(value) > 0;
      } // 1+

    },
    total: {
      type: Number,
      default: 0
    },
    pagesVisible: {
      default: 0,
      validator: function validator(value) {
        return value === 0 || value % 2 === 1;
      } // 0, 1, 3, 5 ...

    }
  },
  computed: {
    currentPage: function currentPage() {
      return Math.floor(this.offset / this.limit) + 1;
    },
    halfPagesVisible: function halfPagesVisible() {
      return Math.floor(this.pagesVisible / 2);
    },
    totalPages: function totalPages() {
      return Math.ceil(this.total / this.limit);
    },
    allVisiblePages: function allVisiblePages() {
      // visible = 3
      // [[1], 2, 3] = []
      // [1, [2], 3] = [1]
      // [6, 7, [8]] = [6, 7]
      var startPointBefore = Math.max(1, this.currentPage - this.halfPagesVisible);
      var maxPages = this.totalPages ? this.totalPages + 1 : this.currentPage + this.pagesVisible;
      var startPointByVisiblePages = Math.max(1, maxPages - this.pagesVisible);
      var startPoint = Math.min(startPointBefore, startPointByVisiblePages);
      var endPoint = Math.min(maxPages, startPoint + this.pagesVisible);
      var countTotalPages = endPoint - startPoint;
      return Array.from(Array(countTotalPages), function (d, i) {
        return startPoint + i;
      });
    },
    isInfinityPages: function isInfinityPages() {
      return this.total === 0;
    },
    hasVisiblePages: function hasVisiblePages() {
      return !!this.pagesVisible;
    },
    hasBack: function hasBack() {
      return this.currentPage !== 1;
    },
    hasForward: function hasForward() {
      if (this.isInfinityPages) {
        return true; // infinity next
      }

      return this.totalPages > this.currentPage;
    },
    showFirstPage: function showFirstPage() {
      if (!this.hasVisiblePages) {
        return false;
      }

      return !this.allVisiblePages.includes(1);
    },
    isShowFirstPageDots: function isShowFirstPageDots() {
      if (!this.hasVisiblePages) {
        return false;
      }

      if (this.allVisiblePages.includes(2)) {
        return false;
      }

      return this.currentPage > 2;
    },
    showLastPage: function showLastPage() {
      if (!this.hasVisiblePages) {
        return false;
      }

      return !this.isInfinityPages && !this.allVisiblePages.includes(this.totalPages);
    },
    showLastPageDots: function showLastPageDots() {
      if (!this.hasVisiblePages) {
        return false;
      }

      var nearPageNumber = this.totalPages - 1;

      if (this.allVisiblePages.includes(nearPageNumber)) {
        return false;
      }

      if (this.isInfinityPages) {
        return true;
      }

      return this.totalPages > 2;
    },
    showCurrentPageTitle: function showCurrentPageTitle() {
      return this.pagesVisible === 0;
    }
  },
  methods: {
    goToFirst: function goToFirst() {
      this.goToPage(1);
    },
    goToLast: function goToLast() {
      this.goToPage(this.totalPages);
    },
    goToPrev: function goToPrev() {
      this.goToPage(this.currentPage - 1);
    },
    goToNext: function goToNext() {
      this.goToPage(this.currentPage + 1);
    },
    goToPage: function goToPage(pageNumber) {
      if (pageNumber > 0 && this.currentPage === pageNumber) {
        return;
      }

      var nextOffset = (pageNumber - 1) * this.limit;
      this.$emit('offset', nextOffset);
    }
  },
  mixins: [ThemeMixin.ThemeMixin]
};

/* script */
const __vue_script__ = script;

/* template */
var __vue_render__ = function () {var _vm=this;var _h=_vm.$createElement;var _c=_vm._self._c||_h;return _c('div',{staticClass:"v-pagination",class:_vm.themeCssClass},[_c('div',{staticClass:"v-pagination-navigation"},[(_vm.showCurrentPageTitle)?_c('span',{staticClass:"v-pagination-node"},[_vm._v(_vm._s(_vm.title)+" "+_vm._s(_vm.currentPage))]):_vm._e(),_vm._v(" "),_c('a',{staticClass:"v-pagination-node",attrs:{"disabled":!_vm.hasBack,"data-test":"pagination-prev"},on:{"click":function($event){$event.preventDefault();return _vm.goToPrev()}}},[_c('span',{staticClass:"v-pagination-triangle-left"})]),_vm._v(" "),(_vm.showFirstPage)?_c('a',{staticClass:"v-pagination-node",attrs:{"data-test":"btn-goto-first"},on:{"click":function($event){$event.preventDefault();return _vm.goToFirst()}}},[_vm._v("\n      1\n    ")]):_vm._e(),_vm._v(" "),(_vm.isShowFirstPageDots)?_c('span',{staticClass:"v-pagination-node",attrs:{"data-test":"dots-left"}},[_vm._v("...")]):_vm._e(),_vm._v(" "),_vm._l((_vm.allVisiblePages),function(num){return _c('a',{key:num,staticClass:"v-pagination-node",attrs:{"data-test":"btn-goto-page","data-test-active":_vm.currentPage === num},on:{"click":function($event){$event.preventDefault();return _vm.goToPage(num)}}},[_vm._v("\n      "+_vm._s(num)+"\n      "),(_vm.currentPage === num)?_c('span',{staticClass:"v-pagination-active-foot",attrs:{"data-test":"active-page-indicator"}}):_vm._e()])}),_vm._v(" "),(_vm.showLastPageDots)?_c('span',{staticClass:"v-pagination-node",attrs:{"data-test":"dots-right"}},[_vm._v("...")]):_vm._e(),_vm._v(" "),(_vm.showLastPage)?_c('a',{staticClass:"v-pagination-node",attrs:{"data-test":"btn-goto-last"},on:{"click":function($event){$event.preventDefault();return _vm.goToLast()}}},[_vm._v("\n      "+_vm._s(_vm.totalPages)+"\n    ")]):_vm._e(),_vm._v(" "),_c('a',{staticClass:"v-pagination-node",attrs:{"disabled":!_vm.hasForward,"data-test":"pagination-next"},on:{"click":function($event){$event.preventDefault();return _vm.goToNext()}}},[_c('span',{staticClass:"v-pagination-triangle-right"})])],2)])};
var __vue_staticRenderFns__ = [];

  /* style */
  const __vue_inject_styles__ = undefined;
  /* scoped */
  const __vue_scope_id__ = undefined;
  /* module identifier */
  const __vue_module_identifier__ = undefined;
  /* functional template */
  const __vue_is_functional_template__ = false;
  /* style inject */
  
  /* style inject SSR */
  

  
  var VPagination = normalizeComponent.__vue_normalize__(
    { render: __vue_render__, staticRenderFns: __vue_staticRenderFns__ },
    __vue_inject_styles__,
    __vue_script__,
    __vue_scope_id__,
    __vue_is_functional_template__,
    __vue_module_identifier__,
    undefined,
    undefined
  );

exports.default = VPagination;
