import { Workbook } from 'exceljs';
const defaultHeaderStyle = {
    fill: { type: 'pattern', pattern: 'solid', fgColor: { argb: 'CCCCCC' } },
    border: {
        top: { style: 'thin' },
        left: { style: 'thin' },
        bottom: { style: 'thin' },
        right: { style: 'thin' }
    },
    font: { bold: true }
};
export class ExcelEaasExport {
    constructor() {
        this._excel = new Workbook();
    }
    addWorksheet(sheetName = 'sheet') {
        const worksheet = this._excel.addWorksheet(sheetName);
        this._worksheet = worksheet; // 생성된 워크시트로 현재 워크시트 설정
    }
    setWorksheetByName(sheetName) {
        const worksheet = this._excel.getWorksheet(sheetName);
        if (worksheet) {
            this._worksheet = worksheet;
        }
        else {
            throw new Error(`Worksheet with name ${sheetName} does not exist`);
        }
    }
    setHeader(headers, startRow = 1) {
        const worksheet = this._worksheet;
        worksheet.columns = headers.map(item => {
            const { key, width = 10 } = item;
            return {
                key,
                width,
                outlineLevel: 1,
                hidden: false
            };
        });
        headers.forEach((item, idx) => {
            const { key, header, dataAlign = 'left', dataVerticalAlign = 'middle', backgroundColor = 'CCCCCC' } = item;
            // 데이터 정렬
            worksheet.getColumn(key).alignment = { horizontal: dataAlign, vertical: dataVerticalAlign };
            // Get the header cell in row 4
            const headerCell = worksheet.getRow(startRow).getCell(idx + 1);
            // Set the header cell value
            headerCell.value = header;
            // Set header style
            const headerStyle = {
                ...defaultHeaderStyle,
                fill: { type: 'pattern', pattern: 'solid', fgColor: { argb: backgroundColor } }
            };
            headerCell.style = headerStyle;
            // Set header alignment (centered)
            headerCell.alignment = { horizontal: 'center', vertical: 'middle' };
        });
    }
    addRow(row) {
        // 데이터 추가
        this._worksheet.addRow(row);
        // 데이터 테두리 스타일
        const border = {
            top: { style: 'thin' },
            left: { style: 'thin' },
            bottom: { style: 'thin' },
            right: { style: 'thin' }
        };
        // 추가된 로우에 테두리 스타일 적용
        const lastRow = this._worksheet.lastRow;
        for (let i = 1; i <= this._worksheet.columnCount; i++) {
            const cell = lastRow.getCell(i);
            cell.border = border;
            if (typeof cell.value === 'number') {
                cell.numFmt = '#,##0';
            }
        }
    }
    setSummaryData(summary1, summary2) {
        if (!this._worksheet) {
            throw new Error('Worksheet is not defined. Call addWorksheet or setWorksheetByName first.');
        }
        const worksheet = this._worksheet;
        // 1,2와 2,2 셀에 데이터를 입력합니다.
        worksheet.getRow(1).getCell(2).value = summary1[1];
        worksheet.getRow(2).getCell(2).value = summary2[1];
        // 헤더 생성 후 스타일 적용
        worksheet.getRow(1).getCell(1).value = summary1[0];
        worksheet.getRow(1).getCell(1).style = defaultHeaderStyle;
        worksheet.getRow(2).getCell(1).value = summary2[0];
        worksheet.getRow(2).getCell(1).style = defaultHeaderStyle;
        worksheet.getRow(2).getCell(2).numFmt = '#,##0';
        worksheet.getRow(1).getCell(2).style = {
            ...defaultHeaderStyle,
            fill: { type: 'pattern', pattern: 'solid', fgColor: { argb: 'FFFFFF' } },
            font: { bold: false }
        };
        worksheet.getRow(2).getCell(2).style = worksheet.getRow(1).getCell(2).style;
        worksheet.getRow(1).getCell(2).alignment = { horizontal: 'center', vertical: 'middle' };
        worksheet.getRow(2).getCell(2).alignment = { horizontal: 'center', vertical: 'middle' };
    }
    async getBuffer() {
        return this._excel.xlsx.writeBuffer();
    }
    get excel() {
        return this._excel;
    }
    set excel(workbook) {
        this._excel = workbook;
    }
    get worksheet() {
        return this._worksheet;
    }
    set worksheet(worksheet) {
        this._worksheet = worksheet;
    }
}
//# sourceMappingURL=excel-eaas-export.js.map