'use client'

import { createContext, useContext, useMemo } from 'react'
import type { AutomationDomain } from '@/lib/mail-automation/domains'
import { inferDomainsFromTriggers } from '@/lib/mail-automation/domains'
import type { TriggerOrGroup } from '@/lib/mail-automation/types'

const AutomationDomainContext = createContext<AutomationDomain[]>(['mail'])

export function AutomationDomainProvider({
  triggers,
  children,
}: {
  triggers: TriggerOrGroup
  children: React.ReactNode
}) {
  const domains = useMemo(() => inferDomainsFromTriggers(triggers), [triggers])
  return (
    <AutomationDomainContext.Provider value={domains}>{children}</AutomationDomainContext.Provider>
  )
}

export function useAutomationDomains(): AutomationDomain[] {
  return useContext(AutomationDomainContext)
}
