export function escapeCsvValue(value) {
  const normalized = String(value ?? "");

  if (
    normalized.includes('"') ||
    normalized.includes(",") ||
    normalized.includes("\n")
  ) {
    return `"${normalized.replace(/"/g, '""')}"`;
  }

  return normalized;
}

export function downloadCsv(filename, columns, rows) {
  const header = columns
    .map((column) => escapeCsvValue(column.label))
    .join(",");
  const body = rows
    .map((row) =>
      columns.map((column) => escapeCsvValue(row[column.key] ?? "")).join(","),
    )
    .join("\n");

  const csv = [header, body].filter(Boolean).join("\n");
  const blob = new Blob([csv], { type: "text/csv;charset=utf-8;" });
  const url = URL.createObjectURL(blob);
  const link = document.createElement("a");

  link.href = url;
  link.download = filename;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
  URL.revokeObjectURL(url);
}
