"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/block-list/use-block-props/use-focus-first-element.js
var use_focus_first_element_exports = {};
__export(use_focus_first_element_exports, {
  useFocusFirstElement: () => useFocusFirstElement
});
module.exports = __toCommonJS(use_focus_first_element_exports);
var import_element = require("@wordpress/element");
var import_dom = require("@wordpress/dom");
var import_data = require("@wordpress/data");
var import_dom2 = require("../../../utils/dom");
var import_store = require("../../../store");
var import_lock_unlock = require("../../../lock-unlock");
function useFocusFirstElement({ clientId, initialPosition }) {
  const ref = (0, import_element.useRef)();
  const { isBlockSelected, isMultiSelecting, isZoomOut } = (0, import_lock_unlock.unlock)(
    (0, import_data.useSelect)(import_store.store)
  );
  (0, import_element.useEffect)(() => {
    if (!isBlockSelected(clientId) || isMultiSelecting() || isZoomOut()) {
      return;
    }
    if (initialPosition === void 0 || initialPosition === null) {
      return;
    }
    if (!ref.current) {
      return;
    }
    const { ownerDocument } = ref.current;
    if ((0, import_dom2.isInsideRootBlock)(ref.current, ownerDocument.activeElement)) {
      return;
    }
    const textInputs = import_dom.focus.tabbable.find(ref.current).filter((node) => (0, import_dom.isTextField)(node));
    const isReverse = -1 === initialPosition;
    const target = textInputs[isReverse ? textInputs.length - 1 : 0] || ref.current;
    if (!(0, import_dom2.isInsideRootBlock)(ref.current, target)) {
      ref.current.focus();
      return;
    }
    if (!ref.current.getAttribute("contenteditable")) {
      const focusElement = import_dom.focus.tabbable.findNext(ref.current);
      if (focusElement && (0, import_dom2.isInsideRootBlock)(ref.current, focusElement) && (0, import_dom.isFormElement)(focusElement)) {
        focusElement.focus();
        return;
      }
    }
    (0, import_dom.placeCaretAtHorizontalEdge)(target, isReverse);
  }, [initialPosition, clientId]);
  return ref;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useFocusFirstElement
});
//# sourceMappingURL=use-focus-first-element.js.map
