/**
 * WordPress dependencies
 */
import { Button } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
import { useEffect, useRef } from '@wordpress/element';
import { store as blockEditorStore } from '@wordpress/block-editor';

/**
 * Internal dependencies
 */
import { store as editorStore } from '../../store';
import PostTextEditor from '../post-text-editor';
import PostTitleRaw from '../post-title/post-title-raw';
import { unlock } from '../../lock-unlock';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
export default function TextEditor({
  autoFocus = false
}) {
  const {
    switchEditorMode
  } = useDispatch(editorStore);
  const {
    shortcut,
    isRichEditingEnabled
  } = useSelect(select => {
    const {
      getEditorSettings
    } = select(editorStore);
    const {
      getShortcutRepresentation
    } = select(keyboardShortcutsStore);
    return {
      shortcut: getShortcutRepresentation('core/editor/toggle-mode'),
      isRichEditingEnabled: getEditorSettings().richEditingEnabled
    };
  }, []);
  const {
    resetZoomLevel,
    __unstableSetEditorMode
  } = unlock(useDispatch(blockEditorStore));
  const titleRef = useRef();
  useEffect(() => {
    resetZoomLevel();
    __unstableSetEditorMode('edit');
    if (autoFocus) {
      return;
    }
    titleRef?.current?.focus();
  }, [autoFocus, resetZoomLevel, __unstableSetEditorMode]);
  return /*#__PURE__*/_jsxs("div", {
    className: "editor-text-editor",
    children: [isRichEditingEnabled && /*#__PURE__*/_jsxs("div", {
      className: "editor-text-editor__toolbar",
      children: [/*#__PURE__*/_jsx("h2", {
        children: __('Editing code')
      }), /*#__PURE__*/_jsx(Button, {
        __next40pxDefaultSize: true,
        variant: "tertiary",
        onClick: () => switchEditorMode('visual'),
        shortcut: shortcut,
        children: __('Exit code editor')
      })]
    }), /*#__PURE__*/_jsxs("div", {
      className: "editor-text-editor__body",
      children: [/*#__PURE__*/_jsx(PostTitleRaw, {
        ref: titleRef
      }), /*#__PURE__*/_jsx(PostTextEditor, {})]
    })]
  });
}
//# sourceMappingURL=index.js.map