function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { extend } from '../../vue';
import { NAME_PAGINATION } from '../../constants/components';
import { EVENT_NAME_CHANGE, EVENT_NAME_PAGE_CLICK } from '../../constants/events';
import { PROP_TYPE_NUMBER_STRING, PROP_TYPE_STRING } from '../../constants/props';
import { BvEvent } from '../../utils/bv-event.class';
import { attemptFocus, isVisible } from '../../utils/dom';
import { isUndefinedOrNull } from '../../utils/inspect';
import { mathCeil, mathMax } from '../../utils/math';
import { toInteger } from '../../utils/number';
import { sortKeys } from '../../utils/object';
import { makeProp, makePropsConfigurable } from '../../utils/props';
import { MODEL_PROP_NAME, paginationMixin, props as paginationProps } from '../../mixins/pagination';

// --- Constants ---

var DEFAULT_PER_PAGE = 20;
var DEFAULT_TOTAL_ROWS = 0;

// --- Helper methods ---

// Sanitize the provided per page number (converting to a number)
var sanitizePerPage = function sanitizePerPage(value) {
  return mathMax(toInteger(value) || DEFAULT_PER_PAGE, 1);
};

// Sanitize the provided total rows number (converting to a number)
var sanitizeTotalRows = function sanitizeTotalRows(value) {
  return mathMax(toInteger(value) || DEFAULT_TOTAL_ROWS, 0);
};

// --- Props ---

export var props = makePropsConfigurable(sortKeys(_objectSpread(_objectSpread({}, paginationProps), {}, {
  ariaControls: makeProp(PROP_TYPE_STRING),
  perPage: makeProp(PROP_TYPE_NUMBER_STRING, DEFAULT_PER_PAGE),
  totalRows: makeProp(PROP_TYPE_NUMBER_STRING, DEFAULT_TOTAL_ROWS)
})), NAME_PAGINATION);

// --- Main component ---

// @vue/component
export var BPagination = /*#__PURE__*/extend({
  name: NAME_PAGINATION,
  // The render function is brought in via the `paginationMixin`
  mixins: [paginationMixin],
  props: props,
  computed: {
    numberOfPages: function numberOfPages() {
      var result = mathCeil(sanitizeTotalRows(this.totalRows) / sanitizePerPage(this.perPage));
      return result < 1 ? 1 : result;
    },
    // Used for watching changes to `perPage` and `numberOfPages`
    pageSizeNumberOfPages: function pageSizeNumberOfPages() {
      return {
        perPage: sanitizePerPage(this.perPage),
        totalRows: sanitizeTotalRows(this.totalRows),
        numberOfPages: this.numberOfPages
      };
    }
  },
  watch: {
    pageSizeNumberOfPages: function pageSizeNumberOfPages(newValue, oldValue) {
      if (!isUndefinedOrNull(oldValue)) {
        if (newValue.perPage !== oldValue.perPage && newValue.totalRows === oldValue.totalRows) {
          // If the page size changes, reset to page 1
          this.currentPage = 1;
        } else if (newValue.numberOfPages !== oldValue.numberOfPages && this.currentPage > newValue.numberOfPages) {
          // If `numberOfPages` changes and is less than
          // the `currentPage` number, reset to page 1
          this.currentPage = 1;
        }
      }
      this.localNumberOfPages = newValue.numberOfPages;
    }
  },
  created: function created() {
    var _this = this;
    // Set the initial page count
    this.localNumberOfPages = this.numberOfPages;
    // Set the initial page value
    var currentPage = toInteger(this[MODEL_PROP_NAME], 0);
    if (currentPage > 0) {
      this.currentPage = currentPage;
    } else {
      this.$nextTick(function () {
        // If this value parses to `NaN` or a value less than `1`
        // trigger an initial emit of `null` if no page specified
        _this.currentPage = 0;
      });
    }
  },
  methods: {
    // These methods are used by the render function
    onClick: function onClick(event, pageNumber) {
      var _this2 = this;
      // Dont do anything if clicking the current active page
      if (pageNumber === this.currentPage) {
        return;
      }
      var target = event.target;

      // Emit a user-cancelable `page-click` event
      var clickEvent = new BvEvent(EVENT_NAME_PAGE_CLICK, {
        cancelable: true,
        vueTarget: this,
        target: target
      });
      this.$emit(clickEvent.type, clickEvent, pageNumber);
      if (clickEvent.defaultPrevented) {
        return;
      }

      // Update the `v-model`
      this.currentPage = pageNumber;
      // Emit event triggered by user interaction
      this.$emit(EVENT_NAME_CHANGE, this.currentPage);

      // Keep the current button focused if possible
      this.$nextTick(function () {
        if (isVisible(target) && _this2.$el.contains(target)) {
          attemptFocus(target);
        } else {
          _this2.focusCurrent();
        }
      });
    },
    makePage: function makePage(pageNum) {
      return pageNum;
    },
    /* istanbul ignore next */linkProps: function linkProps() {
      // No props, since we render a plain button
      return {};
    }
  }
});