"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _data = require("@wordpress/data");
var _element = require("@wordpress/element");
var _inbetween = _interopRequireDefault(require("../block-popover/inbetween"));
var _zoomOutModeInserterButton = _interopRequireDefault(require("./zoom-out-mode-inserter-button"));
var _store = require("../../store");
var _lockUnlock = require("../../lock-unlock");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

function ZoomOutModeInserters() {
  const [isReady, setIsReady] = (0, _element.useState)(false);
  const {
    hasSelection,
    blockOrder,
    setInserterIsOpened,
    sectionRootClientId,
    selectedBlockClientId
  } = (0, _data.useSelect)(select => {
    const {
      getSettings,
      getBlockOrder,
      getSelectionStart,
      getSelectedBlockClientId,
      getSectionRootClientId
    } = (0, _lockUnlock.unlock)(select(_store.store));
    const root = getSectionRootClientId();
    return {
      hasSelection: !!getSelectionStart().clientId,
      blockOrder: getBlockOrder(root),
      sectionRootClientId: root,
      setInserterIsOpened: getSettings().__experimentalSetIsInserterOpened,
      selectedBlockClientId: getSelectedBlockClientId()
    };
  }, []);
  const {
    showInsertionPoint
  } = (0, _data.useDispatch)(_store.store);

  // Defer the initial rendering to avoid the jumps due to the animation.
  (0, _element.useEffect)(() => {
    const timeout = setTimeout(() => {
      setIsReady(true);
    }, 500);
    return () => {
      clearTimeout(timeout);
    };
  }, []);
  if (!isReady || !hasSelection) {
    return null;
  }
  const previousClientId = selectedBlockClientId;
  const index = blockOrder.findIndex(clientId => selectedBlockClientId === clientId);
  const nextClientId = blockOrder[index + 1];
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_inbetween.default, {
    previousClientId: previousClientId,
    nextClientId: nextClientId,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_zoomOutModeInserterButton.default, {
      onClick: () => {
        // Hotfix for wp/6.7 where focus is not transferred to the sidebar if the
        // block library is already open.
        const blockLibrary = document.querySelector('[aria-label="Block Library"]');
        setInserterIsOpened({
          rootClientId: sectionRootClientId,
          insertionIndex: index + 1,
          tab: 'patterns',
          category: 'all'
        });
        showInsertionPoint(sectionRootClientId, index + 1, {
          operation: 'insert'
        });

        // If the block library was available before we opened it with `setInserterIsOpened`, we need to
        // send focus to the block library.
        if (blockLibrary) {
          blockLibrary.focus();
        }
      }
    })
  });
}
var _default = exports.default = ZoomOutModeInserters;
//# sourceMappingURL=zoom-out-mode-inserters.js.map