import { use } from 'react';

import { cookies } from 'next/headers';

import { Page, PageMobileNavigation, PageNavigation } from '@kit/ui/page';
import { SidebarProvider } from '@kit/ui/shadcn-sidebar';


import { loadUserWorkspace } from '../home/(user)/_lib/server/load-user-workspace';
import { DoctorSidebar } from './_components/doctor-sidebar';
import { DoctorMobileNavigation } from './_components/mobile-navigation';

export const metadata = {
  title: `Doctor`,
};

export const dynamic = 'force-dynamic';

export default function DoctorLayout(props: React.PropsWithChildren) {
  const state = use(getLayoutState());
  const workspace = use(loadUserWorkspace());

  return (
    <SidebarProvider defaultOpen={state.open}>
      <Page style={'sidebar'}>
        <PageNavigation>
          <DoctorSidebar accounts={workspace.accounts} />
        </PageNavigation>

        <PageMobileNavigation>
          <DoctorMobileNavigation />
        </PageMobileNavigation>

        {props.children}
      </Page>
    </SidebarProvider>
  );
}

async function getLayoutState() {
  const cookieStore = await cookies();
  const sidebarOpenCookie = cookieStore.get('sidebar:state');

  return {
    open: sidebarOpenCookie?.value !== 'true',
  };
}
