import { editorCommandToPMCommand } from './editor-commands';
/**
 * Core plugin that is always included in the preset.
 * Allows for executing `EditorCommand` and other core functionality.
 */
export const corePlugin = ({
  config
}) => {
  return {
    name: 'core',
    actions: {
      execute: command => {
        const editorView = config === null || config === void 0 ? void 0 : config.getEditorView();
        if (!editorView || !command) {
          return false;
        }
        const {
          state,
          dispatch
        } = editorView;
        return editorCommandToPMCommand(command)(state, dispatch);
      },
      // Code copied from `EditorActions.focus()`
      focus: () => {
        const editorView = config === null || config === void 0 ? void 0 : config.getEditorView();
        if (!editorView || editorView.hasFocus()) {
          return false;
        }
        editorView.focus();
        editorView.dispatch(editorView.state.tr.scrollIntoView());
        return true;
      },
      // Code copied from `EditorActions.blur()`
      blur: () => {
        const editorView = config === null || config === void 0 ? void 0 : config.getEditorView();
        if (!editorView || !editorView.hasFocus()) {
          return false;
        }
        editorView.dom.blur();
        return true;
      }
    }
  };
};