"use client";

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

export type DoomOverlayContextValue = {
  isOpen: boolean;
  open: () => void;
  close: () => void;
};

const DoomOverlayContext = createContext<DoomOverlayContextValue | undefined>(
  undefined
);

interface DoomOverlayProviderProps {
  children: ReactNode;
}

export function DoomOverlayProvider({ children }: DoomOverlayProviderProps) {
  const [isOpen, setIsOpen] = useState(false);

  const open = () => {
    try {
      console.log("[DoomOverlay] open");
    } catch {}
    setIsOpen(true);
  };
  const close = () => {
    try {
      console.log("[DoomOverlay] close");
    } catch {}
    setIsOpen(false);
  };

  const value: DoomOverlayContextValue = {
    isOpen,
    open,
    close,
  };

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

export function useDoomOverlay(): DoomOverlayContextValue {
  const context = useContext(DoomOverlayContext);
  if (context === undefined) {
    throw new Error("useDoomOverlay must be used within a DoomOverlayProvider");
  }
  return context;
}
