'use client'

import React from 'react'
import { ToastContainer } from 'react-toastify'

import ProtectedRoute from '@/lib/components/ProtectedRoute'
import { SidebarProvider, useSidebar } from '@/lib/context/SidebarContext'
import Sidebar from './_partials/Sidebar'
import Navbar from './_partials/Navbar'

function DashboardContent({ children }: { children: React.ReactNode }) {
  const { isSidebarOpened } = useSidebar()

  return (
    <main className="min-h-screen flex">
      <Sidebar />

      <div
        className={`transition-all ${!isSidebarOpened ? '-ml-64 w-full' : 'max-[930px]:-ml-64 max-[930px]:w-full max-[1140px]:w-175 max-[1300px]:w-200 xl:max-w-none xl:w-0 grow overflow-x-hidden'}`}
      >
        <Navbar />

        {children}
      </div>
    </main>
  )
}

export default function DashboardLayout({
  children,
}: Readonly<{ children: React.ReactNode }>) {
  return (
    <ProtectedRoute>
      <SidebarProvider>
        <DashboardContent>{children}</DashboardContent>

        <ToastContainer limit={3} style={{ fontFamily: 'Figtree' }} />
      </SidebarProvider>
    </ProtectedRoute>
  )
}
