import { Result, AppError, fail, success } from '../utils/result';
import { WidgetDataLoaderInterface } from './widget-data-loader-interface';
import { WidgetInterface } from './widget-interface';

export class InteractiveWidget
  implements WidgetInterface<any, any, any, any, any>
{
  constructor(
    public dataLoader: WidgetDataLoaderInterface<any, any, any>,
    public type: string,
  ) {}

  async render(
    widgetParams: any,
    dataLoaderParams: any,
    dashboardParams: any,
    dashboardId: string,
    widgetId: string,
  ): Promise<Result<any, AppError>> {
    const data = await this.dataLoader.load(
      dataLoaderParams,
      dashboardParams,
      dashboardId,
      widgetId,
    );
    return data.error ? fail(data.error) : success(data.result);
  }
}

export function createInteractiveWidget(
  dataLoader: WidgetDataLoaderInterface<any, any, any>,
  type: string,
): WidgetInterface<any, any, any, any, any> {
  return new InteractiveWidget(dataLoader, type);
}
