"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");

var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");

require("core-js/modules/es.function.name");

require("core-js/modules/es.object.to-string");

require("core-js/modules/es.regexp.to-string");

_Object$defineProperty(exports, "__esModule", {
  value: true
});

exports.flatObjectEntries = flatObjectEntries;

var _getIterator2 = _interopRequireDefault(require("@babel/runtime-corejs3/core-js/get-iterator"));

var _getIteratorMethod2 = _interopRequireDefault(require("@babel/runtime-corejs3/core-js/get-iterator-method"));

var _symbol = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/symbol"));

var _from = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/from"));

var _slice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/slice"));

var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));

var _keys = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/keys"));

var _map = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/map"));

var _some = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/some"));

var _isArray = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/is-array"));

var _helpers = require("./helpers");

function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof _symbol.default === "undefined" || (0, _getIteratorMethod2.default)(o) == null) { if ((0, _isArray.default)(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = (0, _getIterator2.default)(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }

function _unsupportedIterableToArray(o, minLen) { var _context; if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = (0, _slice.default)(_context = Object.prototype.toString.call(o)).call(_context, 8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return (0, _from.default)(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function isArrayOfPrimitives(value
/*: any*/
)
/*: boolean*/
{
  return (0, _isArray.default)(value) && value.length && !(0, _some.default)(value).call(value, _helpers.isObj);
}
/*:: type Primitive = string | boolean | null | void | number*/


/**
 * Similar to Object.entries, but flattens an object and keys are arrays. Arrays
 * of primitives are *not* flattened.
 *
 * @example
 * flatObjectEntries({
 *   foo: { bar: 'qux' },
 *   hello: 'world',
 *   arr: ['biz', 'baz'],
 *   other: [{ foo: 'bar'}]
 * })
 * // [
 * //   [['foo', 'bar'], 'qux'],
 * //   [['hello'], 'world'],
 * //   [['arr'], ['biz', 'baz']],
 * //   [['other', 0, 'foo'], 'bar']
 * // ]
 */
function flatObjectEntries(object
/*: {}*/
)
/*: Array<
  [Array<string | number>, Primitive | Primitive[]]
>*/
{
  var entries = [];

  (function step(object
  /*: any*/
  ) {
    var prev
    /*: Array<string | number>*/
    = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
    var currentDepth
    /*: number*/
    = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
    var keys = (0, _isArray.default)(object) ? (0, _map.default)(object).call(object, function (_, i) {
      return i;
    }) : (0, _keys.default)(object);

    var _iterator = _createForOfIteratorHelper(keys),
        _step;

    try {
      for (_iterator.s(); !(_step = _iterator.n()).done;) {
        var key = _step.value;
        var value = object[key];
        var newKey = (0, _concat.default)(prev).call(prev, [key]);

        if ((0, _helpers.isObj)(value) && !isArrayOfPrimitives(value)) {
          step(value, newKey, currentDepth + 1);
        } else {
          entries.push([newKey, value]);
        }
      }
    } catch (err) {
      _iterator.e(err);
    } finally {
      _iterator.f();
    }
  })(object);

  return entries;
}
//# sourceMappingURL=flat_object_entries.js.map