const createNode = ({
  id,
  parent = null,
  children = [],
  collapsed = true,
  visible = false,
  displayName = "",
  mapLayer = null,
}) => {
  const node = {
    id,
    parent,
    children,
    collapsed,
    visible,
    displayName,
    mapLayer,
  };
  return node;
};

const isParentOf = (queryNode, destNode) => {
  let ancestors = [];
  function familyTree(node) {
    if (node.parent) {
      ancestors.push(node.parent);
      familyTree(node.parent);
    }
  }
  familyTree(destNode);
  return ancestors.indexOf(queryNode) !== -1;
};

const removeFromParent = (node) => {
  const parent = node.parent;
  const idx = parent.children.indexOf(node);
  parent.children.splice(idx, 1);
};

const insertNodeAt = (node, parent, idx) => {
  node.parent = parent;
  parent.children.splice(idx, 0, node);
};

export { createNode, insertNodeAt, isParentOf, removeFromParent };
