/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Popup
 */
import "./Tooltip.scss";
import classnames from "classnames";
import * as React from "react";
import { Point, Rectangle, Size } from "@itwin/core-react";
/** Positionable tooltip component.
 * @internal
 */
export class Tooltip extends React.PureComponent {
    constructor() {
        super(...arguments);
        this._lastSize = new Size();
        this._tooltip = React.createRef();
    }
    render() {
        const className = classnames("nz-popup-tooltip", this.props.className);
        const style = {
            ...this.props.style,
            left: this.props.position.x,
            top: this.props.position.y,
        };
        return (React.createElement("div", { className: className, ref: this._tooltip, style: style },
            this.props.icon !== undefined ? (React.createElement("div", null, this.props.icon)) : undefined,
            React.createElement("div", { className: "nz-content" }, this.props.children)));
    }
    componentDidMount() {
        this.onSizeChanged();
    }
    componentDidUpdate() {
        this.onSizeChanged();
    }
    onSizeChanged() {
        const tooltip = this._tooltip.current;
        const rect = tooltip.getBoundingClientRect();
        const size = {
            height: rect.height,
            width: rect.width,
        };
        if (this._lastSize.equals(size))
            return;
        this._lastSize = Size.create(size);
        this.props.onSizeChanged && this.props.onSizeChanged(size);
    }
}
Tooltip.defaultProps = {
    position: {
        x: 0,
        y: 0,
    },
};
/** Function to apply offset and contain tooltip bounds in container.
 * @internal
 */
export const offsetAndContainInContainer = (tooltipBounds, containerSize, offset = new Point(20, 20)) => {
    let newBounds = Rectangle.create(tooltipBounds).offset(offset);
    const containerBounds = Rectangle.createFromSize(containerSize);
    if (containerBounds.contains(newBounds))
        return newBounds.topLeft();
    newBounds = newBounds.containIn(containerBounds);
    return newBounds.topLeft();
};
//# sourceMappingURL=Tooltip.js.map