"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = TextEditor;
var _components = require("@wordpress/components");
var _data = require("@wordpress/data");
var _i18n = require("@wordpress/i18n");
var _keyboardShortcuts = require("@wordpress/keyboard-shortcuts");
var _element = require("@wordpress/element");
var _blockEditor = require("@wordpress/block-editor");
var _store = require("../../store");
var _postTextEditor = _interopRequireDefault(require("../post-text-editor"));
var _postTitleRaw = _interopRequireDefault(require("../post-title/post-title-raw"));
var _lockUnlock = require("../../lock-unlock");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

function TextEditor({
  autoFocus = false
}) {
  const {
    switchEditorMode
  } = (0, _data.useDispatch)(_store.store);
  const {
    shortcut,
    isRichEditingEnabled
  } = (0, _data.useSelect)(select => {
    const {
      getEditorSettings
    } = select(_store.store);
    const {
      getShortcutRepresentation
    } = select(_keyboardShortcuts.store);
    return {
      shortcut: getShortcutRepresentation('core/editor/toggle-mode'),
      isRichEditingEnabled: getEditorSettings().richEditingEnabled
    };
  }, []);
  const {
    resetZoomLevel,
    __unstableSetEditorMode
  } = (0, _lockUnlock.unlock)((0, _data.useDispatch)(_blockEditor.store));
  const titleRef = (0, _element.useRef)();
  (0, _element.useEffect)(() => {
    resetZoomLevel();
    __unstableSetEditorMode('edit');
    if (autoFocus) {
      return;
    }
    titleRef?.current?.focus();
  }, [autoFocus, resetZoomLevel, __unstableSetEditorMode]);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: "editor-text-editor",
    children: [isRichEditingEnabled && /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "editor-text-editor__toolbar",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("h2", {
        children: (0, _i18n.__)('Editing code')
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
        __next40pxDefaultSize: true,
        variant: "tertiary",
        onClick: () => switchEditorMode('visual'),
        shortcut: shortcut,
        children: (0, _i18n.__)('Exit code editor')
      })]
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "editor-text-editor__body",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_postTitleRaw.default, {
        ref: titleRef
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_postTextEditor.default, {})]
    })]
  });
}
//# sourceMappingURL=index.js.map