import type { EchartOptionsProps, EchartWidgetData } from '../../echart' /** * Base configuration interface for chart widgets */ export interface ChartWidgetBaseConfig { data?: TData } /** * Parameters for creating a chart widget configuration */ export interface CreateChartWidgetConfigParams< TData = EchartWidgetData, TConfig extends ChartWidgetBaseConfig = ChartWidgetBaseConfig, TType extends string = string, > { /** Widget type identifier (e.g., 'bar', 'pie', 'histogram') */ type: TType /** Function to get EChart options from config */ getOptions: (config: TConfig) => EchartOptionsProps } /** * Return type of the chart widget config function */ export type ChartWidgetConfigResult< TData = EchartWidgetData, TConfig extends ChartWidgetBaseConfig = ChartWidgetBaseConfig, TType extends string = string, > = TConfig & { type: TType } /** * Factory function to create a standardized chart widget config function. * This eliminates duplication across chart widgets by providing a common structure. * * @example * ```ts * export const barConfig = createChartWidgetConfig({ * type: 'bar' as const, * getOptions: ({ data, theme }) => ({ * // EChart configuration * }), * csvModifier: (data) => flattenObjectArrayToCSV(data), * }) * ``` */ export function createChartWidgetConfig< TData = EchartWidgetData, TConfig extends ChartWidgetBaseConfig = ChartWidgetBaseConfig, TType extends string = string, >({ type, getOptions, }: CreateChartWidgetConfigParams): ( config: TConfig, ) => ChartWidgetConfigResult { return function (config: TConfig) { return { ...config, option: getOptions(config), type: type, } as ChartWidgetConfigResult } }