import { timeFormat } from '../../../polyfill/d3/d3-time-format-polyfill';
import { timeSecond, timeMinute, timeHour, timeDay, timeMonth, timeWeek, timeYear, } from '../../../polyfill/d3/d3-time-polyfill';
/** The XAxisInputProcessor class generates X-axis options for a line chart based on input data and previous layout results. */
export class XAxisLabelFormatterInputProcessor {
  constructor() {
    this.formatMillisecond = timeFormat('.%L');
    this.formatSecond = timeFormat(':%S');
    this.formatMinute = timeFormat('%I:%M');
    //return 18:00
    this.formatHour = timeFormat('%H:00');
    //return 01
    this.formatDay = timeFormat('%d');
    //return Jan 01
    this.formatWeek = timeFormat('%b %d');
    //return Jan
    this.formatMonth = timeFormat('%b');
    //return 2023
    this.formatYear = timeFormat('%Y');
  }
  /**
   * This function checks if the xAxis type is 'time' and sets a tickFormat if a labelFormatter is not provided.
   *
   * @param {ChartOptions<unknown>} input - An object containing chart options, including the type of x-axis and a label
   * formatter function.
   * @param {ChartExtend<AxisScale<AxisDomain>>} previousOutput - `previousOutput` is a parameter that represents the output of the previous
   * step in the charting process. It is of type `ChartExtend<AxisScale<AxisDomain>>`, which is an
   * object that contains information about the chart's axis scales. This parameter is used to update
   * the axis scale information
   * @returns {ChartExtend<AxisScale<AxisDomain>>} a `ChartExtend` object that contains an `AxisScale` object with a tick format function.
   * The tick format function is either the `timeFormatter` function or the previous tick format
   * function if the `xAxisType` is not `'time'`.
   */
  process(input, previousOutput) {
    const { xAxis } = input;
    const xAxisType = (xAxis === null || xAxis === void 0 ? void 0 : xAxis.type) || 'value';
    if (xAxisType === 'time' && typeof previousOutput.labelFormatter !== 'function') {
      // eslint-disable-next-line jsdoc/require-jsdoc
      previousOutput.scale['tickFormat'] = () => this.timeFormatter.bind(this);
    }
    return previousOutput;
  }
  /**
   * override the scaleTime default ticks label formatter function
   * The function formats a given date object based on its time interval using a set of predefined format strings.
   *
   * @param {Date} date - The input date that needs to be formatted.
   * @returns {string} A string representing the formatted date based on the input date parameter. The format of
   * the string depends on the time interval between the input date and the current time, and is
   * determined by the conditional statements in the function.
   */
  timeFormatter(date) {
    return (timeSecond(date) < date
      ? this.formatMillisecond
      : timeMinute(date) < date
        ? this.formatSecond
        : timeHour(date) < date
          ? this.formatMinute
          : timeDay(date) < date
            ? this.formatHour
            : timeMonth(date) < date
              ? timeWeek(date) < date
                ? this.formatDay
                : this.formatWeek
              : timeYear(date) < date
                ? this.formatMonth
                : this.formatYear)(date);
  }
}
