import { useCallback } from 'react';

import { useTextArea } from '../../context';

import { type UseTextAreaModalProps } from './types';

/**
 * Provides the editor instance and a close function that restores
 * focus to the editor after the modal unmounts.
 */
const useTextAreaModal = ({ onClose }: UseTextAreaModalProps) => {
  const { editor } = useTextArea();

  const close = useCallback(() => {
    onClose();
    setTimeout(() => editor?.commands.focus(), 0);
  }, [onClose, editor]);

  return { editor, close };
};

export default useTextAreaModal;
