import { createContext, useCallback, useContext, useState, type ReactNode } from "react";

type Ctx = { open: boolean; openLead: (source?: string) => void; closeLead: () => void; source: string };
const LeadContext = createContext<Ctx>({ open: false, openLead: () => {}, closeLead: () => {}, source: "" });

export function LeadProvider({ children }: { children: ReactNode }) {
  const [open, setOpen] = useState(false);
  const [source, setSource] = useState("CTA");
  const openLead = useCallback((s = "CTA") => { setSource(s); setOpen(true); }, []);
  const closeLead = useCallback(() => setOpen(false), []);
  return (
    <LeadContext.Provider value={{ open, openLead, closeLead, source }}>{children}</LeadContext.Provider>
  );
}

export const useLead = () => useContext(LeadContext);
