import { ChartOptions } from 'chart.js';
import { ChartConfig } from '../chart-config';
import { ScaleService } from './scale.service';
import { Injectable } from '@angular/core';
import { ChartAxis } from '../chart-type';

@Injectable()
export class OptionsService {
  constructor(private scaleService: ScaleService) {}

  options(chart: ChartConfig, data: Record<string, any>[]): ChartOptions {
    const result: ChartOptions = {};

    result.responsive = true;
    result.maintainAspectRatio = false;
    result.scales = this.scaleService.scales(chart, data);

    if (chart.title) {
      result.plugins = result.plugins ?? {};
      result.plugins.title = result.plugins.title ?? {};
      result.plugins.title.text = chart.title;
    }

    const fill =
      (chart?.axis ?? []).filter((axis: ChartAxis) => axis.fill).length > 0;
    if (fill) {
      result.plugins = result.plugins ?? {};
      result.plugins.filler = result.plugins.filler ?? {};
      result.plugins.filler.propagate = true;
    }

    return result;
  }
}
