import { tools } from 'hq-tools';
import Column from './column.js';
import Group from './group.js';
const toStandardColumn = column => {
	if (column.children) {
		let newGroup = new Group(column);
		for (let child of column.children) {
			newGroup.children.push(toStandardColumn(child));
		}
		return newGroup;
	} else {
		return new Column(column);
	}
};

//将配置用的columns转为aggrid使用columns
const toGridColumns = (columns, gridOpts) => {
	let formatedColumns = [];
	//复选框列
	if (gridOpts.rowSelection) {
		formatedColumns.push({
			headerName: '',
			field: '_selection',
			pinned: 'left',
			checkboxSelection: true,
			minWidth: 40,
			width: 40,
			maxWidth: 60,
			filter: false,
			sort: false,
			lockPosition: true,
			cellStyle: {
				padding: '0 11px'
			},
			headerClass:'selection-header',
			headerCheckboxSelection:true,
			style:{
				padding:'100px'
			}
		});
	}
	//序号列
	if (gridOpts.rowIndex) {
		formatedColumns.push({
			headerName: ' ',
			field: '_rowIndex',
			pinned: 'left',
			colId: '_row_' + tools.createUUID(),
			sortable: false,
			lockPosition: true,
			resizable: true,
			minWidth: 60,
			maxWidth: 80,
			filter: false,
			cellStyle: {
				padding: '0 11px',
				'text-align': 'center'
			},
			cellRenderer(params) {
				// if (params.data._subTotal_row) {
				// 	return `<span>页面小计</span>`;
				// }
				return `<span class="grid-index">${params.rowIndex + 1}<span>`;
			}

		});
	}
	for (let column of columns) {
		let newColumn = Object.assign({}, column);
		formatedColumns.push(toStandardColumn(newColumn));
	}
	return formatedColumns;
};

export default toGridColumns;
