// @flow
import { useCallback, useState, useRef } from 'react';

export const useModal = (initialShowModal?: boolean) => {
  const refToRestoreFocus = useRef<HTMLElement | null>(null);
  const [showModal, setShowModal] = useState(initialShowModal || false);

  const onModalClose = useCallback(() => {
    setShowModal(false);
  }, [setShowModal]);

  const onModalToggle = useCallback(() => {
    setShowModal(!showModal);
  }, [setShowModal, showModal]);

  return { showModal, onModalClose, onModalToggle, refToRestoreFocus };
};
