"use client" import { createContext, useContext, useMemo, type ReactNode, } from "react" import type { Email } from "@/lib/email-data" import { useScheduledStore, type ScheduleSendPayload, } from "@/lib/stores/scheduled-store" export type { ScheduleSendPayload } from "@/lib/stores/scheduled-store" type ScheduledMailContextValue = { scheduledEmails: Email[] snoozedEmails: Email[] sentPlaceholderEmails: Email[] refreshAll: () => Promise scheduleSend: (payload: ScheduleSendPayload) => Promise<{ id: string }> removeScheduledLocal: (id: string) => void requestDeleteScheduled: (id: string) => Promise requestArchiveScheduled: (id: string) => Promise requestSnoozeScheduled: (id: string) => Promise requestToggleReadScheduled: (id: string, read: boolean) => Promise requestRescheduleScheduled: (id: string, sendAtIso: string) => Promise requestGetScheduledEditPayload: (id: string) => Promise requestUpdateScheduledSend: (id: string, payload: ScheduleSendPayload) => Promise requestSendScheduledNow: (id: string) => Promise } const ScheduledMailContext = createContext(null) const noop = async () => {} export function ScheduledMailProvider({ children }: { children: ReactNode }) { const scheduledEmails = useScheduledStore((s) => s.scheduledEmails) const snoozedEmails = useScheduledStore((s) => s.snoozedEmails) const sentPlaceholderEmails = useScheduledStore((s) => s.sentPlaceholderEmails) const value = useMemo(() => { const actions = useScheduledStore.getState() return { scheduledEmails, snoozedEmails, sentPlaceholderEmails, refreshAll: noop, scheduleSend: async (payload) => actions.createScheduledSend(payload), removeScheduledLocal: (id) => actions.removeScheduledLocal(id), requestDeleteScheduled: async (id) => { actions.deleteScheduledSend(id) }, requestArchiveScheduled: async (id) => { actions.archiveScheduledSend(id) }, requestSnoozeScheduled: async (id) => { actions.snoozeScheduledSend(id) }, requestToggleReadScheduled: async (id, read) => { actions.markScheduledReadState(id, read) }, requestRescheduleScheduled: async (id, sendAtIso) => { actions.rescheduleScheduledSend(id, sendAtIso) }, requestGetScheduledEditPayload: async (id) => actions.getScheduledEditPayload(id), requestUpdateScheduledSend: async (id, payload) => { actions.updateScheduledSend(id, payload) }, requestSendScheduledNow: async (id) => { actions.sendScheduledNow(id) }, } }, [scheduledEmails, snoozedEmails, sentPlaceholderEmails]) return ( {children} ) } export function useScheduledMail() { const ctx = useContext(ScheduledMailContext) if (!ctx) { throw new Error("useScheduledMail must be used within ScheduledMailProvider") } return ctx }