/**
* @desc 克隆
* @param obj 克隆源
*/
export const deepClone = (obj) => {
    let newObj = Array.isArray(obj) ? [] : {};
    if (obj && typeof obj === "object") {
        for (let key in obj) {
            if (obj.hasOwnProperty(key)) {
                newObj[key] =
                    obj && typeof obj[key] === "object"
                        ? deepClone(obj[key])
                        : obj[key];
            }
        }
    }
    return newObj;
}



/**
* @desc 建立树型
* @param data 数据源
* @param parentId 根节点
* @param id id 匹配项
* @param pid parentId匹配项
*/
export const toTreeData = (data, parentId, id, pid) => {
    // 建立个树形结构,需要定义个最顶层的父节点，pId是-1
    let parent = [];
    for (let i = 0; i < data.length; i++) {
        if (data[i][pid] != parentId) {
        } else {
            let obj = {};
            for (let key in data[i]) {
                obj[key] = data[i][key]
            }
            obj['children'] = [];
            parent.push(obj);
        }

    }
    fnChildren(parent);
    function fnChildren(parent) {
        if (data.length !== 0) {
            for (let i = 0; i < parent.length; i++) {
                for (let j = 0; j < data.length; j++) {
                    if (parent[i].id == data[j][pid]) {
                        let obj = {};
                        for (let k in data[j]) {
                            obj[k] = data[j][k]
                        }
                        obj["children"] = [];
                        parent[i].children.push(obj);
                    }
                }
                fnChildren(parent[i].children);
            }
        }
    }
    return deleteEmptyChildren(parent);

}

export const deleteEmptyChildren = (arr) => {
    if (arr.length) {
        for (let i in arr) {
            if (arr[i].children.length) {
                deleteEmptyChildren(arr[i].children)
            } else {
                delete arr[i].children;
            }
        }
    }
    return arr
}

export const parentTreeNode = (treeList, parentKEY, id) => {
    let temp = []
    let searchFn = function (treeList, id) {
        for (let i = 0; i < treeList.length; i++) {
            let item = treeList[i]
            if (item.id === id) {
                temp.push(item)
                searchFn(treeList, item[parentKEY])
                break
            } else {
                if (item.children) {
                    searchFn(item.children, id)
                }
            }
        }
    }
    searchFn(treeList, id)
    return temp
}


export const childrenTreeNode = (treeList, parentKEY, id) => {
    let temp = [{ id: id }];
    let searchFn = function (treeList, id) {
        for (let i = 0; i < treeList.length; i++) {
            let item = treeList[i];
            if (item[parentKEY] === id) {
                temp.push(item);
                searchFn(treeList, item.id);
                // break;
            } else {
                if (item.children) {
                    searchFn(item.children, id);
                }
            }
            // console.log("run");
        }
    };
    searchFn(treeList, id);
    return temp;
}


export const getItemData = (list, key, value) => {
    for (let i = 0; i < list.length; i++) {
        let item = list[i];
        if (item[key] === value) {
            return item;
        } else {
            if (item.children && item.children.length > 0) {
                let res = getItemData(item.children, key, value);
                if (res) {
                    return res;
                }
            }
        }
    }
}



/**
* @desc 叶子节点查询根节点
* @param tree 数据源
* @param parentId 根节点
* @param config id children 匹配项
*/

export const getTreeIds = (tree, nodeId, config) => {
    const { children = 'children', id = 'id' } = config || {}
    const toFlatArray = (tree, parentId) => {
        return tree.reduce((t, _) => {
            const child = _[children]
            return [
                ...t,
                parentId ? { ..._, parentId } : _,
                ...(child && child.length ? toFlatArray(child, _[id]) : [])]
        }, [])
    }

    const getIds = flatArray => {
        let ids = [nodeId]
        let child = flatArray.find(_ => _[id] === nodeId)
        while (child && child.parentId) {
            ids = [child.parentId, ...ids]
            child = flatArray.find(_ => _[id] === child.parentId)
        }
        return ids
    }


    return getIds(toFlatArray(tree))



}


/**
 * 根据树子节点ID查找所有父节点ID
 * @param {array} dataSource 树形结构数据源
 * @param {number} nodeId 子节点ID
 * @returns {array} 包含所有父节点ID的数组，按照从根节点到直接父节点的顺序排序
 */
export const findParentIds = (dataSource, nodeId) => {
    const parentIds = []; // 用于存储所有父节点ID的数组
    // 定义一个递归函数，用于遍历整棵树并查找子节点的所有父节点
    function traverse(node, nodeId) {
      if (node.id === nodeId) { // 如果当前节点的ID等于子节点的ID，则表示已经找到了子节点，可以开始向上查找父节点
        return true; // 返回true表示已经找到了子节点
      }
      if (node.children) { // 如果当前节点有子节点，则继续遍历子节点
        for (const childNode of node.children) {
          if (traverse(childNode, nodeId)) { // 如果在子节点中找到了子节点的父节点，则将当前节点的ID添加到父节点ID数组中，并返回true表示已经找到了子节点
            parentIds.push(node.id);
            return true;
          }
        }
      }
      return false; // 如果当前节点不是子节点的父节点，则返回false
    }
    // 从根节点开始遍历整棵树，并调用递归函数查找子节点的所有父节点
    for (const node of dataSource) {
      if (traverse(node, nodeId)) { // 如果在当前节点的子树中找到了子节点的父节点，则直接退出循环
        break;
      }else{
          if(node.children && node.children.length>0){
            traverse(node.children, nodeId)
          }
      }
    }
    return parentIds; // 返回所有父节点ID的数组
}
  
// 格式化
export const booleanToNum = (val) => {
    if(val){
        return 1
    }else{
        return 0
    }
}