"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "getKeys", {
  enumerable: true,
  get: function get() {
    return _getKeys.getKeys;
  }
});
exports.getShallowPropsDifference = exports.getPropsDifference = exports.getKeysAddedRemovedCommon = void 0;
Object.defineProperty(exports, "serializeValue", {
  enumerable: true,
  get: function get() {
    return _serializeValue.serializeValue;
  }
});
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _react = _interopRequireDefault(require("react"));
var _getKeys = require("./getKeys");
var _serializeValue = require("./serializeValue");
var getKeysAddedRemovedCommon = exports.getKeysAddedRemovedCommon = function getKeysAddedRemovedCommon(object1, object2) {
  var oldKeys = object1 !== null ? (0, _getKeys.getKeys)(object1) : [];
  var newKeys = object2 !== null ? (0, _getKeys.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
  };
};

// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
var _getPropsDifference = exports.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 = (0, _typeof2.default)(value1);
    var value2Type = (0, _typeof2.default)(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.default.isValidElement(value1) || /*#__PURE__*/_react.default.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: (0, _serializeValue.serializeValue)(value1),
          newValue: (0, _serializeValue.serializeValue)(value2)
        });
      }
    }
  });
  return {
    added: added,
    changed: changed,
    removed: removed
  };
};

// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
var getShallowPropsDifference = exports.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

// eslint-disable-next-line @atlaskit/editor/no-re-export