/*
 * @FilePath     : /src/utils/tree.js
 * @Description  :
 */

// 树形组件数据转换
export const listTotree = (data, A, B) => {
  let idFiled = A
  let parentField = B
  let i,
    l,
    treeData = [],
    tmpMap = []
  for (i = 0, l = data.length; i < l; i++) {
    tmpMap[data[i][idFiled]] = data[i]
  }
  for (i = 0, l = data.length; i < l; i++) {
    if (tmpMap[data[i][parentField]] && data[i][idFiled] != data[i][parentField]) {
      if (!tmpMap[data[i][parentField]]['children']) tmpMap[data[i][parentField]]['children'] = []

      tmpMap[data[i][parentField]]['children'].push(data[i])
    } else {
      // if (!(data[i].children && data[i]?.children?.length)) data[i].isSmall = true
      treeData.push(data[i])
    }
  }
  // console.log(tmpMap, 'tmpMap')
  // console.log(data, 'data')
  return treeData
}
