"use strict";
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

require("core-js/modules/es.array.slice.js");

require("core-js/modules/es.function.name.js");

require("core-js/modules/es.array.from.js");

require("core-js/modules/es.regexp.exec.js");

require("core-js/modules/es.object.get-prototype-of.js");

require("core-js/modules/es.reflect.construct.js");

require("core-js/modules/es.reflect.get.js");

require("core-js/modules/es.object.get-own-property-descriptor.js");

require("core-js/modules/es.array.iterator.js");

require("core-js/modules/es.map.js");

require("core-js/modules/es.object.to-string.js");

require("core-js/modules/es.string.iterator.js");

require("core-js/modules/web.dom-collections.iterator.js");

require("core-js/modules/es.symbol.iterator.js");

require("core-js/modules/es.symbol.js");

require("core-js/modules/es.symbol.description.js");

require("core-js/modules/web.dom-collections.for-each.js");

require("core-js/modules/es.symbol.to-string-tag.js");

require("core-js/modules/es.json.to-string-tag.js");

require("core-js/modules/es.math.to-string-tag.js");

function _get() { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(arguments.length < 3 ? target : receiver); } return desc.value; }; } return _get.apply(this, arguments); }

function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } Object.defineProperty(subClass, "prototype", { value: Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }), writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, 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 normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

var _a;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.LRUCache = exports.LinkedMap = exports.Touch = void 0;
var Touch;

(function (Touch) {
  Touch.None = 0;
  Touch.First = 1;
  Touch.AsOld = Touch.First;
  Touch.Last = 2;
  Touch.AsNew = Touch.Last;
})(Touch || (exports.Touch = Touch = {}));

