'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var isNum = _interopDefault(require('./isNum'));

/**
 * 分页参数类，创建一个分页对象
 * @param {number} pn
 * @param {number} ps
 * @returns {
 *   pn: number;
 *   ps: number;
 *   pageNum: number;
 *   pageSize: number;
 *   total: number;
 *   hasMore: boolean
 * }
 *
 * @example
 * import { PageParams } from '@bilibili/b-utils';
 *
 * const pageParams = new PageParams(1, 10);
 * // total未赋值时，hasMore为true
 * console.log(pageParams.hasMore); // true;
 *
 * pageParams.pn = 2;
 * pageParams.total = 9;
 * // total赋值后，hasMore将被计算出来
 * console.log(pageParams.hasMore); // false;
 */
function PageParams(pn, ps) {
  this.pn = isNum(pn) ? pn : 1;
  this.ps = isNum(ps) ? ps : 10;
  this._total = 0;
  this._hasMore = true;
  this._totalInited = false;
}

Object.defineProperties(PageParams.prototype, {
  pageNum: {
    get: function get() {
      return this.pn;
    },
    set: function set(val) {
      this.pn = val;
    }
  },
  pageSize: {
    get: function get() {
      return this.ps;
    },
    set: function set(val) {
      this.ps = val;
    }
  },

  /**
   * 设置total
   */
  total: {
    get: function get() {
      return this._total;
    },
    set: function set(val) {
      this._total = val;
      this._totalInited = true;
    }
  },

  /**
   * 根据total、pn、ps计算是否还有更多数据
   */
  hasMore: {
    get: function get() {
      return this._hasMore && (!this._totalInited || this.total > this.ps * (this.pn - 1));
    },
    set: function set(val) {
      this._hasMore = val;
    }
  }
});

module.exports = PageParams;
