"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = useSelectAll;
var _dom = require("@wordpress/dom");
var _data = require("@wordpress/data");
var _keyboardShortcuts = require("@wordpress/keyboard-shortcuts");
var _compose = require("@wordpress/compose");
var _store = require("../../store");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

function useSelectAll() {
  const {
    getBlockOrder,
    getSelectedBlockClientIds,
    getBlockRootClientId
  } = (0, _data.useSelect)(_store.store);
  const {
    multiSelect,
    selectBlock
  } = (0, _data.useDispatch)(_store.store);
  const isMatch = (0, _keyboardShortcuts.__unstableUseShortcutEventMatch)();
  return (0, _compose.useRefEffect)(node => {
    function onKeyDown(event) {
      if (!isMatch('core/block-editor/select-all', event)) {
        return;
      }
      const selectedClientIds = getSelectedBlockClientIds();
      if (selectedClientIds.length < 2 && !(0, _dom.isEntirelySelected)(event.target)) {
        return;
      }
      event.preventDefault();
      const [firstSelectedClientId] = selectedClientIds;
      const rootClientId = getBlockRootClientId(firstSelectedClientId);
      const blockClientIds = getBlockOrder(rootClientId);

      // If we have selected all sibling nested blocks, try selecting up a
      // level. See: https://github.com/WordPress/gutenberg/pull/31859/
      if (selectedClientIds.length === blockClientIds.length) {
        if (rootClientId) {
          node.ownerDocument.defaultView.getSelection().removeAllRanges();
          selectBlock(rootClientId);
        }
        return;
      }
      multiSelect(blockClientIds[0], blockClientIds[blockClientIds.length - 1]);
    }
    node.addEventListener('keydown', onKeyDown);
    return () => {
      node.removeEventListener('keydown', onKeyDown);
    };
  }, []);
}
//# sourceMappingURL=use-select-all.js.map