var LinkedMap = /*#__PURE__*/function (_ref) {
  function LinkedMap() {
    _classCallCheck(this, LinkedMap);

    this[_a] = 'LinkedMap';
    this._map = new Map();
    this._head = undefined;
    this._tail = undefined;
    this._size = 0;
    this._state = 0;
  }

  _createClass(LinkedMap, [{
    key: "clear",
    value: function clear() {
      this._map.clear();

      this._head = undefined;
      this._tail = undefined;
      this._size = 0;
      this._state++;
    }
  }, {
    key: "isEmpty",
    value: function isEmpty() {
      return !this._head && !this._tail;
    }
  }, {
    key: "size",
    get: function get() {
      return this._size;
    }
  }, {
    key: "first",
    get: function get() {
      var _this$_head;

      return (_this$_head = this._head) === null || _this$_head === void 0 ? void 0 : _this$_head.value;
    }
  }, {
    key: "last",
    get: function get() {
      var _this$_tail;

      return (_this$_tail = this._tail) === null || _this$_tail === void 0 ? void 0 : _this$_tail.value;
    }
  }, {
    key: "has",
    value: function has(key) {
      return this._map.has(key);
    }
  }, {
    key: "get",
    value: function get(key) {
      var touch = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Touch.None;

      var item = this._map.get(key);

      if (!item) {
        return undefined;
      }

      if (touch !== Touch.None) {
        this.touch(item, touch);
      }

      return item.value;
    }
  }, {
    key: "set",
    value: function set(key, value) {
      var touch = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : Touch.None;

      var item = this._map.get(key);

      if (item) {
        item.value = value;

        if (touch !== Touch.None) {
          this.touch(item, touch);
        }
      } else {
        item = {
          key: key,
          value: value,
          next: undefined,
          previous: undefined
        };

        switch (touch) {
          case Touch.None:
            this.addItemLast(item);
            break;

          case Touch.First:
            this.addItemFirst(item);
            break;

          case Touch.Last:
            this.addItemLast(item);
            break;

          default:
            this.addItemLast(item);
            break;
        }

        this._map.set(key, item);

        this._size++;
      }

      return this;
    }
  }, {
    key: "delete",
    value: function _delete(key) {
      return !!this.remove(key);
    }
  }, {
    key: "remove",
    value: function remove(key) {
      var item = this._map.get(key);

      if (!item) {
        return undefined;
      }

      this._map.delete(key);

      this.removeItem(item);
      this._size--;
      return item.value;
    }
  }, {
    key: "shift",
    value: function shift() {
      if (!this._head && !this._tail) {
        return undefined;
      }

      if (!this._head || !this._tail) {
        throw new Error('Invalid list');
      }

      var item = this._head;

      this._map.delete(item.key);

      this.removeItem(item);
      this._size--;
      return item.value;
    }
  }, {
    key: "forEach",
    value: function forEach(callbackfn, thisArg) {
      var state = this._state;
      var current = this._head;

      while (current) {
        if (thisArg) {
          callbackfn.bind(thisArg)(current.value, current.key, this);
        } else {
          callbackfn(current.value, current.key, this);
        }

        if (this._state !== state) {
          throw new Error("LinkedMap got modified during iteration.");
        }

        current = current.next;
      }
    }
  }, {
    key: "keys",
    value: function keys() {
      var _iterator;

      var map = this;
      var state = this._state;
      var current = this._head;
      var iterator = (_iterator = {}, _defineProperty(_iterator, Symbol.iterator, function () {
        return iterator;
      }), _defineProperty(_iterator, "next", function next() {
        if (map._state !== state) {
          throw new Error("LinkedMap got modified during iteration.");
        }

        if (current) {
          var result = {
            value: current.key,
            done: false
          };
          current = current.next;
          return result;
        } else {
          return {
            value: undefined,
            done: true
          };
        }
      }), _iterator);
      return iterator;
    }
  }, {
    key: "values",
    value: function values() {
      var _iterator2;

      var map = this;
      var state = this._state;
      var current = this._head;
      var iterator = (_iterator2 = {}, _defineProperty(_iterator2, Symbol.iterator, function () {
        return iterator;
      }), _defineProperty(_iterator2, "next", function next() {
        if (map._state !== state) {
          throw new Error("LinkedMap got modified during iteration.");
        }

        if (current) {
          var result = {
            value: current.value,
            done: false
          };
          current = current.next;
          return result;
        } else {
          return {
            value: undefined,
            done: true
          };
        }
      }), _iterator2);
      return iterator;
    }
  }, {
    key: "entries",
    value: function entries() {
      var _iterator3;

      var map = this;
      var state = this._state;
      var current = this._head;
      var iterator = (_iterator3 = {}, _defineProperty(_iterator3, Symbol.iterator, function () {
        return iterator;
      }), _defineProperty(_iterator3, "next", function next() {
        if (map._state !== state) {
          throw new Error("LinkedMap got modified during iteration.");
        }

        if (current) {
          var result = {
            value: [current.key, current.value],
            done: false
          };
          current = current.next;
          return result;
        } else {
          return {
            value: undefined,
            done: true
          };
        }
      }), _iterator3);
      return iterator;
    }
  }, {
    key: _ref,
    value: function value() {
      return this.entries();
    }
  }, {
    key: "trimOld",
    value: function trimOld(newSize) {
      if (newSize >= this.size) {
        return;
      }

      if (newSize === 0) {
        this.clear();
        return;
      }

      var current = this._head;
      var currentSize = this.size;

      while (current && currentSize > newSize) {
        this._map.delete(current.key);

        current = current.next;
        currentSize--;
      }

      this._head = current;
      this._size = currentSize;

      if (current) {
        current.previous = undefined;
      }

      this._state++;
    }
  }, {
    key: "addItemFirst",
    value: function addItemFirst(item) {
      // First time Insert
      if (!this._head && !this._tail) {
        this._tail = item;
      } else if (!this._head) {
        throw new Error('Invalid list');
      } else {
        item.next = this._head;
        this._head.previous = item;
      }

      this._head = item;
      this._state++;
    }
  }, {
    key: "addItemLast",
    value: function addItemLast(item) {
      // First time Insert
      if (!this._head && !this._tail) {
        this._head = item;
      } else if (!this._tail) {
        throw new Error('Invalid list');
      } else {
        item.previous = this._tail;
        this._tail.next = item;
      }

      this._tail = item;
      this._state++;
    }
  }, {
    key: "removeItem",
    value: function removeItem(item) {
      if (item === this._head && item === this._tail) {
        this._head = undefined;
        this._tail = undefined;
      } else if (item === this._head) {
        // This can only happend if size === 1 which is handle
        // by the case above.
        if (!item.next) {
          throw new Error('Invalid list');
        }

        item.next.previous = undefined;
        this._head = item.next;
      } else if (item === this._tail) {
        // This can only happend if size === 1 which is handle
        // by the case above.
        if (!item.previous) {
          throw new Error('Invalid list');
        }

        item.previous.next = undefined;
        this._tail = item.previous;
      } else {
        var next = item.next;
        var previous = item.previous;

        if (!next || !previous) {
          throw new Error('Invalid list');
        }

        next.previous = previous;
        previous.next = next;
      }

      item.next = undefined;
      item.previous = undefined;
      this._state++;
    }
  }, {
    key: "touch",
    value: function touch(item, _touch) {
      if (!this._head || !this._tail) {
        throw new Error('Invalid list');
      }

      if (_touch !== Touch.First && _touch !== Touch.Last) {
        return;
      }

      if (_touch === Touch.First) {
        if (item === this._head) {
          return;
        }

        var next = item.next;
        var previous = item.previous; // Unlink the item

        if (item === this._tail) {
          // previous must be defined since item was not head but is tail
          // So there are more than on item in the map
          previous.next = undefined;
          this._tail = previous;
        } else {
          // Both next and previous are not undefined since item was neither head nor tail.
          next.previous = previous;
          previous.next = next;
        } // Insert the node at head


        item.previous = undefined;
        item.next = this._head;
        this._head.previous = item;
        this._head = item;
        this._state++;
      } else if (_touch === Touch.Last) {
        if (item === this._tail) {
          return;
        }

        var _next = item.next;
        var _previous = item.previous; // Unlink the item.

        if (item === this._head) {
          // next must be defined since item was not tail but is head
          // So there are more than on item in the map
          _next.previous = undefined;
          this._head = _next;
        } else {
          // Both next and previous are not undefined since item was neither head nor tail.
          _next.previous = _previous;
          _previous.next = _next;
        }

        item.next = undefined;
        item.previous = this._tail;
        this._tail.next = item;
        this._tail = item;
        this._state++;
      }
    }
  }, {
    key: "toJSON",
    value: function toJSON() {
      var data = [];
      this.forEach(function (value, key) {
        data.push([key, value]);
      });
      return data;
    }
  }, {
    key: "fromJSON",
    value: function fromJSON(data) {
      this.clear();

      var _iterator4 = _createForOfIteratorHelper(data),
          _step;

      try {
        for (_iterator4.s(); !(_step = _iterator4.n()).done;) {
          var _step$value = _slicedToArray(_step.value, 2),
              key = _step$value[0],
              value = _step$value[1];

          this.set(key, value);
        }
      } catch (err) {
        _iterator4.e(err);
      } finally {
        _iterator4.f();
      }
    }
  }]);

  return LinkedMap;
}((_a = Symbol.toStringTag, Symbol.iterator));

