"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = useSetCommandContext;
var _data = require("@wordpress/data");
var _commands = require("@wordpress/commands");
var _blockEditor = require("@wordpress/block-editor");
var _router = require("@wordpress/router");
var _lockUnlock = require("../../lock-unlock");
var _editorCanvasContainer = require("../../components/editor-canvas-container");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  useCommandContext
} = (0, _lockUnlock.unlock)(_commands.privateApis);
const {
  useLocation
} = (0, _lockUnlock.unlock)(_router.privateApis);

/**
 * React hook used to set the correct command context based on the current state.
 */
function useSetCommandContext() {
  const {
    query = {}
  } = useLocation();
  const {
    canvas = 'view'
  } = query;
  const hasBlockSelected = (0, _data.useSelect)(select => {
    return select(_blockEditor.store).getBlockSelectionStart();
  }, []);
  const hasEditorCanvasContainer = (0, _editorCanvasContainer.useHasEditorCanvasContainer)();

  // Sets the right context for the command palette
  let commandContext = 'site-editor';
  if (canvas === 'edit') {
    commandContext = 'entity-edit';
  }
  if (hasBlockSelected) {
    commandContext = 'block-selection-edit';
  }
  if (hasEditorCanvasContainer) {
    /*
     * The editor canvas overlay will likely be deprecated in the future, so for now we clear the command context
     * to remove the suggested commands that may not make sense with Style Book or Style Revisions open.
     * See https://github.com/WordPress/gutenberg/issues/62216.
     */
    commandContext = '';
  }
  useCommandContext(commandContext);
}
//# sourceMappingURL=use-set-command-context.js.map