"use client"

import { useMemo } from "react"
import { useAuthStore } from "@/lib/api/auth-store"
import { useMailAccounts } from "@/lib/api/hooks/use-mail-queries"
import { useComposeIdentitiesStore } from "@/lib/stores/compose-identities-store"
import { collectSelfMailEmails } from "@/lib/mail-message-participants"

/** All addresses considered "me" for thread sender / recipient labeling. */
export function useSelfMailEmails(): string[] {
  const { data: accounts } = useMailAccounts()
  const platformEmail = useAuthStore((s) => s.user?.email)
  const identities = useComposeIdentitiesStore((s) => s.identities)

  return useMemo(
    () =>
      collectSelfMailEmails(
        accounts,
        identities.map((i) => ({ email: i.email })),
        platformEmail
      ),
    [accounts, identities, platformEmail]
  )
}
