import { Injectable } from "@nestjs/common"
import * as xlsx from "node-xlsx"
import { isEmpty } from "src/utils/helpers"

@Injectable()
export class ExcelService {
  async generateExcelBuffer(
    data: any,
    options?: any,
    sheetName: string = "sheet",
  ) {
    // Create the Excel buffer
    const buffer = xlsx.build([
      {
        name: sheetName,
        data: data,
        options: isEmpty(options) ? {} : options,
      },
    ])

    return buffer
  }
}
