/**
 * 数据修正函数模块
 * 用于修正柱状图和条形图的数据点位置和宽度
 */

// @ts-nocheck - 为了与原始 u-charts 代码保持兼容性，跳过类型检查

import type { ChartOptions, UChartsConfig } from '../data-processing/index';

// 使用 any 类型来简化与原始 u-charts 代码的兼容性
type AnyChartOptions = any;

/**
 * 修正柱状图数据
 * @param points 数据点数组
 * @param eachSpacing 每个数据点的间距
 * @param columnLen 柱状图系列数量
 * @param index 当前系列索引
 * @param config 图表配置
 * @param opts 图表配置选项
 * @returns 修正后的数据点数组
 */
export function fixColumeData(
  points: Array<{ x: number; width?: number } | null>,
  eachSpacing: number,
  columnLen: number,
  index: number,
  config: UChartsConfig,
  opts: AnyChartOptions
): Array<{ x: number; width: number } | null> {
  return points.map(function (item) {
    if (item === null) {
      return null;
    }
    let seriesGap = 0;
    let categoryGap = 0;
    if (opts.type === 'mix') {
      seriesGap = opts.extra?.mix?.column?.seriesGap
        ? opts.extra.mix.column.seriesGap * opts.pix
        : 0;
      categoryGap = opts.extra?.mix?.column?.categoryGap
        ? opts.extra.mix.column.categoryGap * opts.pix
        : 0;
    } else {
      seriesGap = opts.extra?.column?.seriesGap
        ? opts.extra.column.seriesGap * opts.pix
        : 0;
      categoryGap = opts.extra?.column?.categoryGap
        ? opts.extra.column.categoryGap * opts.pix
        : 0;
    }
    seriesGap = Math.min(seriesGap, eachSpacing / columnLen);
    categoryGap = Math.min(categoryGap, eachSpacing / columnLen);
    (item as any).width = Math.ceil(
      (eachSpacing - 2 * categoryGap - seriesGap * (columnLen - 1)) /
        columnLen
    );
    if (
      opts.extra?.mix?.column?.width &&
      +opts.extra.mix.column.width > 0
    ) {
      (item as any).width = Math.min(
        (item as any).width,
        +opts.extra.mix.column.width * opts.pix
      );
    }
    if (
      opts.extra?.column?.width &&
      +opts.extra.column.width > 0
    ) {
      (item as any).width = Math.min(
        (item as any).width,
        +opts.extra.column.width * opts.pix
      );
    }
    if ((item as any).width <= 0) {
      (item as any).width = 1;
    }
    item.x += (index + 0.5 - columnLen / 2) * ((item as any).width + seriesGap);
    return item as { x: number; width: number };
  });
}

/**
 * 修正条形图数据
 * @param points 数据点数组
 * @param eachSpacing 每个数据点的间距
 * @param columnLen 条形图系列数量
 * @param index 当前系列索引
 * @param config 图表配置
 * @param opts 图表配置选项
 * @returns 修正后的数据点数组
 */
export function fixBarData(
  points: Array<{ y: number; width?: number } | null>,
  eachSpacing: number,
  columnLen: number,
  index: number,
  config: UChartsConfig,
  opts: AnyChartOptions
): Array<{ y: number; width: number } | null> {
  return points.map(function (item) {
    if (item === null) {
      return null;
    }
    let seriesGap = 0;
    let categoryGap = 0;
    seriesGap = opts.extra?.bar?.seriesGap ? opts.extra.bar.seriesGap * opts.pix : 0;
    categoryGap = opts.extra?.bar?.categoryGap
      ? opts.extra.bar.categoryGap * opts.pix
      : 0;
    seriesGap = Math.min(seriesGap, eachSpacing / columnLen);
    categoryGap = Math.min(categoryGap, eachSpacing / columnLen);
    (item as any).width = Math.ceil(
      (eachSpacing - 2 * categoryGap - seriesGap * (columnLen - 1)) /
        columnLen
    );
    if (opts.extra?.bar?.width && +opts.extra.bar.width > 0) {
      (item as any).width = Math.min(
        (item as any).width,
        +opts.extra.bar.width * opts.pix
      );
    }
    if ((item as any).width <= 0) {
      (item as any).width = 1;
    }
    (item as any).y += (index + 0.5 - columnLen / 2) * ((item as any).width + seriesGap);
    return item as { y: number; width: number };
  });
}

