import { clone } from "lodash";

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.cellRendererParams && column.cellRendererParams.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.cellRendererParams.asyncTranslate,
						set: new Set(),
						type: 'default'
					});
				}
				//如果当前值未被翻译，加入未翻译列表
				if (datas[dataIndex][column.field] && !vm.gridDicts[column.colId][datas[dataIndex][column.field]]) {
					asyncLeft.get(column.colId).set.add(datas[dataIndex][column.field]);
				}
			}

			//使用分页接口进行翻译
			if (column.cellRendererParams && column.cellRendererParams.asyncTranslateByPager) {

				//如果字典里没有该列，加入该列的字典
				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.cellRendererParams.asyncTranslateByPager,
						set: new Set(),
						type: 'pager',
						column
					});
				}
				//如果当前值未被翻译，加入未翻译列表
				if (datas[dataIndex][column.field] && !vm.gridDicts[column.colId][datas[dataIndex][column.field]]) {
					asyncLeft.get(column.colId).set.add(datas[dataIndex][column.field]);
				}
			}

			//小计
			if (column.cellRendererParams && column.cellRendererParams.subTotal) {
				if (!subTotal[column.field]) {
					subTotal[column.field] = 0;
				}
				if (typeof column.cellRendererParams.subTotal === 'string') {
					subTotal[column.field] = column.cellRendererParams.subTotal;
				} else {
					subTotal[column.field] += datas[dataIndex][column.field];
				}
			}
		}
	}

	//查异步询
	for (let [key, value] of asyncLeft) {
		if (value.set.size > 0) {
			let queryUrlList = clone(Array.isArray(value.url) ? value.url : [value.url])
			if (value.type === 'default') {
				Promise.all(queryUrlList.map(url => {
					return vm.$http.post(url, Array.from(value.set))
				}))
					.then(data => {
						for (let item of data) {
							for (let key of Object.keys(item)) {
								vm.$set(vm.gridDicts[value.key], key, item[key]);
							}
						}
						vm.gridApi.refreshCells({ force: true, columns: columns.filter(column => column.cellRendererParams && column.cellRendererParams.asyncTranslate).map(item => item.colId) });
					})
					.catch(error => console.error(error));
			} else if (value.type === 'pager') {
				queryUrlList = queryUrlList.map(queryConfig => {
					queryConfig.dictKey = queryConfig.key ? queryConfig.key : value.column.field
					return queryConfig
				})
				Promise.all(queryUrlList.map(queryConfig => {
					return vm.$http
						.getByPager(queryConfig.url, {
							pageSize: 9999999,
							fastQueryParameters: [{
								columnId: queryConfig.dictKey,
								fastQueryType: 'in',
								dataType: 'list',
								value: Array.from(value.set)
							}]
						})
				}))
					.then((pagerList) => {
						for (let index in pagerList) {
							const item = queryUrlList[index];
							for (let row of pagerList[index].exhibitDatas) {
								if (row[item.dictKey] && row[item.name]) {
									vm.$set(vm.gridDicts[value.key], row[item.dictKey], row[item.name]);
								} else {
									console.warn(`异步翻译（byPager）${item.url}-${item.dictKey}-${item.name}不存在`)
								}

							}
						}
						vm.gridApi.refreshCells({ force: true, columns: columns.filter(column => column.cellRendererParams && column.cellRendererParams.asyncTranslateByPager).map(item => item.colId) });
					})
					.catch(error => console.error(error));
			}
		}
	}
	return { subTotal };
};
