import { useCallback, useState } from 'react';

type UseDialogStateReturn = {
  open: boolean;
  setOpen: (open: boolean) => void;
  handleOpen: () => void;
  handleClose: () => void;
  handleToggle: () => void;
};

function useDialogState(initialState: boolean = false): UseDialogStateReturn {
  const [open, setOpen] = useState(Boolean(initialState));
  const safeSetOpen = useCallback((open: boolean) => setOpen(Boolean(open)), [setOpen]);
  const handleOpen = useCallback(() => setOpen(true), [setOpen]);
  const handleClose = useCallback(() => setOpen(false), [setOpen]);
  const handleToggle = useCallback(() => setOpen((prev) => !prev), [setOpen]);

  return {
    open,
    setOpen: safeSetOpen,
    handleOpen,
    handleClose,
    handleToggle
  };
}

export { useDialogState };
