import _Object$getPrototypeOf from 'babel-runtime/core-js/object/get-prototype-of';
import _Object$getOwnPropertyNames from 'babel-runtime/core-js/object/get-own-property-names';
import _ from 'lodash';

/**
 * delegate calls from a baseClass to a providerClass if the call does not exist in base class
 * @param {object} baseClass - The base object that has first priority to make calls
 * @param {object} providerClass - The class whose methods will be called by baseClass
 * @param {function} middlewareCall - <optional> Middleware function that can act as
 * intermediary between base and provider. Should take a two params which is the provider
 * classes method call and the params passed
 */
var delegateCalls = function delegateCalls(baseClass, providerClass, middlewareCall) {
  // we don't want to delegate any calls that are part of the base class
  var existingMethods = _Object$getOwnPropertyNames(baseClass).concat(_Object$getPrototypeOf(baseClass));
  // methods of provider
  var providerMethods = _Object$getOwnPropertyNames(providerClass);
  // calls we will delegate
  var delegatableMethods = providerMethods.filter(function (method) {
    if (!providerClass.hasOwnProperty(method) || typeof providerClass[method] !== 'function') {
      return false;
    }

    return !_.includes(existingMethods, method);
  });

  // delegate calls in baseClass
  delegatableMethods.forEach(function (methodName) {
    var curriedCall = void 0;
    if (middlewareCall) curriedCall = function curriedCall() {
      for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }

      return middlewareCall.apply(undefined, [providerClass[methodName]].concat(args));
    };else curriedCall = function curriedCall() {
      return providerClass[methodName].apply(providerClass, arguments);
    };
    // set method in baseClass
    baseClass[methodName] = curriedCall;
  });
};

/**
{ constant: true,
  inputs: [],
  name: 'lastPeriodChange',
  outputs: [ [Object] ],
  payable: false,
  stateMutability: 'view',
  type: 'function' },
*/

export default delegateCalls;
//# sourceMappingURL=delegateCalls.js.map