import { SystemMenue } from "../entity/SystemMenue";
interface routerTree extends SystemMenue {
  children?: SystemMenue[];
}
let filterKey = (obj: SystemMenue) => {
  delete obj.id;
  delete obj.updateDate;
  delete obj.createDate;
  delete obj.parentId;
  return obj;
};
export default function menueToTree(data: SystemMenue[]): routerTree[] {
  let dataList = [];
  if (!data.some((item) => item.parentId === 0)) {
    return data.map((item) => filterKey(item));
  }
  for (let i = 0; i < data.length; i++) {
    let item: routerTree = data[i];

    item.children = [];

    for (let j = 0; j < data.length; j++) {
      let childrenItem = data[j];
      if (childrenItem.parentId === item.id) {
        item.children.push(filterKey({ ...childrenItem }));
      }
    }
    // 只添加父级
    if (!item.parentId) dataList.push(filterKey({ ...item }));
  }
  return dataList;
}
