import { getEntry, type CollectionEntry } from "astro:content";
import type { CopyrightedCharacters } from "@content/config";

export async function formatCopyrightedCharacters(copyrightedCharacters: CopyrightedCharacters) {
  return await Promise.all(
    Object.values(
      Object.keys(copyrightedCharacters).reduce(
        (acc, character) => {
          const user = copyrightedCharacters[character]!;
          if (user.id in acc) {
            acc[user.id][1].push(character);
          } else {
            acc[user.id] = [getEntry(user), [character]];
          }
          return acc;
        },
        {} as Record<CollectionEntry<"users">["id"], [Promise<CollectionEntry<"users">>, string[]]>,
      ),
    ).map(async ([userPromise, characters]) => ({ user: await userPromise, characters })),
  );
}
