import { buildCsvDownloadItem, buildPngDownloadItem, type DownloadItem, } from '../actions/download' import type { ScatterplotWidgetData } from './types' /** * Download menu items for the Scatterplot widget. Always includes a CSV * item with `series, x, y` columns (one row per point). When * `getCaptureEl` is supplied, prepends a PNG item that rasterises the * captured element via `html2canvas`. */ export function createScatterplotDownloadConfig(args: { filename: string getData: () => ScatterplotWidgetData seriesNames?: readonly string[] getCaptureEl?: () => HTMLElement | null pngPixelRatio?: number pngBackgroundColor?: string | null }): DownloadItem[] { const items: DownloadItem[] = [] if (args.getCaptureEl) { items.push( buildPngDownloadItem({ filename: args.filename, getCaptureEl: args.getCaptureEl, pixelRatio: args.pngPixelRatio, backgroundColor: args.pngBackgroundColor, }), ) } items.push( buildCsvDownloadItem({ filename: args.filename, getRows: () => { const data = args.getData() const rows: unknown[][] = [['series', 'x', 'y']] for (const [i, series] of data.entries()) { const seriesName = args.seriesNames?.[i] ?? `series_${i + 1}` for (const [x, y] of series) { rows.push([seriesName, x, y]) } } return rows }, }), ) return items }