/*---------------------------------------------------------------------------------------------
 * 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 * as React from "react";
import { Key } from "ts-key-enum";
import { DivWithOutsideClick } from "@itwin/core-react";
import { FocusTrap, Point } from "@itwin/core-react/internal";
import { Direction, ToolbarOpacitySetting, ToolbarPanelAlignment, } from "@itwin/components-react";
import { CursorPopup } from "../cursor/cursorpopup/CursorPopup.js";
import { PopupManager } from "./PopupManager.js";
import { PositionPopup } from "./PositionPopup.js";
import { Toolbar } from "../toolbar/Toolbar.js";
import { WrapperContext } from "../configurableui/ConfigurableUiContent.js";
import { mapToPlacement } from "../utils/Placement.js";
/** Popup component for Toolbar
 * @beta
 */
export class ToolbarPopup extends React.PureComponent {
    /** @internal */
    static contextType = WrapperContext;
    state = {
        size: { width: -1, height: -1 },
    };
    _onSizeKnown = (newSize) => {
        if (this.state.size.width === newSize.width &&
            this.state.size.height === newSize.height)
            return;
        this.setState({ size: newSize });
    };
    _handleKeyDown = (event) => {
        switch (event.key) {
            case Key.Escape.valueOf():
                this._cancel();
                break;
        }
    };
    _cancel() {
        if (this.props.onCancel) {
            this.props.onCancel();
        }
    }
    render() {
        let point = PopupManager.getPopupPosition(this.props.el ?? this.context, this.props.pt, new Point(), this.state.size);
        const popupRect = CursorPopup.getPopupRect(point, this.props.offset, this.state.size, 
        // eslint-disable-next-line @typescript-eslint/no-deprecated
        this.props.placement ?? mapToPlacement(this.props.relativePosition));
        point = new Point(popupRect.left, popupRect.top);
        return (React.createElement(PositionPopup, { key: this.props.id, className: "uifw-no-border", point: point, onSizeKnown: this._onSizeKnown },
            React.createElement(DivWithOutsideClick, { onOutsideClick: this.props.onCancel, onKeyDown: this._handleKeyDown },
                React.createElement(FocusTrap, { active: true, returnFocusOnDeactivate: true },
                    React.createElement(Toolbar, { expandsTo: Direction.Bottom, panelAlignment: ToolbarPanelAlignment.Start, items: this.props.items, useDragInteraction: true, toolbarOpacitySetting: ToolbarOpacitySetting.Defaults, onItemExecuted: this.props.onItemExecuted })))));
    }
}
//# sourceMappingURL=ToolbarPopup.js.map