'use client'

import { ExtendedMessage } from '@/atoms/im'
import ChatCustomImageItem from './ChatCustomImageItem'
import SendGiftItem from './SendGiftItem'
import { CustomMessageType } from '@/types/im'
import CallEndItem from './CallEnditem'
import ChatUnlockImageItem from './ChatUnlockImageItem'
import CallCancelItem from './CallCancelItem'

const ChatCustomItem = ({ message, isUser }: { message: ExtendedMessage; isUser: boolean }) => {
  const { attachment } = message || {}
  const { raw } = attachment || {}
  const customData = JSON.parse(raw || '{}')
  const { type, albumId, unlockPrice } = customData || {}

  if (type === CustomMessageType.IMAGE) {
    if (albumId && !!unlockPrice) {
      return <ChatUnlockImageItem message={message} customData={customData} />
    }
    return <ChatCustomImageItem message={message} customData={customData} isUser={isUser} />
  }

  if (type === CustomMessageType.IM_SEND_GIFT) {
    return <SendGiftItem message={message} customData={customData} />
  }

  if (type === CustomMessageType.CALL) {
    return <CallEndItem message={message} customData={customData} />
  }

  if (type === CustomMessageType.CALL_CANCEL) {
    return <CallCancelItem message={message} customData={customData} />
  }
}

export default ChatCustomItem
