"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.ToolSettingsPopup = void 0;
const React = require("react");
const ts_key_enum_1 = require("ts-key-enum");
const core_react_1 = require("@itwin/core-react");
const CursorPopup_1 = require("../cursor/cursorpopup/CursorPopup");
const PopupManager_1 = require("./PopupManager");
const PositionPopup_1 = require("./PositionPopup");
const ComponentGenerator_1 = require("../uiprovider/ComponentGenerator");
const DefaultDialogGridContainer_1 = require("../uiprovider/DefaultDialogGridContainer");
/** Popup component for Tool Settings
 * @beta
 */
class ToolSettingsPopup extends React.PureComponent {
    constructor(props) {
        super(props);
        /** @internal */
        this.state = {
            size: new core_react_1.Size(-1, -1),
        };
        this._onSizeKnown = (newSize) => {
            if (!this.state.size.equals(newSize))
                this.setState({ size: core_react_1.Size.create(newSize) });
        };
        this._handleKeyDown = (e) => {
            switch (e.key) {
                case ts_key_enum_1.Key.Escape.valueOf():
                    if (this.props.onCancel)
                        this.props.onCancel();
                    break;
            }
        };
    }
    render() {
        const componentGenerator = new ComponentGenerator_1.ComponentGenerator(this.props.dataProvider);
        let point = PopupManager_1.PopupManager.getPopupPosition(this.props.el, this.props.pt, new core_react_1.Point(), this.state.size);
        const popupRect = CursorPopup_1.CursorPopup.getPopupRect(point, this.props.offset, this.state.size, this.props.relativePosition);
        point = new core_react_1.Point(popupRect.left, popupRect.top);
        return (React.createElement(PositionPopup_1.PositionPopup, { key: this.props.id, point: point, onSizeKnown: this._onSizeKnown },
            React.createElement(core_react_1.DivWithOutsideClick, { onOutsideClick: this.props.onCancel, onKeyDown: this._handleKeyDown },
                React.createElement(PositionPopup_1.PositionPopupContent, null,
                    React.createElement(core_react_1.FocusTrap, { active: true, returnFocusOnDeactivate: true }, componentGenerator && (React.createElement(DefaultDialogGridContainer_1.DialogGridContainer, { componentGenerator: componentGenerator })))))));
    }
}
exports.ToolSettingsPopup = ToolSettingsPopup;
//# sourceMappingURL=ToolSettingsPopup.js.map