// plots/wsiviewer/interactions/annotationsCsv.ts
var CSV_HEADERS = ["filename", "label", "coordinates", "datetime"];
function escapeCell(v) {
  if (v === null || v === void 0) return "";
  const s = typeof v === "object" ? JSON.stringify(v) : String(v);
  const needsQuotes = /[",\n\r]/.test(s);
  const escaped = s.replace(/"/g, '""');
  return needsQuotes ? `"${escaped}"` : escaped;
}
function formatTimestamp(ts) {
  if (!ts) return "";
  const d = new Date(ts);
  if (isNaN(d.getTime())) return ts;
  const pad = (n) => n.toString().padStart(2, "0");
  const yyyy = d.getFullYear();
  const mm = pad(d.getMonth() + 1);
  const dd = pad(d.getDate());
  const hh = pad(d.getHours());
  const min = pad(d.getMinutes());
  const ss = pad(d.getSeconds());
  return `${yyyy}-${mm}-${dd} ${hh}:${min}:${ss}`;
}
function buildAnnotationsCsv(annotations, fixedFilename) {
  if (!Array.isArray(annotations) || annotations.length === 0) return "";
  const rows = annotations.map((a) => {
    const filename = fixedFilename ?? a?.filename ?? a?.image ?? "";
    const label = a?.class ?? "";
    const coordinatesVal = a?.zoomCoordinates ?? "";
    const datetime = formatTimestamp(a?.timestamp);
    const rowValues = [filename, label, coordinatesVal, datetime];
    return rowValues.map((v) => escapeCell(v ?? "")).join(",");
  });
  return [CSV_HEADERS.join(","), ...rows].join("\n");
}

export {
  buildAnnotationsCsv
};
//# sourceMappingURL=chunk-XJGPL2JZ.js.map
