"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/inserter/hooks/use-insertion-point.js
var use_insertion_point_exports = {};
__export(use_insertion_point_exports, {
  default: () => use_insertion_point_default
});
module.exports = __toCommonJS(use_insertion_point_exports);
var import_data = require("@wordpress/data");
var import_blocks = require("@wordpress/blocks");
var import_i18n = require("@wordpress/i18n");
var import_a11y = require("@wordpress/a11y");
var import_element = require("@wordpress/element");
var import_store = require("../../../store");
var import_lock_unlock = require("../../../lock-unlock");
function getIndex({
  destinationRootClientId,
  destinationIndex,
  rootClientId,
  registry
}) {
  if (rootClientId === destinationRootClientId) {
    return destinationIndex;
  }
  const parents = [
    "",
    ...registry.select(import_store.store).getBlockParents(destinationRootClientId),
    destinationRootClientId
  ];
  const parentIndex = parents.indexOf(rootClientId);
  if (parentIndex !== -1) {
    return registry.select(import_store.store).getBlockIndex(parents[parentIndex + 1]) + 1;
  }
  return registry.select(import_store.store).getBlockOrder(rootClientId).length;
}
function useInsertionPoint({
  rootClientId = "",
  insertionIndex,
  clientId,
  isAppender,
  onSelect,
  shouldFocusBlock = true,
  selectBlockOnInsert = true
}) {
  const registry = (0, import_data.useRegistry)();
  const {
    getSelectedBlock,
    getClosestAllowedInsertionPoint,
    isBlockInsertionPointVisible
  } = (0, import_lock_unlock.unlock)((0, import_data.useSelect)(import_store.store));
  const { destinationRootClientId, destinationIndex } = (0, import_data.useSelect)(
    (select) => {
      const {
        getSelectedBlockClientId,
        getBlockRootClientId,
        getBlockIndex,
        getBlockOrder,
        getInsertionPoint
      } = (0, import_lock_unlock.unlock)(select(import_store.store));
      const selectedBlockClientId = getSelectedBlockClientId();
      let _destinationRootClientId = rootClientId;
      let _destinationIndex;
      const insertionPoint = getInsertionPoint();
      if (insertionIndex !== void 0) {
        _destinationIndex = insertionIndex;
      } else if (insertionPoint && insertionPoint.hasOwnProperty("index")) {
        _destinationRootClientId = insertionPoint?.rootClientId ? insertionPoint.rootClientId : rootClientId;
        _destinationIndex = insertionPoint.index;
      } else if (clientId) {
        _destinationIndex = getBlockIndex(clientId);
      } else if (!isAppender && selectedBlockClientId) {
        _destinationRootClientId = getBlockRootClientId(
          selectedBlockClientId
        );
        _destinationIndex = getBlockIndex(selectedBlockClientId) + 1;
      } else {
        _destinationIndex = getBlockOrder(
          _destinationRootClientId
        ).length;
      }
      return {
        destinationRootClientId: _destinationRootClientId,
        destinationIndex: _destinationIndex
      };
    },
    [rootClientId, insertionIndex, clientId, isAppender]
  );
  const {
    replaceBlocks,
    insertBlocks,
    showInsertionPoint,
    hideInsertionPoint,
    setLastFocus
  } = (0, import_lock_unlock.unlock)((0, import_data.useDispatch)(import_store.store));
  const onInsertBlocks = (0, import_element.useCallback)(
    (blocks, meta, shouldForceFocusBlock = false, _rootClientId) => {
      if (shouldForceFocusBlock || shouldFocusBlock || selectBlockOnInsert) {
        setLastFocus(null);
      }
      const selectedBlock = getSelectedBlock();
      if (!isAppender && selectedBlock && (0, import_blocks.isUnmodifiedDefaultBlock)(selectedBlock, "content")) {
        replaceBlocks(
          selectedBlock.clientId,
          blocks,
          null,
          shouldFocusBlock || shouldForceFocusBlock ? 0 : null,
          meta
        );
      } else {
        insertBlocks(
          blocks,
          isAppender || _rootClientId === void 0 ? destinationIndex : getIndex({
            destinationRootClientId,
            destinationIndex,
            rootClientId: _rootClientId,
            registry
          }),
          isAppender || _rootClientId === void 0 ? destinationRootClientId : _rootClientId,
          selectBlockOnInsert,
          shouldFocusBlock || shouldForceFocusBlock ? 0 : null,
          meta
        );
      }
      const blockLength = Array.isArray(blocks) ? blocks.length : 1;
      const message = (0, import_i18n.sprintf)(
        // translators: %d: the name of the block that has been added
        (0, import_i18n._n)("%d block added.", "%d blocks added.", blockLength),
        blockLength
      );
      (0, import_a11y.speak)(message);
      if (onSelect) {
        onSelect(blocks);
      }
    },
    [
      isAppender,
      getSelectedBlock,
      replaceBlocks,
      insertBlocks,
      destinationRootClientId,
      destinationIndex,
      onSelect,
      shouldFocusBlock,
      selectBlockOnInsert
    ]
  );
  const onToggleInsertionPoint = (0, import_element.useCallback)(
    (item) => {
      if (item && !isBlockInsertionPointVisible()) {
        const allowedDestinationRootClientId = getClosestAllowedInsertionPoint(
          item.name,
          destinationRootClientId
        );
        if (allowedDestinationRootClientId !== null) {
          showInsertionPoint(
            allowedDestinationRootClientId,
            getIndex({
              destinationRootClientId,
              destinationIndex,
              rootClientId: allowedDestinationRootClientId,
              registry
            })
          );
        }
      } else {
        hideInsertionPoint();
      }
    },
    [
      getClosestAllowedInsertionPoint,
      isBlockInsertionPointVisible,
      showInsertionPoint,
      hideInsertionPoint,
      destinationRootClientId,
      destinationIndex
    ]
  );
  return [destinationRootClientId, onInsertBlocks, onToggleInsertionPoint];
}
var use_insertion_point_default = useInsertionPoint;
//# sourceMappingURL=use-insertion-point.js.map
