"use server";
cache: "no-store";
import AuthHandler from "@/components/server/admin/authHandler";
import Sidebar, { SidebarEntry } from "@/components/server/admin/views/sidebar";
import { ProjectView } from "@/views/admin/project";
import { PostView } from "@/views/admin/post";
import { redirect } from 'next/navigation'

function Home() {
	return <div>home</div>;
}
function PostManager() {
	return <PostView></PostView>;
}
function ProjectManager() {
	return <ProjectView></ProjectView>;
}

async function getViewMap(): Promise<Map<string, JSX.Element>> {
	return new Map([
		["home", <Home key={0}></Home>],
		["man-post", <PostManager key={1}></PostManager>],
		["man-proj", <ProjectManager key={2}></ProjectManager>],
	]);
}

const sidebarEntries: SidebarEntry[] = [
  { label: "Home", view: "home" },
  { label: "Post Management", view: "man-post" },
  { label: "Project Management", view: "man-proj" },
  { label: "Tag Management", view: "man-tags" },
  { label: "User Management", view: "man-user" },
];

async function getCurrentView(view: string): Promise<JSX.Element> {
	const viewMap = await getViewMap();
	const viewJSX = viewMap.get(view || "home");
	return viewJSX || <Home></Home>;
}

type Props = {
	params: {
		slug: string[];
	};
};

export default async function Page({ params: { slug = ["home"] } }: Props) {
	if (
		(await sidebarEntries)
			.map((entry) => entry.view)
			.indexOf(slug.toString()) < 0
	) redirect("/admin/")
	return (
		<main className="h-screen w-screen flex flex-col p-0 bg-gray-300 box-border m-0">
			<AuthHandler params={null}>
				<Sidebar
					sidebarEntries={sidebarEntries}
					slug={slug.toString()}
				></Sidebar>
				<div className="AdminPanelWrapper flex flex-col p-2">
					{await getCurrentView(slug.toString())}
				</div>
			</AuthHandler>
			{/* <section>{JSON.stringify(cookies().getAll())}</section> */}
		</main>
	);
}
