/*---------------------------------------------------------------------------------------------
 * 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 } from "@itwin/core-react/internal";
/** Positionable tooltip component.
 * @internal
 */
export class Tooltip extends React.PureComponent {
    static defaultProps = {
        position: {
            x: 0,
            y: 0,
        },
    };
    _lastSize = { width: 0, height: 0 };
    _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.width === size.width &&
            this._lastSize.height === size.height)
            return;
        this._lastSize = size;
        this.props.onSizeChanged && this.props.onSizeChanged(size);
    }
}
/** 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