import DB from "src/system/DB";
import { getTitle } from "src/Utility";

export const getForums = async (
  language: string,
  forumGroup?: string,
): Promise<
  {
    forumID: string;
    title: string;
    essays: {
      essayID: number;
      avatarID: string;
      date: string;
      title: string;
      text: string;
    }[];
  }[]
> => {
  const forums = (await DB.getForums(forumGroup)).map(
    ({
      forumID,
      title,
      title1042,
    }: {
      forumID: string;
      title: string;
      title1042: string;
    }): {
      forumID: string;
      title: string;
      essays: {
        essayID: number;
        avatarID: string;
        date: string;
        title: string;
        text: string;
      }[];
    } => ({
      forumID,
      title: getTitle(language, { title, title1042 }),
      essays: [],
    }),
  );

  for await (const forum of forums) {
    forum.essays = await DB.getMostEssays(forum.forumID);
  }

  return forums;
};
