import { isArray, isPlainObject } from './inspect';
import { keys } from './object';

const cloneDeep = function (obj) {
  let defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : obj;
  if (isArray(obj)) {
    return obj.reduce((result, val) => [...result, cloneDeep(val, val)], []);
  }
  if (isPlainObject(obj)) {
    return keys(obj).reduce((result, key) => ({
      ...result,
      [key]: cloneDeep(obj[key], obj[key])
    }), {});
  }
  return defaultValue;
};

export { cloneDeep };
