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,pager) =>{
    let formatedColumns = [];
    //复选框列
    if ( gridOpts.rowSelection ){
        formatedColumns.push({
            headerName: '',
            field:'_selection',
            pinned: 'left',
            checkboxSelection:true,
            minWidth:40,
            width:40,
            maxWidth:40,
            lockPosition:true,
            cellStyle:{
                padding:'0 11px'
            }
        });
    }
    //序号列
    if ( gridOpts.rowIndex ){
        formatedColumns.push({
            headerName: '序号',
            field:'_rowIndex',
            pinned:'left',
            colId:'_row_' + tools.createUUID(),
            sortable:false,
            lockPosition:true,
            resizable:true,
            minWidth:40,
            width:100,
            maxWidth:100,
            cellStyle:{
                padding:'0 11px',
                "text-align":"center"
            },
            cellRenderer(params){
                if ( params.data._subTotal_row){
                    return `<span>页面小计</span>`
                }
                return `<span class="grid-index">${ pager.pageSize * (pager.nowPage - 1) + params.rowIndex + 1}<span>`;
            }
        })
    }
    for ( let column of columns){
        let newColumn = Object.assign({},column);
        delete newColumn.asyncTranslate;
        delete newColumn.fastquery;
        delete newColumn.subTotal;
        delete newColumn.dict;
        delete newColumn.buttons;
        delete newColumn.format;
        if (newColumn){
            // console.log(newColumn.unSortIcon.dictName )
            formatedColumns.push(newColumn);
        }
    }
    return formatedColumns;
}


 export default toGridColumns