import type { HTMLAttributes } from 'react'
import { ResponseRootProvider, useResponseContext, DEMO_ROUNDS, DEMO_FINAL_MD } from './context'
import {
  ResponseThinking,
  ResponseSteps,
  ResponseStepComponent,
  ResponseContent,
  ResponseImageGenerating,
  ResponseDefaultLayout,
} from './compound'
import type {
  ResponseProps,
  ResponseCompound,
  ResponsePhase,
  ResponseStep,
  ResponseStepToolType,
  ResponseRound,
  ResponseBlock,
  BlockType,
} from './types'

// ===== Response 便捷组件 =====

function ResponseBase({
  simulate,
  phase,
  rounds,
  finalMarkdown,
  onSimulateComplete,
  onStepChange,
  thinkingIndicator,
  className,
  ...props
}: ResponseProps & HTMLAttributes<HTMLDivElement>) {
  return (
    <ResponseRootProvider
      simulate={simulate}
      phase={phase}
      rounds={rounds}
      finalMarkdown={finalMarkdown}
      onSimulateComplete={onSimulateComplete}
      onStepChange={onStepChange}
      thinkingIndicator={thinkingIndicator}
    >
      <div {...props}>
        <ResponseDefaultLayout className={className} />
      </div>
    </ResponseRootProvider>
  )
}

ResponseBase.displayName = 'Response'

// ===== 挂载复合组件 =====

type ResponseBaseType = React.FC<ResponseProps & HTMLAttributes<HTMLDivElement>> &
  Partial<ResponseCompound>

const ResponseWithSlots = ResponseBase as ResponseBaseType

ResponseWithSlots.Root = ResponseRootProvider
ResponseWithSlots.Thinking = ResponseThinking
ResponseWithSlots.Steps = ResponseSteps
ResponseWithSlots.Step = ResponseStepComponent
ResponseWithSlots.Content = ResponseContent
ResponseWithSlots.ImageGenerating = ResponseImageGenerating
ResponseWithSlots.DefaultLayout = ResponseDefaultLayout

export const Response = ResponseWithSlots as React.FC<
  ResponseProps & HTMLAttributes<HTMLDivElement>
> &
  ResponseCompound
export const AssistantResponse = Response
export type AssistantResponseProps = ResponseProps

// ===== 导出类型和工具 =====

export type {
  ResponseProps,
  ResponsePhase,
  ResponseStep,
  ResponseStepToolType,
  ResponseRound,
  ResponseBlock,
  BlockType,
  ResponseCompound,
}

export { useResponseContext, DEMO_ROUNDS, DEMO_FINAL_MD }
