import { cache } from "react";
import { renderBySlug } from "./renderBySlug";
import { metadataBySlug } from "./metadataBySlug";
import { findBySlug } from "@/lib/slug/slugMap";
import { notFound } from "next/navigation";
import type { Metadata } from "next";
import LayoutTypeSetter from "@/components/layout/LayoutTypeSetter"
import { SLUG_CONFIG } from "./slugConfig";

const getCachedSlugData = cache(async (slug: string) => {
    return findBySlug(slug);
});

export async function generateMetadata({
    params,
}: {
    params: Promise<{ slug: string }>;
}): Promise<Metadata> {
    const { slug } = await params;
    const result = await getCachedSlugData(slug);
    if (!result) return { title: "Local PC" };
    return metadataBySlug(result);
}

export default async function SlugPage({ params }: { params: { slug: string } }) {
    const { slug }  = await params;
    const result    = await getCachedSlugData(slug);
    if (!result) notFound();

    const config = SLUG_CONFIG[result.type];
    if (!config) notFound();

    return (
        <LayoutTypeSetter layout={config.layout}>
            {renderBySlug(result)}
        </LayoutTypeSetter>
    );
}