import deepcopy from "lodash/cloneDeep";
import isFunction from "lodash/isFunction";
export default function delegateFunctions(list, key, context, newArgPosition) {
  if (list && list.length) {
    var result = deepcopy(list);
    result.forEach(function (item) {
      var func = item && item[key];
      if (func && isFunction(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;
  }
}