import React, { forwardRef } from 'react'
import type { ReactNode } from 'react'
import { ChatInputRootProvider } from './context'
import {
  ChatInputAbove,
  ChatInputBox,
  ChatInputAttachments,
  ChatInputInput,
  ChatInputActions,
  ChatInputActionsLeft,
  ChatInputActionsRight,
  ChatInputExpandButton,
  ChatInputFolderButton,
  ChatInputMoreButton,
  ChatInputSendButton,
  ChatInputFooterLeft,
  ChatInputDefaultLayout,
} from './compound'
import type { ChatInputProps } from './types'

export type {
  ChatInputProps,
  FolderPermissionConfig,
  GenerationStatusConfig,
  FooterLeftModelSwitchConfig,
  FooterLeftModelSwitchItem,
} from './types'

function hasAttachmentsValue(attachments: ChatInputProps['attachments']): boolean {
  if (attachments == null || attachments === false) return false
  if (typeof attachments === 'object' && !React.isValidElement(attachments)) {
    return !!(attachments as { files?: ReactNode }).files || !!(attachments as { images?: ReactNode }).images
  }
  return true
}

export interface ChatInputCompound {
  Root: typeof ChatInputRootProvider
  Above: typeof ChatInputAbove
  Box: typeof ChatInputBox
  Attachments: typeof ChatInputAttachments
  Input: typeof ChatInputInput
  Actions: typeof ChatInputActions
  ActionsLeft: typeof ChatInputActionsLeft
  ActionsRight: typeof ChatInputActionsRight
  ExpandButton: typeof ChatInputExpandButton
  FolderButton: typeof ChatInputFolderButton
  MoreButton: typeof ChatInputMoreButton
  SendButton: typeof ChatInputSendButton
  FooterLeft: typeof ChatInputFooterLeft
}

const ChatInputBase = forwardRef<HTMLTextAreaElement, ChatInputProps>(function ChatInput(props, ref) {
  // ChatInput = 复合组件的默认组合（Root + DefaultLayout），便捷导出；主 API 为复合组件
  const hasAttachments = hasAttachmentsValue(props.attachments)
  return (
    <ChatInputRootProvider {...props}>
      <ChatInputDefaultLayout
        ref={ref}
        className={props.className}
        hasAbove={props.showGenerationStatus}
        hasAttachments={hasAttachments}
        hasFooter={props.showFooter}
        aboveOverlap
      />
    </ChatInputRootProvider>
  )
})

// 挂载复合组件后断言为带命名空间的类型，便于赋值时通过类型检查
type ChatInputBaseType = React.ForwardRefExoticComponent<
  ChatInputProps & React.RefAttributes<HTMLTextAreaElement>
> &
  Partial<ChatInputCompound>

const ChatInputBaseWithSlots = ChatInputBase as ChatInputBaseType
ChatInputBaseWithSlots.displayName = 'ChatInput'
ChatInputBaseWithSlots.Root = ChatInputRootProvider
ChatInputBaseWithSlots.Above = ChatInputAbove
ChatInputBaseWithSlots.Box = ChatInputBox
ChatInputBaseWithSlots.Attachments = ChatInputAttachments
ChatInputBaseWithSlots.Input = ChatInputInput
ChatInputBaseWithSlots.Actions = ChatInputActions
ChatInputBaseWithSlots.ActionsLeft = ChatInputActionsLeft
ChatInputBaseWithSlots.ActionsRight = ChatInputActionsRight
ChatInputBaseWithSlots.ExpandButton = ChatInputExpandButton
ChatInputBaseWithSlots.FolderButton = ChatInputFolderButton
ChatInputBaseWithSlots.MoreButton = ChatInputMoreButton
ChatInputBaseWithSlots.SendButton = ChatInputSendButton
ChatInputBaseWithSlots.FooterLeft = ChatInputFooterLeft

export const ChatInput = ChatInputBaseWithSlots as React.ForwardRefExoticComponent<
  ChatInputProps & React.RefAttributes<HTMLTextAreaElement>
> &
  ChatInputCompound

// 扁平命名导出（Root、Above、Box、Attachments、Input、Actions 等）
export { ChatInputRootProvider as ChatInputRoot } from './context'
export {
  ChatInputAbove,
  ChatInputBox,
  ChatInputAttachments,
  ChatInputInput,
  ChatInputActions,
  ChatInputActionsLeft,
  ChatInputActionsRight,
  ChatInputExpandButton,
  ChatInputFolderButton,
  ChatInputMoreButton,
  ChatInputSendButton,
  ChatInputFooterLeft,
} from './compound'
export type { ChatInputRootProps } from './context'
export type {
  ChatInputAboveProps,
  ChatInputBoxProps,
  ChatInputAttachmentsProps,
  ChatInputInputProps,
  ChatInputActionsProps,
  ChatInputActionsLeftProps,
  ChatInputActionsRightProps,
  ChatInputExpandButtonProps,
} from './compound'
