'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _MultiWeakMap = require('./MultiWeakMap');

var _MultiWeakMap2 = _interopRequireDefault(_MultiWeakMap);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// Method decorator that uses a MultiWeakMap to cache the results of calls
// for argument combinations previously seen before. See tests for examples.
exports.default = function (target, name, descriptor) {
  var decoratedMethod = descriptor.value;
  var arity = decoratedMethod.length;
  var cache = new _MultiWeakMap2.default(arity);

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

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

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

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

  return descriptor;
}; /* eslint "no-param-reassign": "warn" */