"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _ApiClient = require("./ApiClient");
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 _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), 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); } /*
 * Nutanix Networking APIs
 *
 * OpenAPI spec version: 4.0.1
 *
 * NOTE: This class is auto generated by the Open API Dev Platform.
 *
 * Open API Dev Platform Codegen version: 16.9.0.8546-RELEASE
 *
 * Do not edit the class manually.
 *
 */
/**
* A model that represents a paginable response from a list endpoint.
* @module Paginable
* @version 4.0.1
*/
var Paginable = exports["default"] = /*#__PURE__*/function () {
  /**
  * Constructs a new <code>Paginable</code>.
  * @alias module:Paginable
  * @class
  */
  function Paginable() {
    _classCallCheck(this, Paginable);
  }

  /**
  * Constructs a <code>Paginable</code> from a plain Javascript object, optionally creating a new instance.
  * Copies all relevant properties from <code>data</code> to <code>obj</code> if supplied or a new instance if not.
  * @param {Object} metadata The plain Javascript object bearing properties of interest.
  * @param {module:Paginable} obj Optional instance to populate.
  * @return {module:Paginable} The populated <code>Paginable</code> instance.
  */
  return _createClass(Paginable, [{
    key: "getFirstPage",
    value:
    /**
    * Get the first page of the set, if any
    * @return {Promise} a {@link https://www.promisejs.org/|Promise}, resolves with another Paginable
    *                   or rejects with error message if any issue (link not available, ...)
    */
    function getFirstPage() {
      if (this.isFirstPage()) {
        return Promise.reject('Cannot navigate to first page : already on first page');
      }
      if (this.linkFirst === null) {
        return Promise.reject('No link to first page provided in previous API response');
      }
      return this._getLink(this.linkFirst);
    }

    /**
    * Get the previous page of the set, if any
    * @return {Promise} a {@link https://www.promisejs.org/|Promise}, resolves with another Paginable
    *                   or rejects with error message if any issue (link not available, ...)
    */
  }, {
    key: "getPrevPage",
    value: function getPrevPage() {
      if (this.isFirstPage()) {
        return Promise.reject('Cannot navigate to previous page : already on first page');
      }
      if (this.linkPrev === null) {
        return Promise.reject('No link to previous page provided in previous API response');
      }
      return this._getLink(this.linkPrev);
    }

    /**
    * Get the next page of the set, if any
    * @return {Promise} a {@link https://www.promisejs.org/|Promise}, resolves with another Paginable
    *                   or rejects with error message if any issue (link not available, ...)
    */
  }, {
    key: "getNextPage",
    value: function getNextPage() {
      if (this.isLastPage()) {
        return Promise.reject('Cannot navigate to next page : already on last page');
      }
      if (this.linkNext === null) {
        return Promise.reject('No link to next page provided in previous API response');
      }
      return this._getLink(this.linkNext);
    }

    /**
    * Get the last page of the set, if any
    * @return {Promise} a {@link https://www.promisejs.org/|Promise}, resolves with another Paginable
    *                   or rejects with error message if any issue (link not available, ...)
    */
  }, {
    key: "getLastPage",
    value: function getLastPage() {
      if (this.isLastPage()) {
        return Promise.reject('Cannot navigate to last page : already on last page');
      }
      if (this.linkLast === null) {
        return Promise.reject('No link to last page provided in previous API response');
      }
      return this._getLink(this.linkLast);
    }

    /**
    * Get the current page number
    * @return {Number} the current page number, starts at 0
    */
  }, {
    key: "currentPage",
    value: function currentPage() {
      if (this.page === null) {
        return 0;
      }
      return this.page;
    }

    /**
    * Get the total number of pages
    * @return {Number} the total number of pages available
    */
  }, {
    key: "countPages",
    value: function countPages() {
      if (this.resultsPerPage === null || this.resultsTotal === null || this.resultsPerPage === 0) {
        return 1;
      }
      return Math.ceil(this.resultsTotal / this.resultsPerPage);
    }

    /**
    * Check if current page is first page
    * @return {Boolean} True if current page is 0, false otherwise
    */
  }, {
    key: "isFirstPage",
    value: function isFirstPage() {
      return this.currentPage() === 0;
    }

    /**
    * Check if current page is last page
    * @return {Boolean} True if current page is countPages()-1, false otherwise
    */
  }, {
    key: "isLastPage",
    value: function isLastPage() {
      return this.currentPage() === this.countPages() - 1;
    }
  }, {
    key: "populatePaginationMetadata",
    value: function populatePaginationMetadata(apiClient, page, resultsPerPage) {
      this.apiClient = apiClient;
      this.page = page;
      this.resultsPerPage = resultsPerPage;
    }
  }, {
    key: "_getLink",
    value: function _getLink(path) {
      // If no ApiClient defined, instantiate default one
      if (this.apiClient === null) {
        this.apiClient = _ApiClient.ApiClient.instance;
      }
      var postBody = {};
      var pathParams = {};
      var queryParams = {};
      var headerParams = {};
      var formParams = {};
      var authNames = [];
      var contentTypes = [];
      var accepts = ['application/json'];
      var returnType = this.constructor;
      return this.apiClient.callApi(path, 'GET', pathParams, queryParams, headerParams, formParams, postBody, authNames, contentTypes, accepts, returnType);
    }
  }], [{
    key: "constructFromObject",
    value: function constructFromObject(metadata, obj) {
      if (metadata) {
        obj = obj || new Paginable();
        if (metadata.hasOwnProperty('links') && metadata['links'] !== null && metadata['links'] !== undefined) {
          var _iterator = _createForOfIteratorHelper(metadata['links']),
            _step;
          try {
            for (_iterator.s(); !(_step = _iterator.n()).done;) {
              var link = _step.value;
              if (link.hasOwnProperty('rel') && link.hasOwnProperty('href')) {
                switch (link['rel']) {
                  case 'first':
                    obj.linkFirst = link['href'];
                    break;
                  case 'prev':
                    obj.linkPrev = link['href'];
                    break;
                  case 'next':
                    obj.linkNext = link['href'];
                    break;
                  case 'last':
                    obj.linkLast = link['href'];
                    break;
                  default:
                    break;
                }
              }
            }
          } catch (err) {
            _iterator.e(err);
          } finally {
            _iterator.f();
          }
        }
        if (metadata.hasOwnProperty('totalAvailableResults')) {
          obj.resultsTotal = metadata['totalAvailableResults'];
        }
      }
      return obj;
    }
  }]);
}();
/**
* The link to first page, if any
* @type {String}
* @memberOf Paginable
*/
Paginable.prototype.linkFirst = null;

/**
* The link to previous page, if any
* @type {String}
* @memberOf Paginable
*/
Paginable.prototype.linkPrev = null;

/**
* The link to next page, if any
* @type {String}
* @memberOf Paginable
*/
Paginable.prototype.linkNext = null;

/**
* The link to last page, if any
* @type {String}
* @memberOf Paginable
*/
Paginable.prototype.linkLast = null;

/**
* The ApiClient instance to use for further query
* @type {ApiClient}
* @memberOf Paginable
*/
Paginable.prototype.apiClient = null;

/**
* Current page number
* @type {Number}
* @memberOf Paginable
*/
Paginable.prototype.page = null;

/**
* Number of results per page
* @type {Number}
* @memberOf Paginable
*/
Paginable.prototype.resultsPerPage = null;

/**
* Number of total results available
* @type {Number}
* @memberOf Paginable
*/
Paginable.prototype.resultsTotal = null;