import { Outlet } from 'react-router'

import { getSubscriptions } from '~/apis/common/get-subscriptions'
import { NewsProvider } from '~/contexts/news'
import { NewsDefaultLayout } from '~/layouts/news/default'
import { handleCookie } from '~/libs/cookies'

import type { Route } from './+types/_layout'

export const loader = async ({ request }: Route.LoaderArgs) => {
  const { userToken } = await handleCookie(request)
  const { data: subscriptionsData } = await getSubscriptions()

  return {
    userToken,
    subscriptionsData,
  }
}

const NewsLayout = () => {
  return (
    <NewsProvider>
      <NewsDefaultLayout>
        <Outlet />
      </NewsDefaultLayout>
    </NewsProvider>
  )
}

export default NewsLayout
