"use client";

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

interface LoginPromptContextValue {
  isOpen: boolean;
  open: () => void;
  close: () => void;
}

const LoginPromptContext = createContext<LoginPromptContextValue>({
  isOpen: false,
  open: () => {},
  close: () => {},
});

export function LoginPromptProvider({ children }: { children: React.ReactNode }) {
  const [isOpen, setIsOpen] = useState(false);
  const open  = useCallback(() => setIsOpen(true),  []);
  const close = useCallback(() => setIsOpen(false), []);

  const value = useMemo(() => ({ isOpen, open, close }), [isOpen, open, close]);

  return (
    <LoginPromptContext.Provider value={value}>
      {children}
    </LoginPromptContext.Provider>
  );
}

export function useLoginPrompt() {
  return useContext(LoginPromptContext);
}