exports.LinkedMap = LinkedMap;

var LRUCache = /*#__PURE__*/function (_LinkedMap) {
  _inherits(LRUCache, _LinkedMap);

  var _super = _createSuper(LRUCache);

  function LRUCache(limit) {
    var _this;

    var ratio = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;

    _classCallCheck(this, LRUCache);

    _this = _super.call(this);
    _this._limit = limit;
    _this._ratio = Math.min(Math.max(0, ratio), 1);
    return _this;
  }

  _createClass(LRUCache, [{
    key: "limit",
    get: function get() {
      return this._limit;
    },
    set: function set(limit) {
      this._limit = limit;
      this.checkTrim();
    }
  }, {
    key: "ratio",
    get: function get() {
      return this._ratio;
    },
    set: function set(ratio) {
      this._ratio = Math.min(Math.max(0, ratio), 1);
      this.checkTrim();
    }
  }, {
    key: "get",
    value: function get(key) {
      var touch = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Touch.AsNew;
      return _get(_getPrototypeOf(LRUCache.prototype), "get", this).call(this, key, touch);
    }
  }, {
    key: "peek",
    value: function peek(key) {
      return _get(_getPrototypeOf(LRUCache.prototype), "get", this).call(this, key, Touch.None);
    }
  }, {
    key: "set",
    value: function set(key, value) {
      _get(_getPrototypeOf(LRUCache.prototype), "set", this).call(this, key, value, Touch.Last);

      this.checkTrim();
      return this;
    }
  }, {
    key: "checkTrim",
    value: function checkTrim() {
      if (this.size > this._limit) {
        this.trimOld(Math.round(this._limit * this._ratio));
      }
    }
  }]);

  return LRUCache;
}(LinkedMap);

exports.LRUCache = LRUCache;