import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import classNames from 'classnames';
import isPromise from 'is-promise';
import isNil from 'lodash/isNil';
import isFunction from 'lodash/isFunction';
import assignWith from 'lodash/assignWith';
import { global } from 'choerodon-ui/shared';

if (!global.FUNCTION_CHAINS_MAP) {
  global.FUNCTION_CHAINS_MAP = new WeakMap();
}

var chainsMap = global.FUNCTION_CHAINS_MAP;

function merge(prop, other) {
  if (isNil(prop)) {
    return other;
  }

  if (isNil(other)) {
    return prop;
  }

  return _objectSpread(_objectSpread({}, prop), other);
}

function customizer(value, srcValue, key) {
  if (key === 'style' || key.endsWith('Style')) {
    return merge(value, srcValue);
  }

  if (key === 'className' || key.endsWith('ClassName')) {
    return classNames(value, srcValue);
  }

  if (key.endsWith('Props')) {
    return mergeProps(value, srcValue);
  }

  if (isFunction(value) && isFunction(srcValue)) {
    var chains = chainsMap.get(value);

    if (chains) {
      chains.fn = srcValue;
      return chains;
    }

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

      var $chains = chainsMap.get(value);

      var _ref = $chains || {},
          fn = _ref.fn;

      if (fn) {
        var ret = fn.apply(void 0, args);

        if (isPromise(ret)) {
          return ret.then(function (result) {
            if (result !== false) {
              return value.apply(void 0, args);
            }

            return result;
          });
        }

        if (ret === false) {
          return false;
        }
      }

      return value.apply(void 0, args);
    };

    newChains.fn = srcValue;
    chainsMap.set(value, newChains);
    return newChains;
  }
}

export default function mergeProps(props, otherProps) {
  if (isNil(props)) {
    return otherProps;
  }

  if (isNil(otherProps)) {
    return props;
  }

  return assignWith(props, otherProps, customizer);
}
//# sourceMappingURL=mergeProps.js.map
