"use strict";

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

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

var _MultiWeakMap = _interopRequireDefault(require("./MultiWeakMap"));

/* eslint "no-param-reassign": "warn" */
// Method decorator that uses a MultiWeakMap to cache the results of calls
// for argument combinations previously seen before. See tests for examples.
var _default = function _default(target, name, descriptor) {
  var decoratedMethod = descriptor.value;
  var arity = decoratedMethod.length;
  var cache = new _MultiWeakMap.default(arity);

  descriptor.value = function () {
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    if (args.length !== arity) {
      throw new Error("@withCachedArguments-decorated function expected ".concat(arity, " args but got ").concat(args.length));
    }

    if (cache.has(args)) {
      return cache.get(args);
    }

    var result = decoratedMethod.apply(this, args);
    cache.set(args, result);
    return result;
  };

  return descriptor;
};

exports.default = _default;