/**
 * 饼图数据项接口
 */
export interface PieDataItem {
  data: number | null;
  _proportion_?: number;
  _rose_proportion_?: number;
  _radius_?: number;
  _start_?: number;
  color?: string;
  name?: string;
  formatter?: (value: number) => string;
  textSize?: number;
  [key: string]: any;
}

/**
 * 获取饼图数据点
 * 计算饼图扇区的比例、角度和半径
 * @param series - 饼图数据项数组
 * @param radius - 饼图半径
 * @param process - 动画进度（0-1），默认为1（无动画）
 * @returns 包含计算后的比例和角度的饼图数据项数组
 */
export function getPieDataPoints(
  series: PieDataItem[],
  radius: number,
  process: number = 1
): PieDataItem[] {
  let count = 0;
  let _start_ = 0;
  for (let i = 0; i < series.length; i++) {
    let item = series[i];
    item.data = item.data === null ? 0 : item.data;
    count += item.data;
  }
  for (let i = 0; i < series.length; i++) {
    let item = series[i];
    item.data = item.data === null ? 0 : item.data;
    if (count === 0) {
      item._proportion_ = 1 / series.length * process;
    } else {
      item._proportion_ = item.data / count * process;
    }
    item._radius_ = radius;
  }
  for (let i = 0; i < series.length; i++) {
    let item = series[i];
    item._start_ = _start_;
    _start_ += 2 * item._proportion_! * Math.PI;
  }
  return series;
}

/**
 * 获取玫瑰图数据点
 * 计算玫瑰图（南丁格尔图）扇区的比例、角度和半径
 * @param series - 饼图数据项数组
 * @param type - 玫瑰图类型（'area' 或 'radius'）
 * @param minRadius - 玫瑰图最小半径
 * @param radius - 玫瑰图最大半径
 * @param process - 动画进度（0-1），默认为1（无动画）
 * @returns 包含计算后的比例和角度的饼图数据项数组
 */
export function getRoseDataPoints(
  series: PieDataItem[],
  type: string,
  minRadius: number,
  radius: number,
  process: number = 1
): PieDataItem[] {
  let count = 0;
  let _start_ = 0;
  let dataArr: number[] = [];
  for (let i = 0; i < series.length; i++) {
    let item = series[i];
    item.data = item.data === null ? 0 : item.data;
    count += item.data;
    dataArr.push(item.data);
  }
  let minData = Math.min.apply(null, dataArr);
  let maxData = Math.max.apply(null, dataArr);
  let radiusLength = radius - minRadius;
  for (let i = 0; i < series.length; i++) {
    let item = series[i];
    item.data = item.data === null ? 0 : item.data;
    if (count === 0) {
      item._proportion_ = 1 / series.length * process;
      item._rose_proportion_ = 1 / series.length * process;
    } else {
      item._proportion_ = item.data / count * process;
      if(type == 'area'){
        item._rose_proportion_ = 1 / series.length * process;
      }else{
        item._rose_proportion_ = item.data / count * process;
      }
    }
    item._radius_ = minRadius + radiusLength * ((item.data - minData) / (maxData - minData)) || radius;
  }
  for (let i = 0; i < series.length; i++) {
    let item = series[i];
    item._start_ = _start_;
    _start_ += 2 * item._rose_proportion_! * Math.PI;
  }
  return series;
}
