"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = createSelector;
var _memoize = _interopRequireDefault(require("./memoize"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
/**
 * Shorter and more simple implementation of reselect's createSelector
 * Takes one or more selectors, or an array of selectors, computes their values
 * and passes them as arguments to resultFunc.
 * @see https://github.com/reduxjs/reselect#createselectorinputselectors--inputselectors-resultfunc
 * @param {function} fns
 * @returns {function(...[*]=)}
 * @throws TypeError If any of the arguments is not a function
 */
function createSelector() {
  for (var _len = arguments.length, fns = new Array(_len), _key = 0; _key < _len; _key++) {
    fns[_key] = arguments[_key];
  }
  // skip calling the result function when the params are unchanged (saving expensive calculation)
  var resultFunc = fns.pop();
  if (typeof resultFunc !== 'function') {
    throw new TypeError('createSelector accepts only function as argument!');
  }
  var memResultFunc = (0, _memoize["default"])(resultFunc);
  // check dependency functions
  var length = fns.length;
  for (var i = 0; i < length; i++) {
    var fn = fns[i];
    if (typeof fn !== 'function') {
      throw new TypeError('All of the dependencies have to be a function!');
    }
  }

  // memoize will skip the recalculation if the selector function is called with the same arguments
  // (like an unchanged state object)
  return (0, _memoize["default"])(function () {
    // get the result of the dependency functions
    var params = [];
    for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
      args[_key2] = arguments[_key2];
    }
    for (var _i = 0; _i < length; _i++) {
      params.push(fns[_i].apply(null, args));
    }
    // call the result function with the params
    return memResultFunc.apply(null, params);
  });
}