import { AppShell } from '@mantine/core';
import { PropsWithChildren, useState } from 'react';
import Header from './header';
import Navbar from './navbar';
import Pullable from './pullable';
import useVisualViewportSize from '../hooks/use-visual-viewport-size';
import useRouterConfig from '../hooks/use-router-config';
import Page from '@/components/page';

const Layout: React.FC<PropsWithChildren> = ({ children }) => {
  const { header } = useRouterConfig();
  const viewport = useVisualViewportSize();
  const [scrollPosition, setScrollPosition] = useState({ x: 0, y: 0 });
  const { withPadding, fullWidth } = useRouterConfig();

  return (
    <AppShell
      id='app-shell'
      layout='alt'
      header={{ height: 60, collapsed: header.collapsed }}
      navbar={{
        width: { base: 0, sm: 100, md: 200, lg: 300 },
        breakpoint: 'sm',
        collapsed: { mobile: true },
      }}
      aside={{
        width: { base: 0, sm: 100, md: 200, lg: 300 },
        breakpoint: 'sm',
        collapsed: { desktop: false, mobile: true }
      }}
      pos='relative'
      h='100dvh'
      mah='100dvh'
      style={{ top: viewport.top }} //, transition: 'top 0.1s ease-in-out' }}
    >
      <Header scrollPosition={scrollPosition} {...header} />
      <AppShell.Main
        pos='relative'
        h='100%'
        mah='100%'
        pb={{ base: 70, md: 0 }}
        px={{ base: 0.01, sm: 100, md: 200, lg: 300 }}
        maw='100dvw'
        style={{ transition: 'none', overflow: 'hidden' }}
      >
        <Pullable scrollPosition={scrollPosition} onScrollPositionChange={setScrollPosition}>
          <Page noPadding={!withPadding} fullWidth={fullWidth}>
            {children}
          </Page>
        </Pullable>
      </AppShell.Main>
      <Navbar />
      <AppShell.Aside withBorder />
    </AppShell>
  );
}

export default Layout;
