import fs from 'fs';
import * as AllColumns from './columns/index.js';
import { getTableColumnsConfigTemplate, getTableDefineTemplate } from './template.js';

const set = new Set()
const errorLog = {}

//  AlignType 对齐方式 - 1:居中对齐 2:左对齐 3:右对齐
function getAlignType(val) {
    let result = 1
    switch(val) {
        case 'left':
            result = 2
            break
        case 'right':
            result = 3
            break
        case 'center':
            result = 1
            break
    }
    return result
}

function hanldeColum(modules) {
    const columNames = Object.keys(modules)
    let result = ''
    columNames.forEach(key => {
        if(set.has(key)) {
            errorLog[key] = `重复的key：${key}`
        } else {
            result += getTableDefineTemplate(key)
            // 当前表头key对应的数据
            const tableColumn = AllColumns[key]
            tableColumn.forEach((column, index) => {
                const { title, align, width} = column
                const param = {
                    tableKey: key,
                    columnField: column.key,
                    columnTitle: title,
                    columnWidth: width ? width : 120,
                    orderIndex: (index + 1) * 10,
                    IsShow: 1,
                    alignType: getAlignType(align)
                }
                result += getTableColumnsConfigTemplate(param)
            })
        }
    })
    return result
}
const result = hanldeColum(AllColumns)

fs.writeFileSync("./output/fxgl_v3_pcweb_table_define.sql", result);