import * as XLSX from "xlsx";

export type Template = {
	name: string;
	headers: string[];
};

export const excelTemplate = async (data: Template) => {
	/**
	 * Creates the standard Template for bulk orders in
	 */

	// const headers = [
	//     [
	//         "CustomerArticleNumber",
	//         "CustomerOrderNumber",
	//         "CustomerLineNumber",
	//         "CustomerRealeaseNumber",
	//         "Quantity",
	//         "DeliveryDate",
	//         "CustomerID",
	//         "Remark",
	//         // "InvoiceID",
	//     ],
	// ];

	// create a new workbook
	const wb = XLSX.utils.book_new();
	const ws = XLSX.utils.aoa_to_sheet([data.headers]);
	//const ws2 = XLSX.utils.aoa_to_sheet(headers2);

	const columnWidths = data.headers.map((header) => ({
		width: header.length + 2,
	}));

	ws["!cols"] = columnWidths;

	// append the worksheet to the workbook
	XLSX.utils.book_append_sheet(wb, ws, `Sheet1`);
	//XLSX.utils.book_append_sheet(wb, ws2, `Sheet2`);

	// Creates the file to disk'
	// XLSX.writeFile(wb, data.name);

	// Write the workbook to a buffer and return it
	const excelBuffer = XLSX.write(wb, { bookType: "xlsx", type: "buffer" });

	return excelBuffer;
};
