/*---------------------------------------------------------------------------------------------
 * 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 { CursorPopup } from "../cursor/cursorpopup/CursorPopup.js";
import { PopupManager } from "./PopupManager.js";
import { PositionPopup, PositionPopupContent } from "./PositionPopup.js";
import { ComponentGenerator } from "../uiprovider/ComponentGenerator.js";
import { DialogGridContainer } from "../uiprovider/DefaultDialogGridContainer.js";
/** Popup component for Tool Settings
 * @beta
 */
export class ToolSettingsPopup extends React.PureComponent {
    state = {
        size: { width: -1, height: -1 },
    };
    constructor(props) {
        super(props);
    }
    _onSizeKnown = (newSize) => {
        if (newSize.height === this.state.size.height &&
            newSize.width === this.state.size.width)
            return;
        this.setState({ size: newSize });
    };
    _handleKeyDown = (e) => {
        switch (e.key) {
            case Key.Escape.valueOf():
                if (this.props.onCancel)
                    this.props.onCancel();
                break;
        }
    };
    render() {
        const componentGenerator = new ComponentGenerator(this.props.dataProvider, this.props.onCancel);
        let point = PopupManager.getPopupPosition(this.props.el, this.props.pt, new Point(), this.state.size);
        const popupRect = CursorPopup.getPopupRect(point, this.props.offset, this.state.size, this.props.relativePosition);
        point = new Point(popupRect.left, popupRect.top);
        return (React.createElement(PositionPopup, { key: this.props.id, point: point, onSizeKnown: this._onSizeKnown },
            React.createElement(DivWithOutsideClick, { onOutsideClick: this.props.onCancel, onKeyDown: this._handleKeyDown },
                React.createElement(PositionPopupContent, null,
                    React.createElement(FocusTrap, { active: true, returnFocusOnDeactivate: true }, componentGenerator && (React.createElement(DialogGridContainer, { componentGenerator: componentGenerator })))))));
    }
}
//# sourceMappingURL=ToolSettingsPopup.js.map