import { Injectable } from '@angular/core';
import { ChartConfig } from '../chart-config';
import { ChartAxis, ChartAxisPosition } from '../chart-type';

@Injectable()
export class LabelService {
  constructor() {}

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

    (chart?.axis ?? [])
      .filter((axis: ChartAxis) =>
        [ChartAxisPosition.Top, ChartAxisPosition.Bottom].includes(
          axis.position
        )
      )
      .map((axis: ChartAxis, index: number) => {
        if (index > 0) return;

        const sources = Array.isArray(axis.source)
          ? axis.source
          : [axis.source];
        const source = sources[0];

        (data ?? []).map((record: Record<string, any>) =>
          result.push(record[sources[0]])
        );
      });

    return result;
  }
}
