/*
 * Copyright (c) 2023 AccelByte Inc. All Rights Reserved
 * This is licensed software from AccelByte Inc, for limitations
 * and restrictions contact your company contract manager.
 */

import React from 'react'
import { HomePageWidget, homePageFetcher } from '@accelbyte/widgets-v2/widgets/home'
import { RouteObject, json, useLoaderData } from 'react-router'
import { sdk } from '../appConfig'
import { AppWrapper } from '../AppWrapper'
import { AuthCodeExchangeWidget } from '@accelbyte/widgets-v2/widgets/interceptors'

export const HomeRoute: RouteObject = {
  path: '/',
  loader: async () => {
    const initialData = await homePageFetcher({ sdk, isSingleGame: false, appId: 'lightfantastic' })
    return json(initialData)
  },
  Component: () => {
    const initialData = useLoaderData() as Awaited<ReturnType<typeof homePageFetcher>>

    return (
      <AppWrapper>
        <AuthCodeExchangeWidget>{null}</AuthCodeExchangeWidget>
        <HomePageWidget initialData={initialData} />
      </AppWrapper>
    )
  }
}
