import type { TreeOption } from "naive-ui";
export const findAllChildKeys = (node: TreeOption): (string | number)[] => {
  let result: (string | number)[] = [];
  if (node.children?.length) {
    for (const child of node.children) {
      result.push(child.key as string | number);
      result = result.concat(findAllChildKeys(child));
    }
  }
  return result;
};

export const findAllParentKeys = (
  targetKey: string | number,
  tree: TreeOption[],
  parents: (string | number)[] = []
): (string | number)[] => {
  const search = (nodes: TreeOption[]): boolean => {
    for (const node of nodes) {
      if (node.children) {
        const match = node.children.find((child) => child.key === targetKey);
        if (match) {
          parents.push(node.key as string | number);
          targetKey = node.key as string | number;
          return search(tree); // 继续往上查找
        } else if (search(node.children)) {
          return true;
        }
      }
    }
    return false;
  };

  search(tree);
  return parents;
};
