import { ColumnsType } from "antd/lib/table";
import { Column, ColumnShape } from "react-base-table";

/**
 * antd table columns 换 BaseTable columns
 * @param columns 
 * @returns 
 */
export const getBaseTableColumn = (columns: ColumnsType<any>): ColumnShape<any>[] => {
    return columns.map((item: any) => {
        const { key, dataIndex, width, resizable, title, align, sorter, className, render, fixed, ...pro } = item
        let data: ColumnShape<any> = {
            key: key || dataIndex,
            dataKey: key || dataIndex,
            width: width || 100,
            resizable: resizable || true
        }
        if (title && typeof title === 'string') {
            data.title = title
        } else {
            data.headerRenderer = () => {
                return title
            }
        }
        switch (align) {
            case 'center':
                data.align = Column.Alignment.CENTER
                break
            case 'left':
                data.align = Column.Alignment.LEFT
                break
            case 'right':
                data.align = Column.Alignment.RIGHT
                break
        }
        if (sorter) {
            data.sortable = true
        }
        if (className) {
            data.className = className
        }

        switch(fixed) {
            case 'left':
                data.frozen = Column.FrozenDirection.LEFT
                break
            case 'right':
                data.frozen = Column.FrozenDirection.RIGHT
                break
        }

        if (render) {
            data.cellRenderer = ({ rowData, cellData }: any) => {
                return render(cellData, rowData)
            }
        }
        return { ...data, ...pro }
    })
}