import type { EmailSelection } from '@/analyst/utilities'
import type { Email, EmailThread } from '@inject/graphql/fragment-types'
import getLastEmail from './getLastEmail'
import type { ExtendedEmailThread } from './ThreadLog/typing'

const getExtendedEmailThreads = ({
  emailThreads,
  teamId,
  tab,
}: {
  emailThreads: EmailThread[]
  teamId: string
  tab: EmailSelection
}): ExtendedEmailThread[] =>
  emailThreads
    .filter(thread => !!getLastEmail(thread, teamId, tab))
    .map<ExtendedEmailThread>(thread => ({
      ...thread,
      lastEmailBiased: getLastEmail(thread, teamId, tab) as Email,
    }))

export default getExtendedEmailThreads
