import { getNewsBySlug } from '~/apis/common/get-news-by-slug'
import { getUser } from '~/apis/news/get-user'
import { handleCookie } from '~/libs/cookies'
import { NewsDetailPage } from '~/pages/news-detail'

import type { Route } from './+types/_news.detail.$slug'

export const loader = async ({ request, params }: Route.LoaderArgs) => {
  const { userToken } = await handleCookie(request)
  let userData
  if (userToken) {
    const { data } = await getUser({
      accessToken: userToken,
    })
    userData = data
  }
  // TODO need handle if user not access non premium data
  const { data: newsDetailData } = await getNewsBySlug({
    slug: params.slug,
    accessToken: userToken,
  })

  return {
    newsDetailData,
    userData,
  }
}

const NewsDetailLayout = () => <NewsDetailPage />

export default NewsDetailLayout
