"use client"

import { mailLabelShouldShowInListStrip } from "@/components/gmail/mail-label-pills"
import { EmailView } from "@/components/gmail/email-view"
import { LABEL_PICKER_EXCLUDE } from "@/lib/mail-list/label-actions"
import { threadStoreId } from "@/lib/mail-settings/list-row-id"
import type { EmailListData } from "@/components/gmail/email-list/hooks/use-email-list-data"
import type { EmailListReading } from "@/components/gmail/email-list/hooks/use-email-list-reading"
import type { EmailListSelection } from "@/components/gmail/email-list/hooks/use-email-list-selection"

type EmailListEmailViewPaneProps = {
  data: EmailListData
  reading: EmailListReading
  selection: EmailListSelection
}

export function EmailListEmailViewPane({
  data,
  reading,
  selection,
}: EmailListEmailViewPaneProps) {
  const {
    openEmail,
    openEmailThreadRoot,
    isSingleMessageView,
    handleNavigateToLabel,
    singleNotSpam,
  } = reading
  const { toggleStar } = selection
  const {
    starredEmails,
    listRowLabelBgByTextLower,
    sidebarNav,
    selectedFolder,
  } = data

  if (!openEmail) return null

  return (
    <EmailView
      email={openEmail}
      threadRoot={openEmailThreadRoot}
      isSingleMessageView={isSingleMessageView}
      onToggleStar={toggleStar}
      isStarred={
        starredEmails.includes(threadStoreId(openEmail)) ||
        openEmail.starred
      }
      onNavigateToLabel={handleNavigateToLabel}
      onNotSpam={openEmail.spam === true ? singleNotSpam : undefined}
      labelBgByText={listRowLabelBgByTextLower}
      emailLabelToSidebarFolderId={sidebarNav.emailLabelToSidebarFolderId}
      getNavItemPrefs={sidebarNav.getNavItemPrefs}
      folderTree={sidebarNav.folderTree}
      labelRows={sidebarNav.labelRows}
      currentFolderId={selectedFolder}
      showLabelChip={(lab) => {
        if (LABEL_PICKER_EXCLUDE.has(lab)) return true
        return mailLabelShouldShowInListStrip(
          lab,
          sidebarNav.emailLabelToSidebarFolderId,
          sidebarNav.getNavItemPrefs,
          sidebarNav.labelRows
        )
      }}
    />
  )
}

export type { EmailListEmailViewPaneProps }
