'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _Stack = require('./_Stack.js');
var _arrayEach = require('./_arrayEach.js');
var _assignValue = require('./_assignValue.js');
var _baseAssign = require('./_baseAssign.js');
var _baseAssignIn = require('./_baseAssignIn.js');
var _cloneBuffer = require('./_cloneBuffer.js');
var _copyArray = require('./_copyArray.js');
var _copySymbols = require('./_copySymbols.js');
var _copySymbolsIn = require('./_copySymbolsIn.js');
var _getAllKeys = require('./_getAllKeys.js');
var _getAllKeysIn = require('./_getAllKeysIn.js');
var _getTag = require('./_getTag.js');
var _initCloneArray = require('./_initCloneArray.js');
var _initCloneByTag = require('./_initCloneByTag.js');
var _initCloneObject = require('./_initCloneObject.js');
var isArray = require('./isArray.js');
var isBuffer = require('./isBuffer.js');
var isMap = require('./isMap.js');
var isObject = require('./isObject.js');
var isSet = require('./isSet.js');
var keys = require('./keys.js');
var keysIn = require('./keysIn.js');

/** Used to compose bitmasks for cloning. */
var CLONE_DEEP_FLAG = 1,
    CLONE_FLAT_FLAG = 2,
    CLONE_SYMBOLS_FLAG = 4;

/** `Object#toString` result references. */
var argsTag = '[object Arguments]',
    arrayTag = '[object Array]',
    boolTag = '[object Boolean]',
    dateTag = '[object Date]',
    errorTag = '[object Error]',
    funcTag = '[object Function]',
    genTag = '[object GeneratorFunction]',
    mapTag = '[object Map]',
    numberTag = '[object Number]',
    objectTag = '[object Object]',
    regexpTag = '[object RegExp]',
    setTag = '[object Set]',
    stringTag = '[object String]',
    symbolTag = '[object Symbol]',
    weakMapTag = '[object WeakMap]';

var arrayBufferTag = '[object ArrayBuffer]',
    dataViewTag = '[object DataView]',
    float32Tag = '[object Float32Array]',
    float64Tag = '[object Float64Array]',
    int8Tag = '[object Int8Array]',
    int16Tag = '[object Int16Array]',
    int32Tag = '[object Int32Array]',
    uint8Tag = '[object Uint8Array]',
    uint8ClampedTag = '[object Uint8ClampedArray]',
    uint16Tag = '[object Uint16Array]',
    uint32Tag = '[object Uint32Array]';

/** Used to identify `toStringTag` values supported by `_.clone`. */
var cloneableTags = {};
cloneableTags[argsTag] = cloneableTags[arrayTag] =
cloneableTags[arrayBufferTag] = cloneableTags[dataViewTag] =
cloneableTags[boolTag] = cloneableTags[dateTag] =
cloneableTags[float32Tag] = cloneableTags[float64Tag] =
cloneableTags[int8Tag] = cloneableTags[int16Tag] =
cloneableTags[int32Tag] = cloneableTags[mapTag] =
cloneableTags[numberTag] = cloneableTags[objectTag] =
cloneableTags[regexpTag] = cloneableTags[setTag] =
cloneableTags[stringTag] = cloneableTags[symbolTag] =
cloneableTags[uint8Tag] = cloneableTags[uint8ClampedTag] =
cloneableTags[uint16Tag] = cloneableTags[uint32Tag] = true;
cloneableTags[errorTag] = cloneableTags[funcTag] =
cloneableTags[weakMapTag] = false;

/**
 * The base implementation of `_.clone` and `_.cloneDeep` which tracks
 * traversed objects.
 *
 * @private
 * @param {*} value The value to clone.
 * @param {boolean} bitmask The bitmask flags.
 *  1 - Deep clone
 *  2 - Flatten inherited properties
 *  4 - Clone symbols
 * @param {Function} [customizer] The function to customize cloning.
 * @param {string} [key] The key of `value`.
 * @param {Object} [object] The parent object of `value`.
 * @param {Object} [stack] Tracks traversed objects and their clone counterparts.
 * @returns {*} Returns the cloned value.
 */
function baseClone(value, bitmask, customizer, key, object, stack) {
  var result,
      isDeep = bitmask & CLONE_DEEP_FLAG,
      isFlat = bitmask & CLONE_FLAT_FLAG,
      isFull = bitmask & CLONE_SYMBOLS_FLAG;
  if (result !== undefined) {
    return result;
  }
  if (!isObject.default(value)) {
    return value;
  }
  var isArr = isArray.default(value);
  if (isArr) {
    result = _initCloneArray.default(value);
    if (!isDeep) {
      return _copyArray.default(value, result);
    }
  } else {
    var tag = _getTag.default(value),
        isFunc = tag == funcTag || tag == genTag;

    if (isBuffer.default(value)) {
      return _cloneBuffer.default(value, isDeep);
    }
    if (tag == objectTag || tag == argsTag || (isFunc && !object)) {
      result = (isFlat || isFunc) ? {} : _initCloneObject.default(value);
      if (!isDeep) {
        return isFlat
          ? _copySymbolsIn.default(value, _baseAssignIn.default(result, value))
          : _copySymbols.default(value, _baseAssign.default(result, value));
      }
    } else {
      if (!cloneableTags[tag]) {
        return object ? value : {};
      }
      result = _initCloneByTag.default(value, tag, isDeep);
    }
  }
  // Check for circular references and return its corresponding clone.
  stack || (stack = new _Stack.default);
  var stacked = stack.get(value);
  if (stacked) {
    return stacked;
  }
  stack.set(value, result);

  if (isSet.default(value)) {
    value.forEach(function(subValue) {
      result.add(baseClone(subValue, bitmask, customizer, subValue, value, stack));
    });
  } else if (isMap.default(value)) {
    value.forEach(function(subValue, key) {
      result.set(key, baseClone(subValue, bitmask, customizer, key, value, stack));
    });
  }

  var keysFunc = isFull
    ? (isFlat ? _getAllKeysIn.default : _getAllKeys.default)
    : (isFlat ? keysIn.default : keys.default);

  var props = isArr ? undefined : keysFunc(value);
  _arrayEach.default(props || value, function(subValue, key) {
    if (props) {
      key = subValue;
      subValue = value[key];
    }
    // Recursively populate clone (susceptible to call stack limits).
    _assignValue.default(result, key, baseClone(subValue, bitmask, customizer, key, value, stack));
  });
  return result;
}

exports.default = baseClone;
