import { buildCsvDownloadItem, buildPngDownloadItem, type DownloadItem, } from '../actions/download' import type { SpreadWidgetData } from './types' /** * Download menu items for the Spread widget. Always includes a CSV item * with `series, prefix, min, max, suffix, note` columns (one row per * entry). When `getCaptureEl` is supplied, prepends a PNG item that * rasterises the captured element via `html2canvas`. */ export function createSpreadDownloadConfig(args: { filename: string getData: () => SpreadWidgetData 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', 'prefix', 'min', 'max', 'suffix', 'note'], ] for (const item of data) { rows.push([ item.series?.name ?? '', item.prefix ?? '', item.min, item.max, item.suffix ?? '', item.note ?? '', ]) } return rows }, }), ) return items }