"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/utils/dom.ts
var dom_exports = {};
__export(dom_exports, {
  WITH_OVERFLOW_ELEMENT_BLOCKS: () => WITH_OVERFLOW_ELEMENT_BLOCKS,
  getBlockClientId: () => getBlockClientId,
  getElementBounds: () => getElementBounds,
  isElementVisible: () => isElementVisible,
  isInSameBlock: () => isInSameBlock,
  isInsideRootBlock: () => isInsideRootBlock,
  rectUnion: () => rectUnion
});
module.exports = __toCommonJS(dom_exports);
var BLOCK_SELECTOR = ".block-editor-block-list__block";
var APPENDER_SELECTOR = ".block-list-appender";
var BLOCK_APPENDER_CLASS = ".block-editor-button-block-appender";
function isInSameBlock(a, b) {
  return a.closest(BLOCK_SELECTOR) === b.closest(BLOCK_SELECTOR);
}
function isInsideRootBlock(blockElement, element) {
  const parentBlock = element.closest(
    [BLOCK_SELECTOR, APPENDER_SELECTOR, BLOCK_APPENDER_CLASS].join(",")
  );
  return parentBlock === blockElement;
}
function getBlockClientId(node) {
  while (node && node.nodeType !== node.ELEMENT_NODE) {
    node = node.parentNode;
  }
  if (!node) {
    return;
  }
  const elementNode = node;
  const blockNode = elementNode.closest(BLOCK_SELECTOR);
  if (!blockNode) {
    return;
  }
  return blockNode.id.slice("block-".length);
}
function rectUnion(rect1, rect2) {
  const left = Math.min(rect1.left, rect2.left);
  const right = Math.max(rect1.right, rect2.right);
  const bottom = Math.max(rect1.bottom, rect2.bottom);
  const top = Math.min(rect1.top, rect2.top);
  return new window.DOMRectReadOnly(left, top, right - left, bottom - top);
}
function isElementVisible(element) {
  const viewport = element.ownerDocument.defaultView;
  if (!viewport) {
    return false;
  }
  if (element.hasAttribute("data-visually-hidden")) {
    return false;
  }
  const bounds = element.getBoundingClientRect();
  if (bounds.width === 0 || bounds.height === 0) {
    return false;
  }
  if (element.checkVisibility) {
    return element.checkVisibility?.({
      opacityProperty: true,
      contentVisibilityAuto: true,
      visibilityProperty: true
    });
  }
  const style = viewport.getComputedStyle(element);
  if (style.display === "none" || style.visibility === "hidden" || style.opacity === "0") {
    return false;
  }
  return true;
}
function isScrollable(element) {
  const style = window.getComputedStyle(element);
  return style.overflowX === "auto" || style.overflowX === "scroll" || style.overflowY === "auto" || style.overflowY === "scroll";
}
var WITH_OVERFLOW_ELEMENT_BLOCKS = ["core/navigation"];
function getElementBounds(element) {
  const viewport = element.ownerDocument.defaultView;
  if (!viewport) {
    return new window.DOMRectReadOnly();
  }
  let bounds = element.getBoundingClientRect();
  const dataType = element.getAttribute("data-type");
  if (dataType && WITH_OVERFLOW_ELEMENT_BLOCKS.includes(dataType)) {
    const stack = [element];
    let currentElement;
    while (currentElement = stack.pop()) {
      if (!isScrollable(currentElement)) {
        for (const child of currentElement.children) {
          if (isElementVisible(child)) {
            const childBounds = child.getBoundingClientRect();
            bounds = rectUnion(bounds, childBounds);
            stack.push(child);
          }
        }
      }
    }
  }
  const left = Math.max(bounds.left, 0);
  const right = Math.min(bounds.right, viewport.innerWidth);
  bounds = new window.DOMRectReadOnly(
    left,
    bounds.top,
    right - left,
    bounds.height
  );
  return bounds;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  WITH_OVERFLOW_ELEMENT_BLOCKS,
  getBlockClientId,
  getElementBounds,
  isElementVisible,
  isInSameBlock,
  isInsideRootBlock,
  rectUnion
});
//# sourceMappingURL=dom.cjs.map
