"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 Cursor
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.CursorPopupContent = exports.CursorPopup = exports.CursorPopupShow = void 0;
require("./CursorPopup.scss");
const React = require("react");
const classnames_1 = require("classnames");
const appui_abstract_1 = require("@itwin/appui-abstract");
const core_react_1 = require("@itwin/core-react");
const appui_layout_react_1 = require("@itwin/appui-layout-react");
const CursorPopupManager_1 = require("./CursorPopupManager");
/** Enum for showing CursorPopup
 * @internal - unit testing
 */
var CursorPopupShow;
(function (CursorPopupShow) {
    CursorPopupShow[CursorPopupShow["Open"] = 0] = "Open";
    CursorPopupShow[CursorPopupShow["FadeOut"] = 1] = "FadeOut";
})(CursorPopupShow = exports.CursorPopupShow || (exports.CursorPopupShow = {}));
/** CursorPopup React component
 * @public
 */
class CursorPopup extends React.Component {
    /** @internal */
    constructor(props) {
        super(props);
        this._isMounted = false;
        this._handleCursorPopupFadeOutEvent = (args) => {
            if (this.props.id === args.id) {
                // istanbul ignore else
                if (this._isMounted)
                    this.setState({ showPopup: CursorPopupShow.FadeOut });
            }
        };
        this.state = {
            showPopup: CursorPopupShow.Open,
            size: new core_react_1.Size(-1, -1),
        };
    }
    componentDidMount() {
        this._isMounted = true;
        CursorPopupManager_1.CursorPopupManager.onCursorPopupFadeOutEvent.addListener(this._handleCursorPopupFadeOutEvent);
    }
    componentWillUnmount() {
        this._isMounted = false;
        CursorPopupManager_1.CursorPopupManager.onCursorPopupFadeOutEvent.removeListener(this._handleCursorPopupFadeOutEvent);
    }
    /** @internal */
    static getPopupRect(pt, offset, popupSize, relativePosition) {
        const popupRect = { top: 0, left: 0, right: 0, bottom: 0 };
        if (popupSize === undefined)
            return popupRect;
        switch (relativePosition) {
            case appui_abstract_1.RelativePosition.Top:
                popupRect.bottom = pt.y - offset.y;
                popupRect.left = pt.x - popupSize.width / 2;
                popupRect.top = popupRect.bottom - popupSize.height;
                popupRect.right = popupRect.left + popupSize.width;
                break;
            case appui_abstract_1.RelativePosition.Left:
                popupRect.right = pt.x - offset.x;
                popupRect.top = pt.y - popupSize.height / 2;
                popupRect.left = popupRect.right - popupSize.width;
                popupRect.bottom = popupRect.top + popupSize.height;
                break;
            case appui_abstract_1.RelativePosition.Right:
                popupRect.left = pt.x + offset.x;
                popupRect.top = pt.y - popupSize.height / 2;
                popupRect.right = popupRect.left + popupSize.width;
                popupRect.bottom = popupRect.top + popupSize.height;
                break;
            case appui_abstract_1.RelativePosition.Bottom:
                popupRect.top = pt.y + offset.y;
                popupRect.left = pt.x - popupSize.width / 2;
                popupRect.bottom = popupRect.top + popupSize.height;
                popupRect.right = popupRect.left + popupSize.width;
                break;
            case appui_abstract_1.RelativePosition.TopLeft:
                popupRect.bottom = pt.y - offset.y;
                popupRect.right = pt.x - offset.x;
                popupRect.top = popupRect.bottom - popupSize.height;
                popupRect.left = popupRect.right - popupSize.width;
                break;
            case appui_abstract_1.RelativePosition.TopRight:
                popupRect.bottom = pt.y - offset.y;
                popupRect.left = pt.x + offset.x;
                popupRect.top = popupRect.bottom - popupSize.height;
                popupRect.right = popupRect.left + popupSize.width;
                break;
            case appui_abstract_1.RelativePosition.BottomLeft:
                popupRect.top = pt.y + offset.y;
                popupRect.right = pt.x - offset.x;
                popupRect.bottom = popupRect.top + popupSize.height;
                popupRect.left = popupRect.right - popupSize.width;
                break;
            case appui_abstract_1.RelativePosition.BottomRight:
                popupRect.top = pt.y + offset.y;
                popupRect.left = pt.x + offset.x;
                popupRect.bottom = popupRect.top + popupSize.height;
                popupRect.right = popupRect.left + popupSize.width;
                break;
        }
        return popupRect;
    }
    setDivRef(div) {
        // istanbul ignore else
        if (div) {
            const rect = div.getBoundingClientRect();
            const newSize = new core_react_1.Size(rect.width, rect.height);
            // istanbul ignore else
            if (!this.state.size.equals(newSize)) {
                // istanbul ignore else
                if (this.props.onSizeKnown)
                    this.props.onSizeKnown(newSize);
                // istanbul ignore else
                if (this._isMounted)
                    this.setState({ size: newSize });
            }
        }
    }
    /** @internal */
    render() {
        const popupRect = CursorPopup.getPopupRect(this.props.pt, this.props.offset, this.state.size, this.props.relativePosition);
        const positioningStyle = {
            left: popupRect.left,
            top: popupRect.top,
        };
        const classNames = (0, classnames_1.default)("uifw-cursorpopup", this.props.shadow && "core-popup-shadow", this.state.showPopup === CursorPopupShow.FadeOut &&
            "uifw-cursorpopup-fadeOut");
        return (React.createElement("div", { className: classNames, ref: (e) => this.setDivRef(e), style: positioningStyle },
            this.props.title && (React.createElement(appui_layout_react_1.TitleBar, { title: this.props.title, className: "uifw-cursorpopup-title" })),
            this.props.content));
    }
}
/** @internal */
CursorPopup.fadeOutTime = 500;
exports.CursorPopup = CursorPopup;
/** CursorPopup content with padding
 * @public
 */
function CursorPopupContent(props) {
    return React.createElement(core_react_1.Div, { ...props, mainClassName: "uifw-cursorpopup-content" });
}
exports.CursorPopupContent = CursorPopupContent;
//# sourceMappingURL=CursorPopup.js.map