"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 Popup
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.offsetAndContainInContainer = exports.Tooltip = void 0;
require("./Tooltip.scss");
const classnames_1 = require("classnames");
const React = require("react");
const core_react_1 = require("@itwin/core-react");
/** Positionable tooltip component.
 * @internal
 */
class Tooltip extends React.PureComponent {
    constructor() {
        super(...arguments);
        this._lastSize = new core_react_1.Size();
        this._tooltip = React.createRef();
    }
    render() {
        const className = (0, classnames_1.default)("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 = core_react_1.Size.create(size);
        this.props.onSizeChanged && this.props.onSizeChanged(size);
    }
}
exports.Tooltip = Tooltip;
Tooltip.defaultProps = {
    position: {
        x: 0,
        y: 0,
    },
};
/** Function to apply offset and contain tooltip bounds in container.
 * @internal
 */
const offsetAndContainInContainer = (tooltipBounds, containerSize, offset = new core_react_1.Point(20, 20)) => {
    let newBounds = core_react_1.Rectangle.create(tooltipBounds).offset(offset);
    const containerBounds = core_react_1.Rectangle.createFromSize(containerSize);
    if (containerBounds.contains(newBounds))
        return newBounds.topLeft();
    newBounds = newBounds.containIn(containerBounds);
    return newBounds.topLeft();
};
exports.offsetAndContainInContainer = offsetAndContainInContainer;
//# sourceMappingURL=Tooltip.js.map