import { getForums as dbGetForums, getMostEssays } from "@/app/www/system/DB";
import { getTitle } from "@/app/www/utilities/Utility";

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

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

  return forums;
};