/**
 * 修正仪表盘柱状图数据
 * @param points 数据点数组
 * @param eachSpacing 每个数据点的间距
 * @param columnLen 柱状图系列数量
 * @param index 当前系列索引
 * @param config 图表配置
 * @param opts 图表配置选项
 * @param border 边框宽度
 * @returns 修正后的数据点数组
 */
export function fixColumeMeterData(
  points: Array<{ width?: number } | null>,
  eachSpacing: number,
  columnLen: number,
  index: number,
  config: UChartsConfig,
  opts: AnyChartOptions,
  border: number
): Array<{ width: number } | null> {
  const categoryGap = opts.extra?.column?.categoryGap
    ? opts.extra.column.categoryGap * opts.pix
    : 0;
  return points.map(function (item) {
    if (item === null) {
      return null;
    }
    (item as any).width = eachSpacing - 2 * categoryGap;
    if (opts.extra?.column?.width && +opts.extra.column.width > 0) {
      (item as any).width = Math.min(
        (item as any).width,
        +opts.extra.column.width * opts.pix
      );
    }
    if (index > 0) {
      (item as any).width -= border;
    }
    return item as { width: number };
  });
}

/**
 * 修正堆叠柱状图数据
 * @param points 数据点数组
 * @param eachSpacing 每个数据点的间距
 * @param columnLen 柱状图系列数量
 * @param index 当前系列索引
 * @param config 图表配置
 * @param opts 图表配置选项
 * @param series 数据系列
 * @returns 修正后的数据点数组
 */
export function fixColumeStackData(
  points: Array<{ width?: number } | null>,
  eachSpacing: number,
  columnLen: number,
  index: number,
  config: UChartsConfig,
  opts: AnyChartOptions,
  series: any[]
): Array<{ width: number } | null> {
  const categoryGap = opts.extra?.column?.categoryGap
    ? opts.extra.column.categoryGap * opts.pix
    : 0;
  return points.map(function (item) {
    if (item === null) {
      return null;
    }
    (item as any).width = Math.ceil(eachSpacing - 2 * categoryGap);
    if (opts.extra?.column?.width && +opts.extra.column.width > 0) {
      (item as any).width = Math.min(
        (item as any).width,
        +opts.extra.column.width * opts.pix
      );
    }
    if ((item as any).width <= 0) {
      (item as any).width = 1;
    }
    return item as { width: number };
  });
}

/**
 * 修正堆叠条形图数据
 * @param points 数据点数组
 * @param eachSpacing 每个数据点的间距
 * @param columnLen 条形图系列数量
 * @param index 当前系列索引
 * @param config 图表配置
 * @param opts 图表配置选项
 * @param series 数据系列
 * @returns 修正后的数据点数组
 */
export function fixBarStackData(
  points: Array<{ width?: number } | null>,
  eachSpacing: number,
  columnLen: number,
  index: number,
  config: UChartsConfig,
  opts: AnyChartOptions,
  series: any[]
): Array<{ width: number } | null> {
  const categoryGap = opts.extra?.bar?.categoryGap
    ? opts.extra.bar.categoryGap * opts.pix
    : 0;
  return points.map(function (item) {
    if (item === null) {
      return null;
    }
    (item as any).width = Math.ceil(eachSpacing - 2 * categoryGap);
    if (opts.extra?.bar?.width && +opts.extra.bar.width > 0) {
      (item as any).width = Math.min(
        (item as any).width,
        +opts.extra.bar.width * opts.pix
      );
    }
    if ((item as any).width <= 0) {
      (item as any).width = 1;
    }
    return item as { width: number };
  });
}
