'use server'
import { getCookieAuth } from "@/app/lib/actions/actions";
import AuthHandler from "@/components/server/admin/authHandler";
import Sidebar, { SidebarEntry } from "@/components/server/admin/views/sidebar";
import { cookies } from "next/headers";
import PostView from "@/components/server/admin/views/PostView";
import { ReactNode } from "react";
import ProjectView from "@/components/server/admin/views/ProjectView";

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



function Home(){
  return <div>home</div>
}
function PostManager(){
  return <div>posts</div>
}
function ProjectManager(){
  return <div>projects</div>
}


async function getViewMap():Promise<Map<string, JSX.Element>>{
  return new Map([
  ['home', <Home></Home>],
  ['man-post', <PostView></PostView>],
  ['man-proj', <ProjectView></ProjectView>]
]);
}




async function getSidebarEntries():Promise<Array<SidebarEntry>>{
  return [
    { 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);
  return viewJSX ? viewJSX : <Home></Home>;
}

export default async function Page(props:Props){
  const sidebarEntries:Array<SidebarEntry> = await getSidebarEntries();

  const slug:string|string[] = props.params.slug ? props.params.slug : 'home';

  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>
  );
}