"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = delegateFunctions;
var _cloneDeep = _interopRequireDefault(require("lodash/cloneDeep"));
var _isFunction = _interopRequireDefault(require("lodash/isFunction"));
function delegateFunctions(list, key, context, newArgPosition) {
  if (list && list.length) {
    var result = (0, _cloneDeep["default"])(list);
    result.forEach(function (item) {
      var func = item && item[key];
      if (func && (0, _isFunction["default"])(func)) {
        item[key] = function () {
          var ARGS = Array.prototype.slice.call(arguments);
          if (newArgPosition === undefined) {
            ARGS.push(context);
          } else {
            ARGS.splice(newArgPosition, 0, context);
          }
          return func.apply(context, ARGS);
        };
      }
    });
    return result;
  }
}