/**
 * 提示框数据计算函数
 *
 * 从 u-charts 核心库搬迁的提示框数据计算相关函数
 * 用于计算tooltip显示的数据和配置
 */

// 类型定义
export interface SeriesItem {
  data: (number | null)[] | number[] | { value: number }[];
  name: string;
  color?: string;
  index?: number;
  linearIndex?: number;
  type?: string;
  show?: boolean;
  pointShape?: string;
  legendShape?: string;
  disableLegend?: boolean;
  formatter?: (item: any, titleText: string, index: number, opts: ChartOptions) => string;
}

export interface ChartOptions {
  categories?: string[];
  extra?: ChartExtraOptions;
  chartData?: ChartData;
  xAxis?: XAxisOptions;
  [key: string]: any;
}

export interface ChartExtraOptions {
  tooltip?: TooltipOptions;
  [key: string]: any;
}

export interface TooltipOptions {
  legendShape?: string;
  [key: string]: any;
}

export interface XAxisOptions {
  [key: string]: any;
}

export interface ChartData {
  calPoints?: any[][][];
  xAxisPoints?: number[];
  eachSpacing?: number;
  [key: string]: any;
}

export interface ToolTipOption {
  formatter?: (item: SeriesItem, titleText: string, index: number, opts: ChartOptions) => string;
}

export interface ToolTipDataResult {
  textList: {
    text: string;
    color: string;
    legendShape?: string;
  }[];
  offset: {
    x: number;
    y: number;
  };
}

/**
 * 获取提示框数据
 * 计算tooltip显示的数据
 *
 * @param seriesData - 系列数据数组
 * @param opts - 图表配置选项
 * @param index - 数据索引
 * @param group - 分组索引数组
 * @param categories - 分类数据
 * @param option - 提示框选项（可选）
 * @returns 提示框数据结果
 */
export function getToolTipData(
  seriesData: SeriesItem[],
  opts: ChartOptions,
  index: number | number[],
  group: number[],
  categories: string[],
  option?: ToolTipOption
): ToolTipDataResult {
  const calPoints = opts.chartData?.calPoints || [];
  let points: any = {};
  const indexArray = Array.isArray(index) ? index : [index];
  if (group.length > 0) {
    const filterPoints: any[][] = [];
    for (let i = 0; i < group.length; i++) {
      filterPoints.push(calPoints[group[i]]);
    }
    points = filterPoints[0][indexArray[0]];
  } else {
    for (let i = 0; i < calPoints.length; i++) {
      if (calPoints[i][indexArray[0]]) {
        points = calPoints[i][indexArray[0]];
        break;
      }
    }
  }
  const textList = seriesData.map(function(item) {
    let titleText: string | null = null;
    if (opts.categories && opts.categories.length > 0) {
      titleText = categories[indexArray[0]];
    }
    return {
      text: option?.formatter
        ? option.formatter(item, titleText || '', indexArray[0], opts)
        : item.name + ': ' + String(item.data),
      color: item.color || '',
      legendShape: opts.extra?.tooltip?.legendShape == 'auto'
        ? item.legendShape
        : opts.extra?.tooltip?.legendShape
    };
  });
  const offset = {
    x: Math.round(points.x),
    y: Math.round(points.y)
  };
  return {
    textList,
    offset
  };
}

/**
 * 获取混合图表提示框数据
 * 计算混合图表tooltip显示的数据
 *
 * @param seriesData - 系列数据数组
 * @param opts - 图表配置选项
 * @param index - 数据索引
 * @param categories - 分类数据
 * @param option - 提示框选项（可选）
 * @returns 提示框数据结果
 */
export function getMixToolTipData(
  seriesData: SeriesItem[],
  opts: ChartOptions,
  index: number,
  categories: string[],
  option?: ToolTipOption
): ToolTipDataResult {
  const points = (opts.chartData?.xAxisPoints?.[index] || 0) + (opts.chartData?.eachSpacing || 0) / 2;
  const textList = seriesData.map(function(item) {
    return {
      text: option?.formatter
        ? option.formatter(item, categories[index], index, opts)
        : item.name + ': ' + String(item.data),
      color: item.color || '',
      disableLegend: item.disableLegend ? true : false,
      legendShape: opts.extra?.tooltip?.legendShape == 'auto'
        ? item.legendShape
        : opts.extra?.tooltip?.legendShape
    };
  });
  const filteredTextList = textList.filter(function(item) {
    if ((item as any).disableLegend !== true) {
      return item;
    }
    return undefined;
  }).filter(Boolean);
  const offset = {
    x: Math.round(points),
    y: 0
  };
  return {
    textList: filteredTextList,
    offset
  };
}
