import 'server-only'

import { cache } from 'react'
import { createCallerFactory } from '@/server/trpc'
import { createContext } from '@/server/context'
import { appRouter } from '@/server/routers/_app'

/**
 * Create a server-side tRPC caller that can be used in Server Components
 */
const createCaller = createCallerFactory(appRouter)

/**
 * Cached tRPC caller for use in Server Components
 * Uses React's cache() to deduplicate context creation
 */
export const api = cache(async () => {
  const context = await createContext()
  return createCaller(context)
})
