"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.isMenu = exports.getMenuPath = exports.getMaxDepth = exports.getItemPath = exports.extractItemNodes = void 0;
exports.pick = pick;
var _react = _interopRequireDefault(require("react"));
var readItemNodesRecursively = (parentItemNode, children) => {
  if (!children) {
    return;
  }
  _react.default.Children.toArray(children).forEach(child => {
    var _child$props, _child$props2;
    if (! /*#__PURE__*/_react.default.isValidElement(child)) {
      return;
    }
    var itemKey = child == null || (_child$props = child.props) == null ? void 0 : _child$props.itemKey;
    var itemChildren = child == null || (_child$props2 = child.props) == null ? void 0 : _child$props2.children;
    if (typeof itemKey === 'string' && itemKey !== parentItemNode.itemKey) {
      var _parentItemNode$items;
      var itemNode = {
        itemKey
      };
      ((_parentItemNode$items = parentItemNode.items) !== null && _parentItemNode$items !== void 0 ? _parentItemNode$items : parentItemNode.items = []).push(itemNode);
      readItemNodesRecursively(itemNode, itemChildren);
    } else {
      readItemNodesRecursively(parentItemNode, itemChildren);
    }
  });
};
var readParentKeysRecursively = (keyToParentKey, itemNodes, parentItemKey) => {
  for (var {
    itemKey,
    items
  } of itemNodes) {
    keyToParentKey[itemKey] = parentItemKey;
    if (items) {
      readParentKeysRecursively(keyToParentKey, items, itemKey);
    }
  }
};

/**
 * Recursively walks react children to create a tree of sidebar item nodes and
 * their parent-child relationships.
 */
var extractItemNodes = children => {
  var _rootNode$items;
  var rootNode = {
    itemKey: 'rootMenu'
  }; // TODO: use a symbol
  readItemNodesRecursively(rootNode, children);
  var items = (_rootNode$items = rootNode.items) !== null && _rootNode$items !== void 0 ? _rootNode$items : [];
  var keyToParentKey = Object.create(null);
  readParentKeysRecursively(keyToParentKey, items);
  return {
    items,
    keyToParentKey
  };
};
exports.extractItemNodes = extractItemNodes;
var getItemPath = (keyToParentKey, itemKey) => {
  var path = [];
  while (itemKey !== undefined) {
    path.unshift(itemKey);
    itemKey = keyToParentKey[itemKey];
  }
  return path;
};
exports.getItemPath = getItemPath;
var isMenu = (keyToParentKey, itemKey) => Object.values(keyToParentKey).includes(itemKey);
exports.isMenu = isMenu;
var getMenuPath = (keyToParentKey, itemKey) => {
  var path = getItemPath(keyToParentKey, itemKey);
  if (path.length > 0 && !isMenu(keyToParentKey, path[path.length - 1])) {
    path.pop();
  }
  return path;
};
exports.getMenuPath = getMenuPath;
var getMaxDepth = items => {
  var maxDepth = 0;
  if (items != null && items.length) {
    for (var item of items) {
      maxDepth = Math.max(maxDepth, getMaxDepth(item.items) + 1);
    }
  }
  return maxDepth;
};

/**
 * Returns a new object with only the specified keys from the original one.
 */
exports.getMaxDepth = getMaxDepth;
function pick(object, keys) {
  // Probably should use `lodash.pick` but it's too smart with its key handing (
  // lodash keys can be paths like `a.b.c`). Since we're using this to pick
  // keys from a record which gets created from consumer input, we can't be sure
  // that consumers won't use keys that look like paths.
  var result = {};
  for (var key of keys) {
    if (Object.prototype.hasOwnProperty.call(object, key)) {
      result[key] = object[key];
    }
  }
  return result;
}
//# sourceMappingURL=utils.js.map