import { arr2Tree } from "./array";

export function getColumnsByHead(heads = []) {
  const list = heads.map(it => ({
    key: it.headId,
    ...it,
  }));

  const [tree] = arr2Tree(list, 'parentId', 'headId');

  let num = 1;
  const deep = (arr) => {
    return arr.map((it) => {

      const dataIndex = it.children && it.children.length > 0 ? undefined : `value-${num++}`;
      const children = it.children && it.children.length > 0 ? deep(it.children) : undefined;

      return {
        title: it.title,
        key: it.headId,
        dataIndex,
        children,
        hideInSearch:true,
      }
    });
  }

  return deep(tree);
}
