/**
 * 数组转Tree
 * @param netFlag 标识位（functionNet、faultNet）
 * @param parentNode 父级节点
 * @param targetNode 目标节点
 * @param subNode 下级节点
 * @param checkedNode 选中节点
 * @returns {*[]}
 */
export function arrayToNetTree(netFlag, parentNode, targetNode, subNode, checkedNode) {

    /*********** 数据清洗 Start ***********/
    let parentNodeMap;
    if (netFlag === "functionNet") {
        targetNode = cleanFunctionNetData([targetNode])[0];
        parentNode = cleanFunctionNetData(parentNode);
        parentNodeMap = buildParentNodeMap(parentNode);
        subNode = cleanFunctionNetData(subNode);
    } else if (netFlag === "faultNet") {
        targetNode = cleanFaultNetData([targetNode])[0];
        parentNode = cleanFaultNetData(parentNode);
        parentNodeMap = buildParentNodeMap(parentNode);
        subNode = cleanFaultNetData(subNode);
    } else {
        targetNode = cleanNetLrData([targetNode], checkedNode)[0];
        subNode = cleanNetLrData(subNode, checkedNode);
    }
    /*********** 数据清洗 End ***********/

    findParent(targetNode, targetNode.id, parentNodeMap);
    findChildren(targetNode, subNode);
    return targetNode;
}

/**
 * 构建父集节点Map
 * @param parentNode
 * @returns {Map<any, any>}
 */
function buildParentNodeMap(parentNode) {
    let map = new Map();
    for (let i = 0, len = parentNode.length; i < len; i++) {
        let node = parentNode[i];
        if (!map.has(node.id))
            map.set(node.id, []);
        let array = map.get(node.id);
        array.push(node)
        map.set(node.id, array);
    }
    return map;
}

/**
 * 清楚功能网/失效网左右数据
 * @param nodeArray 节点数组
 * @param checkedNode 选中节点
 * @returns {*}
 */
function cleanNetLrData(nodeArray, checkedNode) {
    return nodeArray.map((item) => {
        return {
            id: item.id,
            pid: item.pid,
            checked: checkedNode.indexOf(item.id) !== -1,
            name: item.name,
            struTypeCode: item.struTypeCode,
            suffix: item.specsName || '',
            deep: item.deep,
            children: [],
            linkName: item.linkName
        };
    });
}

/**
 * 清洗功能网数据
 * @param nodeArray 节点数组
 * @param pcFlag 节点数组
 * @returns {*}
 */
function cleanFunctionNetData(nodeArray) {
    return nodeArray.map((item) => {
        return {
            id: item.functionId,
            pid: item.parentFunctionId,
            struTypeCode: item.funStruTypeCode,
            structureName: item.struName,
            functionId: item.functionId,
            functionName: item.functionName,
            parentFunctionName: item.parentFunctionName,
            parent: [],
            children: [],
        };
    });
}

/**
 * 清洗失效网数据
 * @param nodeArray 节点数组
 * @returns {*}
 */
function cleanFaultNetData(nodeArray) {
    return nodeArray.map((item) => {
        return {
            id: item.faultId,
            pid: item.parentFaultId,
            struTypeCode: item.funStruTypeCode,
            structureName: item.struName,
            functionId: item.functionId,
            functionName: item.functionName,
            requireId: item.requireId,
            requireName: item.requireName,
            faultName: item.faultName,
            parent: [],
            children: [],
        };
    });
}

/**
 * 递归取得子集
 * @param node 节点
 * @param nodes 节点数组
 * @returns {*}
 */
function findChildren(node, nodes) {
    for (let i = 0, len = nodes.length; i < len; i++) {
        if (node.id === nodes[i].pid) {
            node.children.push(nodes[i]);
            findChildren(nodes[i], nodes);
        }
    }
}

/**
 * 递归取得父集
 * @param node 节点
 * @param id 节点ID
 * @param parentNodeMap 父级Map
 * @returns {*}
 */
function findParent(node, id, parentNodeMap) {
    if (parentNodeMap && parentNodeMap.has(id)) {
        node.parent = parentNodeMap.get(id);
    }
    if (node.parent && node.parent.length > 0) {
        for (let i = 0, len = node.parent.length; i < len; i++) {
            findParent(node.parent[i], node.parent[i].pid, parentNodeMap);
        }
    }
}


/**
 * 过滤节点
 * @param sourceNodes Tree节点数组
 * @param filterNodes 过滤节点数组
 * @param k 默认ID
 */
export function filterNode(sourceNodes, filterNodes, k = 'id') {
    let resNodes = [];
    for (let i = 0, len = sourceNodes.length; i < len; i++) {
        if (filterNodes.indexOf(sourceNodes[i][k]) === -1) {
            resNodes.push(sourceNodes[i]);
        }
    }
    return resNodes;
}

/**
 * 构建ztree节点
 * @param treeData
 * @returns {*}
 */
export function buildNodes(treeData) {
    return treeData.map(v => {
        return {
            id: v.id,
            pid: v.pid,
            name: v.name,
            oldName: v.name,
            deep: v.deep,
            description: v.name,
            struTypeCode: v.struTypeCode,
            suffix: v.specsName || '',
            linkName: v.linkName || '',
        }
    });
}


