/**
 * 获取当前节点
 *
 * @param list 集合
 * @param id 节点编号
 */
export const getNode = function (list, id) {
  for (const i in list) {
    const item = list[i]
    if (item.id === id) {
      return item
    } else {
      if (item.children) {
        const value = getNode(item.children, id)
        if (value) {
          return value
        }
      }
    }
  }
}

/**
 * 获取所有父节点 ( 包含当前节点 )
 *
 * @param list 集合
 * @param id 节点编号
 */
export const getParents = function (list, id) {
  for (const i in list) {
    if (list[i].id === id) {
      return [list[i]]
    }
    if (list[i].children) {
      const node = getParents(list[i].children, id)
      if (node !== undefined) {
        return node.concat(list[i])
      }
    }
  }
}

export const renameKeys = function (node, newKeys) {
  if (typeof node !== 'object' || node === null) {
    return node
  }

  const keys = Object.keys(node)
  const newNode = Array.isArray(node) ? [] : {}

  keys.forEach(key => {
    const newKey = newKeys[key] || key
    newNode[newKey] = renameKeys(node[key], newKeys)
  })

  return newNode
}
