import { DivWithOutsideClick } from "@itwin/core-react";
import { Point } from "@itwin/core-react/internal";
import * as React from "react";
import { CursorPopup } from "../cursor/cursorpopup/CursorPopup.js";
import { PopupManager } from "./PopupManager.js";
import { PositionPopup } from "./PositionPopup.js";
import { useState } from "react";
import { WrapperContext } from "../configurableui/ConfigurableUiContent.js";
/**
 * Displays a React Component inside a popup. The user-facing API is the {@link PopupManager} and should be used instead of this component.
 * @internal
 */
export const ComponentPopup = ({ pt, children, offset, placement, id, onCancel, anchor, }) => {
    const wrapper = React.useContext(WrapperContext);
    const [size, setSize] = useState({ width: -1, height: -1 });
    let point = PopupManager.getPopupPosition(anchor ?? wrapper, pt, new Point(), size);
    const popupRect = CursorPopup.getPopupRect(point, offset, size, placement);
    point = new Point(popupRect.left, popupRect.top);
    const handleSizeKnown = (newSize) => {
        if (newSize.height === size.height && newSize.width === size.width)
            return;
        setSize(newSize);
    };
    return (React.createElement(PositionPopup, { key: id, className: "uifw-no-border", point: point, onSizeKnown: handleSizeKnown },
        React.createElement(DivWithOutsideClick, { onOutsideClick: onCancel }, children)));
};
//# sourceMappingURL=ComponentPopup.js.map