"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-editor/src/components/block-popover/index.js
var block_popover_exports = {};
__export(block_popover_exports, {
  PrivateBlockPopover: () => PrivateBlockPopover,
  default: () => block_popover_default
});
module.exports = __toCommonJS(block_popover_exports);
var import_clsx = __toESM(require("clsx"));
var import_compose = require("@wordpress/compose");
var import_components = require("@wordpress/components");
var import_element = require("@wordpress/element");
var import_use_block_refs = require("../block-list/use-block-props/use-block-refs");
var import_use_popover_scroll = __toESM(require("./use-popover-scroll"));
var import_dom = require("../../utils/dom");
var import_jsx_runtime = require("react/jsx-runtime");
var MAX_POPOVER_RECOMPUTE_COUNTER = Number.MAX_SAFE_INTEGER;
function BlockPopover({
  clientId,
  bottomClientId,
  children,
  __unstablePopoverSlot,
  __unstableContentRef,
  shift = true,
  ...props
}, ref) {
  const selectedElement = (0, import_use_block_refs.useBlockElement)(clientId);
  const lastSelectedElement = (0, import_use_block_refs.useBlockElement)(bottomClientId ?? clientId);
  const mergedRefs = (0, import_compose.useMergeRefs)([
    ref,
    (0, import_use_popover_scroll.default)(__unstableContentRef)
  ]);
  const [
    popoverDimensionsRecomputeCounter,
    forceRecomputePopoverDimensions
  ] = (0, import_element.useReducer)(
    // Module is there to make sure that the counter doesn't overflow.
    (s) => (s + 1) % MAX_POPOVER_RECOMPUTE_COUNTER,
    0
  );
  (0, import_element.useLayoutEffect)(() => {
    if (!selectedElement) {
      return;
    }
    const observer = new window.MutationObserver(
      forceRecomputePopoverDimensions
    );
    observer.observe(selectedElement, { attributes: true });
    return () => {
      observer.disconnect();
    };
  }, [selectedElement]);
  const popoverAnchor = (0, import_element.useMemo)(() => {
    if (
      // popoverDimensionsRecomputeCounter is by definition always equal or greater
      // than 0. This check is only there to satisfy the correctness of the
      // exhaustive-deps rule for the `useMemo` hook.
      popoverDimensionsRecomputeCounter < 0 || !selectedElement || bottomClientId && !lastSelectedElement
    ) {
      return void 0;
    }
    return {
      getBoundingClientRect() {
        return lastSelectedElement ? (0, import_dom.rectUnion)(
          (0, import_dom.getElementBounds)(selectedElement),
          (0, import_dom.getElementBounds)(lastSelectedElement)
        ) : (0, import_dom.getElementBounds)(selectedElement);
      },
      contextElement: selectedElement
    };
  }, [
    popoverDimensionsRecomputeCounter,
    selectedElement,
    bottomClientId,
    lastSelectedElement
  ]);
  if (!selectedElement || bottomClientId && !lastSelectedElement) {
    return null;
  }
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    import_components.Popover,
    {
      ref: mergedRefs,
      animate: false,
      focusOnMount: false,
      anchor: popoverAnchor,
      __unstableSlotName: __unstablePopoverSlot,
      inline: !__unstablePopoverSlot,
      placement: "top-start",
      resize: false,
      flip: false,
      shift,
      ...props,
      className: (0, import_clsx.default)("block-editor-block-popover", props.className),
      variant: "unstyled",
      children
    }
  );
}
var PrivateBlockPopover = (0, import_element.forwardRef)(BlockPopover);
var PublicBlockPopover = ({ clientId, bottomClientId, children, ...props }, ref) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
  PrivateBlockPopover,
  {
    ...props,
    bottomClientId,
    clientId,
    __unstableContentRef: void 0,
    __unstablePopoverSlot: void 0,
    ref,
    children
  }
);
var block_popover_default = (0, import_element.forwardRef)(PublicBlockPopover);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  PrivateBlockPopover
});
//# sourceMappingURL=index.js.map
