'use client'

import { DebugContextCard } from './debug-context-card'
import { ResponseOptionsCard } from './response-options-card'
import { EndpointContextCard } from './endpoint-context-card'
import { decodeCardMessage, type CardData, type CardActions } from './types'

// Export types and utilities
export * from './types'
export { DebugContextCard } from './debug-context-card'
export { ResponseOptionsCard, detectErrorType, type ErrorType } from './response-options-card'
export { EndpointContextCard } from './endpoint-context-card'

interface CardRendererProps {
  content: string
  actions?: CardActions
}

/**
 * Renders a card based on the encoded message content.
 * Returns null if the content is not a valid card message.
 */
export function CardRenderer({ content, actions }: CardRendererProps) {
  const cardData = decodeCardMessage(content)
  
  if (!cardData) {
    return null
  }
  
  return <CardByType data={cardData} actions={actions} />
}

/**
 * Renders the appropriate card component based on the card type.
 */
function CardByType({ data, actions }: { data: CardData; actions?: CardActions }) {
  switch (data.type) {
    case 'debug_context':
      return <DebugContextCard context={data.context} />
    
    case 'response_options':
      return <ResponseOptionsCard errorType={data.errorType} actions={actions} />
    
    case 'endpoint_context':
      return <EndpointContextCard endpoint={data.endpoint} />
    
    default:
      return null
  }
}
