import {Font} from "../../../generated/ervu_dashboard/model/Font";
import {Chart} from "chart.js";

export class ChartUtils {

  public static isEmpty(chart: Chart) {
    return 0 == chart.data.datasets
               .map(value => value.data.length)
               .reduce((x, y) => x + y, 0);
  }

  public static getTextMetrics(ctx: CanvasRenderingContext2D, text: string, font: Font) {
    ctx.font = this.getFont(font);
    let textMetrics = ctx.measureText(text);
    return {
      height: textMetrics.fontBoundingBoxAscent + textMetrics.fontBoundingBoxDescent,
      width: textMetrics.width
    }
  }

  public static getFont(font: Font): string {
    return [font.weight, `${font.size}px`, font.family]
        .filter(elem => elem)
        .join(' ');
  }

  public static clone<T>(source: T): T {
    return JSON.parse(JSON.stringify(source));
  }
}