text: "use strict";
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.default = void 0;
  
  var _portableTextEditor = require("@sanity/portable-text-editor");
  
  var _classnames = _interopRequireDefault(require("classnames"));
  
  var _react = _interopRequireWildcard(require("react"));
  
  var _paths = require("@sanity/util/paths");
  
  var _initialValueTemplates = require("@sanity/initial-value-templates");
  
  var _ui = require("@sanity/ui");
  
  var _ActionMenu = _interopRequireDefault(require("./ActionMenu"));
  
  var _BlockStyleSelect = _interopRequireDefault(require("./BlockStyleSelect"));
  
  var _InsertMenu = _interopRequireDefault(require("./InsertMenu"));
  
  var _helpers = require("./helpers");
  
  var _Toolbar = _interopRequireDefault(require("./Toolbar.css"));
  
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  
  function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
  
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
  
  function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
  
  var SLOW_INITIAL_VALUE_LIMIT = 300;
  
  function PTEToolbar(props) {
    var hotkeys = props.hotkeys,
        isFullscreen = props.isFullscreen,
        readOnly = props.readOnly,
        onFocus = props.onFocus,
        renderBlock = props.renderBlock;
    var editor = (0, _portableTextEditor.usePortableTextEditor)();
    var selection = (0, _portableTextEditor.usePortableTextEditorSelection)();
    var disabled = !selection;
    var toast = (0, _ui.useToast)();
    var resolveInitialValue = (0, _react.useCallback)(type => {
      var isSlow = false;
      var slowTimer = setTimeout(() => {
        isSlow = true;
        toast.push({
          id: 'resolving-initial-value',
          status: 'info',
          title: 'Resolving initial value…'
        });
      }, SLOW_INITIAL_VALUE_LIMIT);
      return (0, _initialValueTemplates.resolveInitialValueForType)(type).then(value => {
        if (isSlow) {
          // I found no way to close an existing toast, so this will replace the message in the
          // "Resolving initial value…"-toast and then make sure it gets closed.
          toast.push({
            id: 'resolving-initial-value',
            status: 'info',
            duration: 500,
            title: 'Initial value resolved'
          });
        }
  
        return value;
      }).catch(error => {
        toast.push({
          title: "Could not resolve initial value",
          id: 'resolving-initial-value',
          description: "Unable to resolve initial value for type: ".concat(type.name, ": ").concat(error.message, "."),
          status: 'error'
        });
        return undefined;
      }).finally(() => clearTimeout(slowTimer));
    }, [toast]);
    var handleInsertBlock = (0, _react.useCallback)( /*#__PURE__*/function () {
      var _ref = _asyncToGenerator(function* (type) {
        var initialValue = yield resolveInitialValue(type);
  
        var path = _portableTextEditor.PortableTextEditor.insertBlock(editor, type, initialValue);
  
        setTimeout(() => onFocus(path.concat(_paths.FOCUS_TERMINATOR)), 0);
      });
  
      return function (_x) {
        return _ref.apply(this, arguments);
      };
    }(), [editor, onFocus, resolveInitialValue]);
    var handleInsertInline = (0, _react.useCallback)( /*#__PURE__*/function () {
      var _ref2 = _asyncToGenerator(function* (type) {
        var initialValue = yield resolveInitialValue(type);
  
        var path = _portableTextEditor.PortableTextEditor.insertChild(editor, type, initialValue);
  
        setTimeout(() => onFocus(path.concat(_paths.FOCUS_TERMINATOR)), 0);
      });
  
      return function (_x2) {
        return _ref2.apply(this, arguments);
      };
    }(), [editor, onFocus, resolveInitialValue]);
    var handleInsertAnnotation = (0, _react.useCallback)( /*#__PURE__*/function () {
      var _ref3 = _asyncToGenerator(function* (type) {
        var initialValue = yield resolveInitialValue(type);
  
        var paths = _portableTextEditor.PortableTextEditor.addAnnotation(editor, type, initialValue);
  
        if (paths && paths.markDefPath) {
          onFocus(paths.markDefPath.concat(_paths.FOCUS_TERMINATOR));
        }
      });
  
      return function (_x3) {
        return _ref3.apply(this, arguments);
      };
    }(), [editor, onFocus, resolveInitialValue]);
  
    var actionGroups = _react.default.useMemo(() => editor ? (0, _helpers.getPTEToolbarActionGroups)(editor, selection, handleInsertAnnotation, hotkeys) : [], [editor, selection, handleInsertAnnotation, hotkeys]);
  
    var actionsLen = actionGroups.reduce((acc, x) => acc + x.actions.length, 0);
    var blockStyleSelectProps = editor ? (0, _helpers.getBlockStyleSelectProps)(editor) : null;
  
    var insertMenuItems = _react.default.useMemo(() => editor ? (0, _helpers.getInsertMenuItems)(editor, selection, handleInsertBlock, handleInsertInline) : [], [editor, handleInsertBlock, handleInsertInline, selection]);
  
    return /*#__PURE__*/_react.default.createElement("div", {
      className: (0, _classnames.default)(_Toolbar.default.root, isFullscreen && _Toolbar.default.fullscreen) // Ensure the editor doesn't lose focus when interacting
      // with the toolbar (prevent focus click events)
      ,
      onMouseDown: event => event.preventDefault(),
      onKeyPress: event => event.preventDefault()
    }, blockStyleSelectProps && blockStyleSelectProps.items.length > 1 && /*#__PURE__*/_react.default.createElement("div", {
      className: _Toolbar.default.blockStyleSelectContainer
    }, /*#__PURE__*/_react.default.createElement(_BlockStyleSelect.default, _extends({}, blockStyleSelectProps, {
      className: _Toolbar.default.blockStyleSelect,
      disabled: disabled,
      padding: "small",
      selection: selection,
      readOnly: readOnly,
      renderBlock: renderBlock
    }))), actionsLen > 0 && /*#__PURE__*/_react.default.createElement("div", {
      className: _Toolbar.default.actionMenuContainer
    }, /*#__PURE__*/_react.default.createElement(_ActionMenu.default, {
      disabled: disabled,
      groups: actionGroups,
      readOnly: readOnly
    })), insertMenuItems.length > 0 && /*#__PURE__*/_react.default.createElement("div", {
      className: _Toolbar.default.insertMenuContainer
    }, /*#__PURE__*/_react.default.createElement(_InsertMenu.default, {
      disabled: disabled,
      items: insertMenuItems,
      readOnly: readOnly
    })));
  }
  
  var _default = PTEToolbar;
  exports.default = _default;