import * as XLSX from "xlsx"
import type { RawData } from "../types"

export const mapWorkbook = (workbook: XLSX.WorkBook, sheetName?: string): RawData[] => {
  // Use the provided sheetName or default to the first sheet
  const sheetToUse = sheetName || workbook.SheetNames[0]
  const worksheet = workbook.Sheets[sheetToUse]

  const data = XLSX.utils.sheet_to_json<string[]>(worksheet, {
    header: 1,
    raw: false,
    defval: "",
  })

  return data
}
