/*---------------------------------------------------------------------------------------------
 * 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
 */
import { RelativePosition } from "@itwin/appui-abstract";
import { Div, Size } from "@itwin/core-react";
import classnames from "classnames";
import * as React from "react";
import { StatusBarDialog } from "../../statusbar/dialog/Dialog";
import "./CursorPopup.scss";
import { CursorPopupManager } from "./CursorPopupManager";
/** Enum for showing CursorPopup
 * @internal - unit testing
 */
export var CursorPopupShow;
(function (CursorPopupShow) {
    CursorPopupShow[CursorPopupShow["Open"] = 0] = "Open";
    CursorPopupShow[CursorPopupShow["FadeOut"] = 1] = "FadeOut";
})(CursorPopupShow || (CursorPopupShow = {}));
/** CursorPopup React component
 * @public
 */
export class CursorPopup extends React.Component {
    /** @internal */
    constructor(props) {
        super(props);
        this._isMounted = false;
        this._handleCursorPopupFadeOutEvent = (args) => {
            if (this.props.id === args.id) {
                if (this._isMounted)
                    this.setState({ showPopup: CursorPopupShow.FadeOut });
            }
        };
        this.state = {
            showPopup: CursorPopupShow.Open,
            size: new Size(-1, -1),
        };
    }
    componentDidMount() {
        this._isMounted = true;
        CursorPopupManager.onCursorPopupFadeOutEvent.addListener(this._handleCursorPopupFadeOutEvent);
    }
    componentWillUnmount() {
        this._isMounted = false;
        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;
        if (typeof relativePosition === "string") {
            const placement = relativePosition.split("-");
            const [mainPlacement, subPlacement] = placement;
            switch (mainPlacement) {
                case "top":
                    popupRect.left = pt.x - popupSize.width / 2 - offset.x;
                    popupRect.top = pt.y - offset.y + popupSize.height;
                    break;
                case "left":
                    popupRect.left = pt.x - popupSize.width - offset.x;
                    popupRect.top = pt.y + popupSize.height / 2 - offset.y;
                    break;
                case "right":
                    popupRect.left = pt.x + offset.x;
                    popupRect.top = pt.y + popupSize.height / 2 - offset.y;
                    break;
                case "bottom":
                    popupRect.left = pt.x - popupSize.width / 2 - offset.x;
                    popupRect.top = pt.y + offset.y;
                    break;
            }
            if (subPlacement === "start") {
                switch (mainPlacement) {
                    case "top":
                    case "bottom":
                        popupRect.left = pt.x - offset.x;
                        break;
                    case "left":
                    case "right":
                        popupRect.top = pt.y - offset.y;
                        break;
                }
            }
            else if (subPlacement === "end") {
                switch (mainPlacement) {
                    case "top":
                    case "bottom":
                        popupRect.left = pt.x - popupSize.width - offset.x;
                        break;
                    case "left":
                    case "right":
                        popupRect.top = popupSize.height + pt.y - offset.y;
                        break;
                }
            }
            popupRect.right = popupRect.left + popupSize.width;
            popupRect.bottom = popupRect.top + popupSize.height;
        }
        else {
            switch (relativePosition) {
                case 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 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 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 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 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 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 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 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) {
        if (div) {
            const rect = div.getBoundingClientRect();
            const newSize = new Size(rect.width, rect.height);
            if (!this.state.size.equals(newSize)) {
                if (this.props.onSizeKnown)
                    this.props.onSizeKnown(newSize);
                if (this._isMounted)
                    this.setState({ size: newSize });
            }
        }
    }
    /** @internal */
    render() {
        const popupRect = CursorPopup.getPopupRect(this.props.pt, this.props.offset, this.state.size, 
        // eslint-disable-next-line deprecation/deprecation
        this.props.placement || this.props.relativePosition);
        const positioningStyle = {
            left: popupRect.left,
            top: popupRect.top,
        };
        const classNames = classnames("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(StatusBarDialog.TitleBar, { title: this.props.title, className: "uifw-cursorpopup-title" })),
            this.props.content));
    }
}
/** @internal */
CursorPopup.fadeOutTime = 500;
/** CursorPopup content with padding
 * @public
 */
// eslint-disable-next-line deprecation/deprecation
export function CursorPopupContent(props) {
    // eslint-disable-next-line deprecation/deprecation
    return React.createElement(Div, { ...props, mainClassName: "uifw-cursorpopup-content" });
}
//# sourceMappingURL=CursorPopup.js.map