import { convertArrayToNestedArray, flatNestedArray } from '../../../utils/array';
import { map as d3Map, timeParse as d3TimeParse } from '../../../polyfill/d3';
/** The FormatTimePositionsInputProcessor class processes input data for a line chart by formatting time positions if the x-axis type is 'time'. */
export class FormatTimePositionsInputProcessor {
  /**
   * This function processes chart options for a line chart, converting the x-axis data to a time format if necessary.
   *
   * @param {ChartOptions<LineChartDataType>} input - A ChartOptions object that contains the data source and configuration options for a line chart.
   * @returns {ChartOptions<LineChartDataType>} A modified version of the input ChartOptions object with the dataSource property
   * potentially transformed based on the xAxis type.
   */
  process(input) {
    const { dataSource, xAxis } = input;
    const xAxisType = (xAxis === null || xAxis === void 0 ? void 0 : xAxis.type) || 'value';
    let renderData = dataSource;
    if (xAxisType === 'time') {
      renderData = this.formatTimePositionArray(dataSource);
    }
    input.dataSource = renderData;
    return input;
  }
  /**
   * format date position array function for chart, parse date string yyyy-MM-DD to date
   *
   * @param {StringCategorizedChartData[] | StringCategorizedChartData[][]} data data source
   * @returns {DateCategorizedChartData[] | DateCategorizedChartData[][]} result array
   */
  formatTimePositionArray(data) {
    let result = [];
    let lengthArray = [];
    const flatArray = flatNestedArray(data);
    const flatArray_date = d3Map(flatArray, (d) => {
      return { x: (typeof d.x === 'string' ? d3TimeParse('%Y-%m-%d')(d.x) : d.x), y: d.y };
    });
    if (data && Array.isArray(data[0])) {
      lengthArray = data.map((item) => item.length);
      result = convertArrayToNestedArray(flatArray_date, lengthArray);
    }
    else {
      result = flatArray_date;
    }
    return result;
  }
}
