import is from 'is';
export default ( datas , columns ,vm )=>{

    //异步翻译，未翻译相
    let asyncLeft = new Map();
    let subTotal = {
        _subTotal_row:true
    };
    for ( let dataIndex in Object.keys(datas) ){
        
        for ( let [index,column] of columns.entries() ){
            //异步翻译
            if ( column.asyncTranslate){
                //如果字典里没有该列，加入该列的字典
                if ( !vm.gridDicts[column.colId]){
                    vm.$set(vm.gridDicts,column.colId,{});
                }
                //如果未翻译列表里没有该列，加入该列
                if ( !asyncLeft.get(column.colId) ){
                    asyncLeft.set(column.colId,{
                        key:column.colId,
                        url:column.asyncTranslate,
                        set: new Set()
                    });
                }
                //如果当前值未被翻译，加入未翻译列表
                if (!vm.gridDicts[column.colId][datas[dataIndex][column.field]] ){
                    asyncLeft.get(column.colId).set.add(datas[dataIndex][column.field]);
                }
            }

            //小计
            if ( column.subTotal  ){
                if ( !subTotal[column.field] ){
                    subTotal[column.field] = 0;
                }
                
                subTotal[column.field] += datas[dataIndex][column.field];
            }
        }

       
    }

    //查异步询
    for ( let [key,value] of asyncLeft ){
        if ( value.set.size > 0 ){
            vm.$http.post(value.url,Array.from(value.set))
                .then(data=>{
                    for (let key of Object.keys(data)) {
                        vm.$set(vm.gridDicts[value.key], key, data[key])
                    }
                  
                })
                .catch(error=>console.error(error))
        }
    }

    return {subTotal};
}