import { sortArrayOrNestedArray, sortAsExistArray } from '../../../utils/array';
/** The SortPositionsInputProcessor class sorts the data source of a line chart based on the type of x-axis. */
export class SortPositionsInputProcessor {
  /**
   * This function sorts the data source of a line chart based on the type of x-axis.
   *
   * @param {ChartOptions<LineChartDataType>} input - The input parameter is an object of type ChartOptions with a generic type of
   * LineChartDataType. It contains the data source and configuration options for a line chart,
   * including the x-axis type.
   * @returns {ChartOptions<LineChartDataType>} a modified version of the input `ChartOptions` object with the `dataSource` property
   * sorted based on the `xAxis` type. The modified `ChartOptions` object is of type
   * `ChartOptions<LineChartDataType>`.
   */
  process(input) {
    const { dataSource, xAxis } = input;
    const xAxisType = (xAxis === null || xAxis === void 0 ? void 0 : xAxis.type) || 'value';
    let renderData = dataSource;
    switch (xAxisType) {
      case 'time':
        renderData = sortArrayOrNestedArray(dataSource, (data) => data.x.valueOf());
        break;
      case 'value':
        renderData = sortArrayOrNestedArray(dataSource, (data) => data.x);
        break;
      case 'category':
        renderData = sortAsExistArray(dataSource, (item) => item.x, xAxis.sorter);
    }
    input.dataSource = renderData;
    return input;
  }
}
