import { getCategories } from '~/apis/common/get-categories'
import { getNews } from '~/apis/common/get-news'
import { APP } from '~/configs/meta'
import { NewsCategoriesPage } from '~/pages/news-categories'

import type { Route } from './+types/_news.category.$code'

export const loader = async ({ params }: Route.LoaderArgs) => {
  const { data: categoriesData } = await getCategories()
  const categoryData = categoriesData.find(
    (category) => category.code === params.code,
  )
  const { data: newsData } = await getNews({ categories: [params.code] })
  return { categoryData, newsData }
}

export const meta = ({ data }: Route.MetaArgs) => {
  const { categoryData } = data
  const metaTitle = APP.title
  const title = `${categoryData?.name} - ${metaTitle}`

  return [
    {
      title,
    },
  ]
}

const NewsCategoriesLayout = () => <NewsCategoriesPage />

export default NewsCategoriesLayout
