import { theme } from '@actual-app/components/theme';
import type {
  balanceTypeOpType,
  GroupedEntity,
  IntervalEntity,
  LegendEntity,
} from '@actual-app/core/types/models';

import { getColorScale } from '#components/reports/chart-theme';

export function calculateLegend(
  intervalData: IntervalEntity[],
  calcDataFiltered: GroupedEntity[],
  groupBy: string,
  graphType?: string,
  balanceTypeOp?: balanceTypeOpType,
): LegendEntity[] {
  const colorScale = getColorScale('qualitative');
  const chooseData =
    groupBy === 'Interval'
      ? intervalData.map(c => {
          return { name: c.date, id: null, data: c };
        })
      : calcDataFiltered.map(c => {
          return { name: c.name, id: c.id, data: c };
        });

  function getColor(data: IntervalEntity | GroupedEntity, index: number) {
    if (graphType === 'DonutGraph') {
      return colorScale[index % colorScale.length];
    }

    if (groupBy === 'Interval') {
      if (balanceTypeOp === 'totalDebts') {
        return theme.reportsNumberNegative;
      }

      if (balanceTypeOp === 'netDebts') {
        return theme.reportsNumberNegative;
      }

      if (
        balanceTypeOp === 'totalTotals' ||
        balanceTypeOp === 'totalBudgeted'
      ) {
        const total =
          balanceTypeOp === 'totalBudgeted'
            ? data.totalBudgeted
            : data.totalTotals;

        if (total < 0) {
          return theme.reportsNumberNegative;
        }

        return theme.reportsNumberPositive;
      }

      if (balanceTypeOp === 'totalAssets' || balanceTypeOp === 'netAssets') {
        return theme.reportsNumberPositive;
      }

      return theme.reportsChartFill;
    }

    return colorScale[index % colorScale.length];
  }

  const legend: LegendEntity[] = chooseData.map((item, index) => {
    return {
      id: item.id || '',
      name: item.name || '',
      color: getColor(item.data, index),
      dataKey: item.id || item.name || '', // Use id for unique data lookup
      uncategorizedId:
        'uncategorizedId' in item.data ? item.data.uncategorizedId : undefined,
    };
  });
  return legend;
}
