"use strict";
/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Notification
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.ElementTooltip = exports.ElementTooltipChangedEvent = void 0;
require("./ElementTooltip.scss");
const classnames_1 = require("classnames");
const React = require("react");
const ReactDOM = require("react-dom");
const appui_abstract_1 = require("@itwin/appui-abstract");
const core_bentley_1 = require("@itwin/core-bentley");
const core_react_1 = require("@itwin/core-react");
const Tooltip_1 = require("../layout/popup/Tooltip");
/** ElementTooltip Changed Event class.
 * @public
 * @deprecated in 4.13.0. This class should not be used by applications to instantiate objects.
 */
class ElementTooltipChangedEvent extends appui_abstract_1.UiEvent {
}
exports.ElementTooltipChangedEvent = ElementTooltipChangedEvent;
/** ElementTooltip React component.
 * @public
 */
class ElementTooltip extends React.Component {
    static get onElementTooltipChangedEvent() {
        return ElementTooltip._elementTooltipChangedEvent;
    }
    static get isTooltipVisible() {
        return ElementTooltip._isTooltipVisible;
    }
    static showTooltip(el, message, pt, options) {
        if (ElementTooltip._isTooltipHalted)
            return;
        ElementTooltip._isTooltipVisible = true;
        ElementTooltip.onElementTooltipChangedEvent.emit({
            isTooltipVisible: true,
            el,
            message,
            pt,
            options,
        });
    }
    static hideTooltip() {
        ElementTooltip._isTooltipVisible = false;
        ElementTooltip.onElementTooltipChangedEvent.emit({
            isTooltipVisible: false,
            message: "",
        });
    }
    static get isTooltipHalted() {
        return ElementTooltip._isTooltipHalted;
    }
    static set isTooltipHalted(halt) {
        ElementTooltip._isTooltipHalted = halt;
        if (halt && ElementTooltip._isTooltipVisible)
            ElementTooltip.hideTooltip();
    }
    render() {
        return React.createElement(ElementTooltipComponent, null);
    }
}
exports.ElementTooltip = ElementTooltip;
ElementTooltip._elementTooltipChangedEvent = new core_bentley_1.BeUiEvent();
function ElementTooltipComponent(props) {
    const [visible, setVisible] = React.useState(false);
    const [message, setMessage] = React.useState("");
    const [preferredPosition, setPreferredPosition] = React.useState(undefined);
    const [position, setPosition] = React.useState({ x: 0, y: 0 });
    // Use a dedicated container for the tooltip to avoid z-index issues in the main window.
    const [mainContainer, setMainContainer] = React.useState(undefined);
    const [container, setContainer] = React.useState(undefined);
    const [popout, setPopout] = React.useState(false);
    const [size, setSize] = React.useState(undefined);
    React.useEffect(() => {
        return ElementTooltip.onElementTooltipChangedEvent.addListener((args) => {
            const isPopout = args.el?.ownerDocument.defaultView !== window;
            setPopout(isPopout);
            setVisible(args.isTooltipVisible);
            setMessage(args.message);
            setContainer(args.el);
            setPreferredPosition(args.pt);
        });
    }, []);
    const portalContainer = popout ? container : mainContainer;
    React.useLayoutEffect(() => {
        if (!preferredPosition)
            return;
        // Use preferred position by default.
        setPosition(preferredPosition);
        if (!container)
            return;
        if (!size)
            return;
        // Contain the tooltip based on the container bounds.
        const containerBounds = core_react_1.Rectangle.create(container.getBoundingClientRect());
        const bounds = core_react_1.Rectangle.createFromSize(size).offset(preferredPosition);
        const adjustedPosition = (0, Tooltip_1.offsetAndContainInContainer)(bounds, containerBounds.getSize(), { x: 8, y: 8 });
        const newPosition = adjustedPosition.offset(containerBounds.topLeft());
        setPosition(newPosition);
    }, [container, size, preferredPosition]);
    return (React.createElement(React.Fragment, null,
        React.createElement("div", { className: (0, classnames_1.default)("uifw-feedback-elementTooltip_container", props.className), ref: (el) => setMainContainer(el ?? undefined) }),
        portalContainer &&
            visible &&
            ReactDOM.createPortal(React.createElement(Tooltip_1.Tooltip, { className: (0, classnames_1.default)("uifw-feedback-elementTooltip", props.className), style: props.style, onSizeChanged: setSize, position: position },
                React.createElement(core_react_1.MessageRenderer, { message: message })), portalContainer)));
}
//# sourceMappingURL=ElementTooltip.js.map