import moment from "moment";

const createIncomeStats = async (
  stats: any,
  transactionStats: any,
  duesStats: any
) => {
  if (transactionStats.length === 0 && duesStats.length === 0) {
    return stats;
  } else if (transactionStats.length > duesStats.length) {
    stats = transactionStats.reduce((acc: any, cur: any) => {
      const { month, year } = cur;
      const monthlyTotalDues = duesStats.find((d: any) => d.month === month);

      const stat = {
        totalIncome: cur.totalIncome || 0,
        month,
        year,
        totalDues:
          (monthlyTotalDues && Number(monthlyTotalDues.totalDues)) || 0,
      };
      acc.push(stat);
      return acc;
    }, []);
  } else {
    stats = duesStats.reduce((acc: any, cur: any) => {
      const { month, year } = cur;
      const monthlyTotalIncome = transactionStats.find(
        (d: any) => d.month === month
      );

      const stat = {
        totalIncome:
          (monthlyTotalIncome && Number(monthlyTotalIncome.totalIncome)) || 0,
        month,
        year,
        totalDues: Number(cur.totalDues) || 0,
      };
      acc.push(stat);
      return acc;
    }, []);
  }

  return stats;
};

export default createIncomeStats;
