import { useState } from 'react'

const useToggle = (initial?: boolean) => {
  const [state, setState] = useState<boolean>(initial ?? false);

  const toggle = (e?: React.MouseEvent | null) => {
    if (e) {
      e.preventDefault();
      e.stopPropagation();
    }
    setState((prev) => !prev);
  }

  const set = (value: boolean) => {
    setState(value);
  }

  return { state, toggle, set }
}

export default useToggle