/*---------------------------------------------------------------------------------------------
 * 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 { SpecialKey } from "@itwin/appui-abstract";
import { DivWithOutsideClick, FocusTrap, Point, Size } from "@itwin/core-react";
import { CursorPopup } from "../cursor/cursorpopup/CursorPopup";
import { PopupManager } from "./PopupManager";
import { PositionPopup, PositionPopupContent } from "./PositionPopup";
import { ComponentGenerator } from "../uiprovider/ComponentGenerator";
import { DialogGridContainer } from "../uiprovider/DefaultDialogGridContainer";
/** Popup component for Tool Settings
 * @beta
 */
export class ToolSettingsPopup extends React.PureComponent {
    constructor(props) {
        super(props);
        /** @internal */
        this.state = {
            size: new Size(-1, -1),
        };
        this._onSizeKnown = (newSize) => {
            // istanbul ignore else
            if (!this.state.size.equals(newSize))
                this.setState({ size: Size.create(newSize) });
        };
        this._handleKeyDown = (e) => {
            switch (e.key) {
                case SpecialKey.Escape:
                    // istanbul ignore else
                    if (this.props.onCancel)
                        this.props.onCancel();
                    break;
            }
        };
    }
    render() {
        const componentGenerator = new ComponentGenerator(this.props.dataProvider);
        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