/**
* @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))



}



