import { error } from "@sveltejs/kit";
import { getItemsByTag } from "$lib/services";

export async function load({ fetch, params }) {
  try {
    return {
      tag: params.tag,
      items: await Promise.all([
        getItemsByTag("model", params.tag, fetch).then((items) =>
          items?.map((i) => ({ ...i, category: "model", title: i["artist_name"] || i["title"] })),
        ),
        getItemsByTag("video", params.tag, fetch).then((items) =>
          items?.map((i) => ({ ...i, category: "video", title: i["artist_name"] || i["title"] })),
        ),
        getItemsByTag("article", params.tag, fetch).then((items) =>
          items?.map((i) => ({ ...i, category: "article", title: i["artist_name"] || i["title"] })),
        ),
      ]).then(([a, b, c]) => [...a, ...b, ...c]),
    };
  } catch {
    error(404, "Item not found");
  }
}
