import React, { forwardRef } from 'react'
import { useStepperContext } from './Index'
import { cn } from '@/lib/utils/tw-merge'

// Content sub-component
interface ContentProps {
  className?: string
  children: React.ReactNode | React.ReactNode[]
}

const Content = forwardRef<HTMLDivElement, ContentProps>(function Content(
  { className, children },
  ref,
) {
  const { currentStep, isMobile } = useStepperContext()

  // Handle array of children
  if (Array.isArray(children)) {
    return (
      <div className={cn(isMobile ? 'mt-4' : 'mt-6', className)} ref={ref}>
        {React.Children.toArray(children)[currentStep] || null}
      </div>
    )
  }

  // If not an array, just render the children
  return (
    <div className={cn(isMobile ? 'mt-4' : 'mt-6', className)} ref={ref}>
      {children}
    </div>
  )
})

Content.displayName = 'Stepper.Content'

export default Content
