import {AnalyticalScope, Behavior, Visible} from "@webbpm/base-package";
import {BarElement, Chart, ChartMeta} from "chart.js";
import {ChartPlugin} from "./ChartPlugin";
import {ErvuChartV2} from "../ErvuChartV2";

@AnalyticalScope(ErvuChartV2)
export class BarStackMiddleBorderRoundingChartPlugin extends Behavior implements ChartPlugin {
  @Visible('false')
  id: string = 'bar-stack-middle-border-rounding';

  /**
   * Закругление внутренних стыков в стеке
   */
  beforeDatasetDraw(chart: Chart, args: { index: number; meta: ChartMeta }): void {
    const indexAxis = chart.config.options.indexAxis || 'x';
    const base = indexAxis === 'x' ? chart.chartArea.bottom : chart.chartArea.left;
    const meta = args.meta;
    if (meta.type === 'bar') {
      meta.data.forEach((barElement: BarElement) => {
        Object.assign(barElement, {base: base, enableBorderRadius: true});
      })
    }
  }
}
