"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-tools/use-block-toolbar-popover-props.js
var use_block_toolbar_popover_props_exports = {};
__export(use_block_toolbar_popover_props_exports, {
  default: () => useBlockToolbarPopoverProps
});
module.exports = __toCommonJS(use_block_toolbar_popover_props_exports);
var import_compose = require("@wordpress/compose");
var import_data = require("@wordpress/data");
var import_dom = require("@wordpress/dom");
var import_element = require("@wordpress/element");
var import_store = require("../../store");
var import_use_block_refs = require("../block-list/use-block-props/use-block-refs");
var import_position = require("../../hooks/position");
var import_dom2 = require("../../utils/dom");
var COMMON_PROPS = {
  placement: "top-start"
};
var DEFAULT_PROPS = {
  ...COMMON_PROPS,
  flip: false,
  shift: true
};
var RESTRICTED_HEIGHT_PROPS = {
  ...COMMON_PROPS,
  flip: true,
  shift: false
};
function getProps(contentElement, selectedBlockElement, scrollContainer, toolbarHeight, isSticky) {
  if (!contentElement || !selectedBlockElement) {
    return DEFAULT_PROPS;
  }
  const scrollTop = scrollContainer?.scrollTop || 0;
  const blockRect = (0, import_dom2.getElementBounds)(selectedBlockElement);
  const contentRect = contentElement.getBoundingClientRect();
  const topOfContentElementInViewport = scrollTop + contentRect.top;
  const viewportHeight = contentElement.ownerDocument.documentElement.clientHeight;
  const restrictedTopArea = topOfContentElementInViewport + toolbarHeight;
  const hasSpaceForToolbarAbove = blockRect.top > restrictedTopArea;
  const isBlockTallerThanViewport = blockRect.height > viewportHeight - toolbarHeight;
  if (!isSticky && (hasSpaceForToolbarAbove || isBlockTallerThanViewport)) {
    return DEFAULT_PROPS;
  }
  return RESTRICTED_HEIGHT_PROPS;
}
function useBlockToolbarPopoverProps({
  contentElement,
  clientId
}) {
  const selectedBlockElement = (0, import_use_block_refs.useBlockElement)(clientId);
  const [toolbarHeight, setToolbarHeight] = (0, import_element.useState)(0);
  const { blockIndex, isSticky } = (0, import_data.useSelect)(
    (select) => {
      const { getBlockIndex, getBlockAttributes } = select(import_store.store);
      return {
        blockIndex: getBlockIndex(clientId),
        isSticky: (0, import_position.hasStickyOrFixedPositionValue)(
          getBlockAttributes(clientId)
        )
      };
    },
    [clientId]
  );
  const scrollContainer = (0, import_element.useMemo)(() => {
    if (!contentElement) {
      return;
    }
    return (0, import_dom.getScrollContainer)(contentElement);
  }, [contentElement]);
  const [props, setProps] = (0, import_element.useState)(
    () => getProps(
      contentElement,
      selectedBlockElement,
      scrollContainer,
      toolbarHeight,
      isSticky
    )
  );
  const popoverRef = (0, import_compose.useRefEffect)((popoverNode) => {
    setToolbarHeight(popoverNode.offsetHeight);
  }, []);
  const updateProps = (0, import_element.useCallback)(
    () => setProps(
      getProps(
        contentElement,
        selectedBlockElement,
        scrollContainer,
        toolbarHeight,
        isSticky
      )
    ),
    [contentElement, selectedBlockElement, scrollContainer, toolbarHeight]
  );
  (0, import_element.useLayoutEffect)(updateProps, [blockIndex, updateProps]);
  (0, import_element.useLayoutEffect)(() => {
    if (!contentElement || !selectedBlockElement) {
      return;
    }
    const contentView = contentElement?.ownerDocument?.defaultView;
    contentView?.addEventHandler?.("resize", updateProps);
    let resizeObserver;
    const blockView = selectedBlockElement?.ownerDocument?.defaultView;
    if (blockView.ResizeObserver) {
      resizeObserver = new blockView.ResizeObserver(updateProps);
      resizeObserver.observe(selectedBlockElement);
    }
    return () => {
      contentView?.removeEventHandler?.("resize", updateProps);
      if (resizeObserver) {
        resizeObserver.disconnect();
      }
    };
  }, [updateProps, contentElement, selectedBlockElement]);
  return {
    ...props,
    ref: popoverRef
  };
}
//# sourceMappingURL=use-block-toolbar-popover-props.js.map
