import assignMergeValue from "./_assignMergeValue.mjs";
import cloneBuffer from "./_cloneBuffer.mjs";
import cloneTypedArray from "./_cloneTypedArray.mjs";
import copyArray from "./_copyArray.mjs";
import initCloneObject from "./_initCloneObject.mjs";
import isArguments from "./isArguments.mjs";
import isArray from "./isArray.mjs";
import isArrayLikeObject from "./isArrayLikeObject.mjs";
import isBuffer from "./isBuffer.mjs";
import isFunction from "./isFunction.mjs";
import isObject from "./isObject.mjs";
import isPlainObject from "./isPlainObject.mjs";
import isTypedArray from "./isTypedArray.mjs";
import safeGet from "./_safeGet.mjs";
import toPlainObject from "./toPlainObject.mjs";
function baseMergeDeep(object, source, key, srcIndex, mergeFunc, customizer, stack) {
  var objValue = safeGet(object, key), srcValue = safeGet(source, key), stacked = stack.get(srcValue);
  if (stacked) {
    assignMergeValue(object, key, stacked);
    return;
  }
  var newValue = customizer ? customizer(objValue, srcValue, key + "", object, source, stack) : void 0;
  var isCommon = newValue === void 0;
  if (isCommon) {
    var isArr = isArray(srcValue), isBuff = !isArr && isBuffer(srcValue), isTyped = !isArr && !isBuff && isTypedArray(srcValue);
    newValue = srcValue;
    if (isArr || isBuff || isTyped) {
      if (isArray(objValue)) {
        newValue = objValue;
      } else if (isArrayLikeObject(objValue)) {
        newValue = copyArray(objValue);
      } else if (isBuff) {
        isCommon = false;
        newValue = cloneBuffer(srcValue, true);
      } else if (isTyped) {
        isCommon = false;
        newValue = cloneTypedArray(srcValue, true);
      } else {
        newValue = [];
      }
    } else if (isPlainObject(srcValue) || isArguments(srcValue)) {
      newValue = objValue;
      if (isArguments(objValue)) {
        newValue = toPlainObject(objValue);
      } else if (!isObject(objValue) || isFunction(objValue)) {
        newValue = initCloneObject(srcValue);
      }
    } else {
      isCommon = false;
    }
  }
  if (isCommon) {
    stack.set(srcValue, newValue);
    mergeFunc(newValue, srcValue, srcIndex, customizer, stack);
    stack["delete"](srcValue);
  }
  assignMergeValue(object, key, newValue);
}
export {
  baseMergeDeep as default
};
