var ROOT_TREE_NODE = 0;
export var addItemToDict = function addItemToDict(item, dict) {
  var _Object$assign;

  var id = item.id;

  if (dict[id]) {
    throw new Error("Item with id " + id + " already in dict");
  }

  return Object.assign({}, dict, (_Object$assign = {}, _Object$assign[id] = item, _Object$assign));
};
export var addItemToTree = function addItemToTree(item, tree, dict, root) {
  var _Object$assign2;

  if (root === void 0) {
    root = ROOT_TREE_NODE;
  }

  var id = item.id,
      _item$parentId = item.parentId,
      parentId = _item$parentId === void 0 ? root : _item$parentId;

  if (!id || !parentId && parentId !== root) {
    throw new Error("Got invalid item data: " + item);
  }

  var parent = dict[parentId];

  if (parent && parent.parentId === id) {
    throw new Error("Cyclic structure detected: item " + id + " references " + parent.id);
  }

  var treeNode = tree[parentId] || [];
  return Object.assign({}, tree, (_Object$assign2 = {}, _Object$assign2[parentId] = [].concat(treeNode, [item]), _Object$assign2));
};
export var getItemByIdFromDict = function getItemByIdFromDict(id, dict) {
  return dict[id];
};
export var getItemSiblingsFromTree = function getItemSiblingsFromTree(item, tree, root) {
  if (root === void 0) {
    root = ROOT_TREE_NODE;
  }

  if (!item) {
    return;
  }

  var parentId = item.parentId || root;
  return tree[parentId];
};
export var getItemsChildrenFromTree = function getItemsChildrenFromTree(item, tree) {
  if (!item) {
    return;
  }

  var id = item.id;
  return tree[id];
};
export var getRootNodeFromTree = function getRootNodeFromTree(tree, root) {
  if (root === void 0) {
    root = ROOT_TREE_NODE;
  }

  return tree[root];
};
export var resolveItemPath = function resolveItemPath(item, dict, path) {
  if (path === void 0) {
    path = [];
  }

  if (!item) {
    return;
  }

  var id = item.id,
      parentId = item.parentId;

  if (!id || !(id in dict)) {
    return;
  }

  var parent = dict[parentId];
  var nextPath = [id].concat(path);

  if (!parent) {
    return nextPath;
  }

  return resolveItemPath(parent, dict, nextPath);
};
export var resolveItemsMaxBranchLengthInTree = function resolveItemsMaxBranchLengthInTree(item, tree, resolvedLength) {
  if (resolvedLength === void 0) {
    resolvedLength = 0;
  }

  if (!item) {
    return resolvedLength;
  }

  var children = getItemsChildrenFromTree(item, tree);

  if (children && children.length > 0) {
    var candidates = children.map(function (child) {
      return resolveItemsMaxBranchLengthInTree(child, tree, resolvedLength + 1);
    });
    return Math.max.apply(Math, candidates);
  }

  return resolvedLength;
};