import { Outlet } from 'react-router'

import { getStaff } from '~/apis/admin/get-staff'
import { AdminProvider } from '~/contexts/admin'
import { AdminDefaultLayout } from '~/layouts/admin/default'
import { handleCookie } from '~/libs/cookies'

import type { Route } from './+types/_admin.lg-admin'

export const loader = async ({ request }: Route.LoaderArgs) => {
  const { adminToken } = await handleCookie(request)
  let adminData
  if (adminToken) {
    const { data } = await getStaff({
      accessToken: adminToken,
    })
    adminData = data
  }

  return {
    adminData,
  }
}

const AdminLayout = () => {
  return (
    <AdminProvider>
      <AdminDefaultLayout>
        <Outlet />
      </AdminDefaultLayout>
    </AdminProvider>
  )
}

export default AdminLayout
