'use strict';

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

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

/**
 * Converts an object into an array of name/value pairs (two-element arrays), sorted by name.
 */
var asPairs = exports.asPairs = function asPairs(object) {
  var array = [];

  Object.keys(object).sort().forEach(function (key) {
    var value = object[key];

    array.push([key, (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object' ? asPairs(value) : value]);
  });

  return array;
};

/**
 * Flattens an object. The resultant object contains the leaf values of the source object,
 * where each value is keyed by its flattened path from the source object. Paths are flattened by
 * joining individual keys with a period.
 */
var flatten = exports.flatten = function flatten(object, maxDepth, parentPath) {
  var currentDepth = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 1;

  var flattened = {};

  Object.keys(object).forEach(function (key) {
    var path = parentPath ? parentPath + '.' + key : key;
    var value = object[key];

    if ((typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object' && (typeof maxDepth === 'undefined' || currentDepth < maxDepth)) {
      Object.assign(flattened, flatten(value, maxDepth, path, currentDepth + 1));
    } else {
      flattened[path] = value;
    }
  });

  return flattened;
};

/**
 * Compares two objects, returning the keys that have different values. The return value is an
 * object whose keys are the differing keys.
 */
var diff = exports.diff = function diff(object1, object2, maxDepth) {
  var flat1 = flatten(object1, maxDepth);
  var flat2 = flatten(object2, maxDepth);

  var diffKeys = {};

  Object.keys(flat1).forEach(function (key) {
    if (flat1[key] !== flat2[key]) {
      diffKeys[key] = true;
    }
  });

  Object.keys(flat2).forEach(function (key) {
    if (!(key in flat1)) {
      diffKeys[key] = true;
    }
  });

  return diffKeys;
};