import { buildCsvDownloadItem, buildPngDownloadItem, type DownloadItem, } from '../actions/download' import type { PieWidgetData } from './types' /** * Download menu items for the Pie widget. Always includes a CSV item with * `series, name, value` columns (one row per slice; series labels come from * `seriesNames` if provided, else `series_`). When `getCaptureEl` is * supplied, prepends a PNG item that rasterises the captured element via * `html2canvas`. */ export function createPieDownloadConfig(args: { filename: string getData: () => PieWidgetData 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', 'name', 'value']] for (const [i, series] of data.entries()) { const seriesName = args.seriesNames?.[i] ?? `series_${i + 1}` for (const slice of series) { rows.push([seriesName, slice.name, slice.value]) } } return rows }, }), ) return items }