"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ComponentPopup = void 0;
const core_react_1 = require("@itwin/core-react");
const React = require("react");
const CursorPopup_1 = require("../cursor/cursorpopup/CursorPopup");
const PopupManager_1 = require("./PopupManager");
const PositionPopup_1 = require("./PositionPopup");
const react_1 = require("react");
const ConfigurableUiContent_1 = require("../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
 */
const ComponentPopup = ({ pt, children, offset, placement, id, onCancel, anchor, }) => {
    const wrapper = React.useContext(ConfigurableUiContent_1.WrapperContext);
    const [size, setSize] = (0, react_1.useState)(new core_react_1.Size(-1, -1));
    (0, react_1.useEffect)(() => {
        const newSize = core_react_1.Size.create(size);
        if (!size.equals(newSize)) {
            setSize(newSize);
        }
    }, [size]);
    let point = PopupManager_1.PopupManager.getPopupPosition(anchor ?? wrapper, pt, new core_react_1.Point(), size);
    const popupRect = CursorPopup_1.CursorPopup.getPopupRect(point, offset, size, placement);
    point = new core_react_1.Point(popupRect.left, popupRect.top);
    const handleSizeKnown = (newSize) => {
        if (!size.equals(newSize))
            setSize(core_react_1.Size.create(newSize));
    };
    return (React.createElement(PositionPopup_1.PositionPopup, { key: id, className: "uifw-no-border", point: point, onSizeKnown: handleSizeKnown },
        React.createElement(core_react_1.DivWithOutsideClick, { onOutsideClick: onCancel }, children)));
};
exports.ComponentPopup = ComponentPopup;
//# sourceMappingURL=ComponentPopup.js.map