export const getVoteList = (txs: any[]): { addr: string; reward: number }[] => {
  const logsList: any[] = [];
  txs.forEach((tx) => {
    logsList.push(...tx.receipt.logs);
  });

  return logsList
    .filter((i) => i.ty === 8)
    .reduce((pi, ci) => {
      if (ci.log.current.balance - ci.log.prev.balance === 0.25 * 1e8)
        return pi; //筛选出投票地址
      if (pi.find((item: { addr: any }) => item.addr === ci.log.prev.addr)) {
        // 如果本地址已经在结果数组中存在了，就累加balance
        pi.find(
          (item: { addr: any }) => item.addr === ci.log.prev.addr
        ).reward += ci.log.current.balance - ci.log.prev.balance;
        return pi;
      } else {
        return [
          ...pi,
          {
            addr: ci.log.prev.addr,
            reward: ci.log.current.balance - ci.log.prev.balance,
          },
        ];
      }
    }, []);
};

export const getPackList = (txs: any[]): { addr: string; reward: number }[] => {
  const logsList: any[] = [];
  txs.forEach((tx) => {
    logsList.push(...tx.receipt.logs);
  });

  return logsList
    .filter((i) => i.ty === 8)
    .reduce((pi, ci) => {
      if (ci.log.current.balance - ci.log.prev.balance > 0.25 * 1e8) return pi; //筛选出打包地址
      if (pi.find((item: { addr: any }) => item.addr === ci.log.prev.addr)) {
        // 如果本地址已经在结果数组中存在了，就累加balance
        pi.find(
          (item: { addr: any }) => item.addr === ci.log.prev.addr
        ).reward += ci.log.current.balance - ci.log.prev.balance;
        return pi;
      } else {
        return [
          ...pi,
          {
            addr: ci.log.prev.addr,
            reward: ci.log.current.balance - ci.log.prev.balance,
          },
        ];
      }
    }, []);
};

interface iRetTypeOfGetConItem {
  reward: number;
  addr: string;
  type: "pack" | "vote";
}

export const getConsensusList = (txs: any[]): iRetTypeOfGetConItem[] => {
  const logsList: any[] = [];
  txs.forEach((tx) => {
    tx && tx.receipt && tx.receipt.logs && logsList.push(...tx.receipt.logs);
  });

  return logsList
    .filter((i) => i.ty === 8)
    .reduce<iRetTypeOfGetConItem[]>((pi, ci) => {
      if (!ci.log.current || !ci.log.prev) return pi;
      const reward = ci.log.current.balance - ci.log.prev.balance;
      if (reward > 0.25 * 1e8) {
        //筛选出打包地址
        const theOne = pi.find(
          (i) => i.type === "pack" && i.addr === ci.log.current.addr
        );
        if (theOne) {
          theOne.reward += reward;
          return [...pi];
        } else {
          return [
            ...pi,
            {
              type: "pack",
              addr: ci.log.current.addr,
              reward: reward,
            },
          ];
        }
      } else {
        //筛选出投票地址
        const theOne = pi.find(
          (i) => i.type === "vote" && i.addr === ci.log.current.addr
        );

        if (theOne) {
          // 如果本地址已经在结果数组中存在了，就累加balance
          theOne.reward += reward;
          return [...pi];
        } else {
          return [
            ...pi,
            {
              type: "vote",
              addr: ci.log.prev.addr,
              reward,
            },
          ];
        }
      }
    }, [] as iRetTypeOfGetConItem[]);
};
