/* 
    list = 查询的数组
    tier = 层级
    need = 需要查询的数据
*/
let getObjectKey = (object, value) => {
    return Object.keys(object).find(key => object[key] == value);
};

let name = ['modules', 'apps'];

function query(list, tier, need) {
    if (!list.some(v => v.systemname == tier[0])) return [];
    // if (!list) return;
    if (list.length == 0) return;
    //排除不是数组 在深入查找一级
    if (list.constructor !== Array) return query(list[Object.keys(list).filter((key) => name.includes(key))], tier, need);
    //拿到当前层的数组 如果不是最后一层 继续查找
    let arr = list.find(v => getObjectKey(v, tier[0]));
    if (tier.length > 1) return query(arr, tier.splice(1, 1), need);
    // 到达最后一层查询需要查询的权限
    if (arr.constructor !== Array) arr = arr[Object.keys(arr).filter((key) => name.includes(key))]
    let perArr = [];
    for (let i = 0; i < arr.length; i++) {
        if (need.includes(arr[i].systemmodulename)) perArr.push(arr[i])
    }
    return perArr;
};

module.exports.query = query;