"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-editor/src/components/list-view/use-block-selection.js
var use_block_selection_exports = {};
__export(use_block_selection_exports, {
  default: () => useBlockSelection
});
module.exports = __toCommonJS(use_block_selection_exports);
var import_a11y = require("@wordpress/a11y");
var import_i18n = require("@wordpress/i18n");
var import_data = require("@wordpress/data");
var import_element = require("@wordpress/element");
var import_keycodes = require("@wordpress/keycodes");
var import_blocks = require("@wordpress/blocks");
var import_store = require("../../store");
var import_utils = require("./utils");
function useBlockSelection() {
  const { clearSelectedBlock, multiSelect, selectBlock } = (0, import_data.useDispatch)(import_store.store);
  const {
    getBlockName,
    getBlockParents,
    getBlockSelectionStart,
    getSelectedBlockClientIds,
    hasMultiSelection,
    hasSelectedBlock
  } = (0, import_data.useSelect)(import_store.store);
  const { getBlockType } = (0, import_data.useSelect)(import_blocks.store);
  const updateBlockSelection = (0, import_element.useCallback)(
    async (event, clientId, destinationClientId, focusPosition) => {
      if (!event?.shiftKey && event?.keyCode !== import_keycodes.ESCAPE) {
        selectBlock(clientId, focusPosition);
        return;
      }
      event.preventDefault();
      const isOnlyDeselection = event.type === "keydown" && event.keyCode === import_keycodes.ESCAPE;
      const isKeyPress = event.type === "keydown" && (event.keyCode === import_keycodes.UP || event.keyCode === import_keycodes.DOWN || event.keyCode === import_keycodes.HOME || event.keyCode === import_keycodes.END);
      if (!isKeyPress && !hasSelectedBlock() && !hasMultiSelection()) {
        selectBlock(clientId, null);
        return;
      }
      const selectedBlocks = getSelectedBlockClientIds();
      const clientIdWithParents = [
        ...getBlockParents(clientId),
        clientId
      ];
      if (isOnlyDeselection || isKeyPress && !selectedBlocks.some(
        (blockId) => clientIdWithParents.includes(blockId)
      )) {
        await clearSelectedBlock();
      }
      if (!isOnlyDeselection) {
        let startTarget = getBlockSelectionStart();
        let endTarget = clientId;
        if (isKeyPress) {
          if (!hasSelectedBlock() && !hasMultiSelection()) {
            startTarget = clientId;
          }
          if (destinationClientId) {
            endTarget = destinationClientId;
          }
        }
        const startParents = getBlockParents(startTarget);
        const endParents = getBlockParents(endTarget);
        const { start, end } = (0, import_utils.getCommonDepthClientIds)(
          startTarget,
          endTarget,
          startParents,
          endParents
        );
        await multiSelect(start, end, null);
      }
      const updatedSelectedBlocks = getSelectedBlockClientIds();
      if ((event.keyCode === import_keycodes.HOME || event.keyCode === import_keycodes.END) && updatedSelectedBlocks.length > 1) {
        return;
      }
      const selectionDiff = selectedBlocks.filter(
        (blockId) => !updatedSelectedBlocks.includes(blockId)
      );
      let label;
      if (selectionDiff.length === 1) {
        const title = getBlockType(
          getBlockName(selectionDiff[0])
        )?.title;
        if (title) {
          label = (0, import_i18n.sprintf)(
            /* translators: %s: block name */
            (0, import_i18n.__)("%s deselected."),
            title
          );
        }
      } else if (selectionDiff.length > 1) {
        label = (0, import_i18n.sprintf)(
          /* translators: %s: number of deselected blocks */
          (0, import_i18n.__)("%s blocks deselected."),
          selectionDiff.length
        );
      }
      if (label) {
        (0, import_a11y.speak)(label, "assertive");
      }
    },
    [
      clearSelectedBlock,
      getBlockName,
      getBlockType,
      getBlockParents,
      getBlockSelectionStart,
      getSelectedBlockClientIds,
      hasMultiSelection,
      hasSelectedBlock,
      multiSelect,
      selectBlock
    ]
  );
  return {
    updateBlockSelection
  };
}
//# sourceMappingURL=use-block-selection.js.map
