/** The GraphContextFactory class provides a method to create and return either a HTMLCanvasElement or SVGElement based on the specified mode and container element. */
export class GraphContextFactory {
  /**
   * This function returns either a HTMLCanvasElement or an SVGElement based on the specified mode and container.
   *
   * @param {ChartGraphMode} mode - A string that specifies the mode of the chart graph. It can be either 'canvas' or 'svg'.
   * @param {HTMLElement} container - HTMLElement - the HTML element that will contain the chart/graph.
   * @returns {SVGElement | HTMLCanvasElement} either a HTMLCanvasElement or an SVGElement depending on the value of the `mode` parameter.
   */
  static get(mode, container) {
    if (mode === 'canvas') {
      const canvas = container.querySelector('canvas');
      let context;
      if (canvas) {
        context = canvas;
      }
      else {
        // eslint-disable-next-line no-restricted-globals
        context = document.createElement('canvas');
        context = container.appendChild(context);
      }
      return context;
    }
    else {
      const svg = container.querySelector('svg');
      let context;
      if (svg) {
        context = svg;
      }
      else {
        // eslint-disable-next-line no-restricted-globals
        context = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
        context = container.appendChild(context);
      }
      return context;
    }
  }
}
