import { DivWithOutsideClick, Point, Size } from "@itwin/core-react";
import * as React from "react";
import { CursorPopup } from "../cursor/cursorpopup/CursorPopup";
import { PopupManager } from "./PopupManager";
import { PositionPopup } from "./PositionPopup";
import { useEffect, useState } from "react";
import { WrapperContext } from "../configurableui/ConfigurableUiContent";
/**
 * 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(new Size(-1, -1));
    useEffect(() => {
        const newSize = Size.create(size);
        if (!size.equals(newSize)) {
            setSize(newSize);
        }
    }, [size]);
    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 (!size.equals(newSize))
            setSize(Size.create(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