/**
 * 解析树数据
 * @param {[object]} param [解析对象]
 * @param {[function]} callBack [回调函数 返回一个对象，对象内包含额外属性]
 * @return {[array]} tree [树数据]
 */
export function parseTreeStructure(param, callBack, type) {
    let newParam = param;
    if (typeof param === 'string') {
        newParam = JSON.parse(newParam);
    }
    if (
        !(Object.prototype.toString.call(newParam) === '[object Object]') ||
        !('items' in newParam) ||
        !Array.isArray(newParam.items)
    ) {
        return [];
    }
    let tree = newParam.items;

    tree = (function treeProcessor(data) {
        return data.map(item => {
            let nodeItem = {
                key: type === 'role' ? item.attributes.code : item.id,
                id: type === 'staff' ? item.attributes.staffId : item.attributes.id || item.id,
                name: item.nodeType == 'user' ? item.attributes.staffName : item.name,
                nodeType: item.nodeType,
            };

            if (typeof callBack === 'function') {
                nodeItem = { ...nodeItem, ...callBack(item) };
            }
            if (item.children.length !== 0) {
                nodeItem = {
                    ...nodeItem,
                    children: treeProcessor(item.children),
                };
            }
            return nodeItem;
        });
    })(tree);

    return tree;
}
