import _typeof from "@babel/runtime/helpers/typeof";
import React from 'react';
import { getKeys } from './getKeys';
import { serializeValue } from './serializeValue';
export var getKeysAddedRemovedCommon = function getKeysAddedRemovedCommon(object1, object2) {
  var oldKeys = object1 !== null ? getKeys(object1) : [];
  var newKeys = object2 !== null ? getKeys(object2) : [];
  var removed = oldKeys.filter(function (key) {
    return !newKeys.includes(key);
  });
  var added = newKeys.filter(function (key) {
    return !oldKeys.includes(key);
  });
  var common = oldKeys.filter(function (key) {
    return newKeys.includes(key);
  });
  return {
    added: added,
    common: common,
    removed: removed
  };
};
var _getPropsDifference = function getPropsDifference(object1, object2) {
  var curDepth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  var maxDepth = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 2;
  var keysToIgnore = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : [];
  var _getKeysAddedRemovedC = getKeysAddedRemovedCommon(object1, object2),
    added = _getKeysAddedRemovedC.added,
    common = _getKeysAddedRemovedC.common,
    removed = _getKeysAddedRemovedC.removed;
  var changed = [];
  common.forEach(function (key) {
    var value1 = object1[key];
    var value2 = object2[key];
    var value1Type = _typeof(value1);
    var value2Type = _typeof(value2);

    // Do comparision only if values doesn't match (or reference to same object in memory).
    // Or if key does not exist in keys to ignore.
    if (value1 !== value2 && keysToIgnore.indexOf(key) === -1) {
      // if both key value are objects and not referencing same object in memory.
      //  then get recursive difference.
      if ( /*#__PURE__*/React.isValidElement(value1) || /*#__PURE__*/React.isValidElement(value2)) {
        changed.push({
          key: key,
          reactElementChanged: true
        });
      } else if (value1Type === 'object' && value2Type === 'object') {
        if (curDepth <= maxDepth) {
          var difference = _getPropsDifference(value1, value2, curDepth + 1, maxDepth);
          changed.push({
            key: key,
            difference: difference
          });
        } else {
          changed.push({
            key: key,
            maxDepthReached: true
          });
        }
      } else {
        changed.push({
          key: key,
          oldValue: serializeValue(value1),
          newValue: serializeValue(value2)
        });
      }
    }
  });
  return {
    added: added,
    changed: changed,
    removed: removed
  };
};
export { _getPropsDifference as getPropsDifference };
export var getShallowPropsDifference = function getShallowPropsDifference(object1, object2) {
  var _getKeysAddedRemovedC2 = getKeysAddedRemovedCommon(object1, object2),
    added = _getKeysAddedRemovedC2.added,
    common = _getKeysAddedRemovedC2.common,
    removed = _getKeysAddedRemovedC2.removed;
  var changed = common.filter(function (key) {
    return object1[key] !== object2[key];
  });
  return {
    added: added,
    changed: changed,
    removed: removed
  };
};
// eslint-disable-next-line @atlaskit/editor/no-re-export
export { getKeys } from './getKeys';
// eslint-disable-next-line @atlaskit/editor/no-re-export
export { serializeValue } from './serializeValue';