/**
 * pid形式数据转children形式
 * @param data 需要转换的数组
 * @param idKey id字段名
 * @param pidKey pid字段名
 * @param childKey 生成的children字段名
 * @param pid 顶级的pid
 * @param addPIds 是否添加所有父级id的字段
 * @param parentsKey 所有父级id的字段名称，默认parentIds
 * @param parentIds 所有父级id
 * @returns {[]}
 */
export function toTreeData(data, idKey, pidKey, childKey, pid, addPIds, parentsKey, parentIds) {
  if (typeof data === 'object' && !Array.isArray(data)) {
    idKey = data.idKey
    pidKey = data.pidKey
    childKey = data.childKey
    pid = data.pid
    addPIds = data.addPIds
    parentsKey = data.parentsKey
    parentIds = data.parentIds
    data = data.data
  }
  if (!childKey) {
    childKey = 'children'
  }
  if (typeof pid === 'undefined') {
    pid = []
    data.forEach((d) => {
      let flag = true
      for (let i = 0; i < data.length; i++) {
        if (d[pidKey] === data[i][idKey]) {
          flag = false
          break
        }
      }
      if (flag) {
        pid.push(d[pidKey])
      }
    })
  }
  const result = []
  data.forEach((d) => {
    if (d[idKey] === d[pidKey]) {
      console.error('data error: ', d)
      return
    }
    if (Array.isArray(pid) ? (pid.indexOf(d[pidKey]) !== -1) : (d[pidKey] === pid)) {
      const children = toTreeData({
        data: data,
        idKey: idKey,
        pidKey: pidKey,
        childKey: childKey,
        pid: d[idKey],
        addPIds: addPIds,
        parentsKey: parentsKey,
        parentIds: (parentIds || []).concat([d[idKey]])
      })
      if (children.length > 0) {
        d[childKey] = children
      }
      if (addPIds) {
        d[parentsKey || 'parentIds'] = parentIds || []
      }
      result.push(d)
    }
  })
  return result
}

/**
 * 遍历children形式数据
 * @param data 需要遍历的数组
 * @param callback 回调
 * @param childKey children字段名
 */
export function eachTreeData(data, callback, childKey = 'children') {
  if (!data || !data.length) {
    return
  }
  data.forEach((d) => {
    if (callback(d) !== false && d[childKey]) {
      eachTreeData(d[childKey], callback, childKey)
    }
  })
}

/**
 * 处理树形数据
 * @param data 需要处理的数据
 * @param formatter 处理器
 * @param childKey children字段名
 * @returns {[]} 处理后的数据
 */
export function formatTreeData(data, formatter, childKey = 'children') {
  const result = []
  if (data && data.length) {
    data.forEach((d) => {
      const item = formatter(d)
      if (item !== false) {
        if (item[childKey]) {
          item[childKey] = formatTreeData(item[childKey], formatter, childKey)
        }
        result.push(item)
      }
    })
  }
  return result
}

/**
 * 处理select数据
 * @param data 需要处理的数据
 * @params {label: XX, value: XX, children: XX} 需要处理的结构
 * @returns {[]} 处理后的数据
 */
export function dataToSelect(data, optionsObj) {
  const result = []
  if (data && data.length) {
    data.forEach((item) => {
      const i = {
        ...item
      }
      i.label = item[optionsObj.label]
      i.value = item[optionsObj.value]
      if (item.children && item.children.length) {
        dataToSelect(item.children, optionsObj)
        i.children = item[optionsObj.children]
      }
      result.push(i)
    })
  }
  return result
}
