"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

// Class that acts like a WeakMap, except the key consists of an
// array of multiple objects, instead of being just a single object.
// Intended to allow caching of methods which take more than one argument.
var MultiWeakMap =
/*#__PURE__*/
function () {
  function MultiWeakMap(numKeys) {
    (0, _classCallCheck2.default)(this, MultiWeakMap);

    if (typeof numKeys !== 'number') {
      throw new Error('MultiWeakMap numKeys must be a number');
    }

    if (numKeys <= 0) {
      throw new Error('MultiWeakMap numKeys must be positive');
    }

    this._root = new WeakMap();
    this._numKeys = numKeys;
  }

  (0, _createClass2.default)(MultiWeakMap, [{
    key: "has",
    value: function has(keys) {
      var cursor = this._root;

      for (var idx = 0; idx < this._numKeys; idx++) {
        var key = keys[idx];

        if (!cursor.has(key)) {
          return false;
        }

        cursor = cursor.get(key);
      }

      return true;
    }
  }, {
    key: "get",
    value: function get(keys) {
      var cursor = this._root;

      for (var idx = 0; idx < this._numKeys; idx++) {
        var key = keys[idx];

        if (!cursor.has(key)) {
          return void 0;
        }

        cursor = cursor.get(key);
      }

      return cursor;
    }
  }, {
    key: "set",
    value: function set(keys, value) {
      var cursor = this._root;

      for (var idx = 0; idx < this._numKeys - 1; idx++) {
        var key = keys[idx];

        if (!cursor.has(key)) {
          cursor.set(key, new WeakMap());
        }

        cursor = cursor.get(key);
      }

      cursor.set(keys[this._numKeys - 1], value);
    }
  }]);
  return MultiWeakMap;
}();

exports.default = MultiWeakMap;