"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.KeyinPalettePopup = void 0;
const React = require("react");
const ts_key_enum_1 = require("ts-key-enum");
const core_react_1 = require("@itwin/core-react");
const PositionPopup_1 = require("./PositionPopup");
const KeyinPalettePanel_1 = require("./KeyinPalettePanel");
/** Keyin Palette Popup Component
 * @public
 */
function KeyinPalettePopup({ 
// eslint-disable-next-line deprecation/deprecation
el, id, keyins, onCancel, onItemExecuted, anchorEl, }) {
    // due to deprecation policy, the context won't be used as fallback
    // until we replace `el` with the optional `anchorEl` property.
    // const wrapper = React.useContext(WrapperContext);
    const _el = anchorEl ?? el;
    const [popupSize, setPopupSize] = React.useState(new core_react_1.Size(-1, -1));
    const onSizeKnown = React.useCallback((newSize) => {
        if (!popupSize.equals(newSize))
            setPopupSize(core_react_1.Size.create(newSize));
    }, [popupSize]);
    const cancel = React.useCallback(() => {
        onCancel && onCancel();
    }, [onCancel]);
    const handleKeyDown = React.useCallback((event) => {
        switch (event.key) {
            case ts_key_enum_1.Key.Escape.valueOf():
                cancel();
                event.preventDefault();
                break;
        }
    }, [cancel]);
    const xMid = _el.getBoundingClientRect().left + _el.getBoundingClientRect().width / 2;
    let point = new core_react_1.Point(xMid, _el.getBoundingClientRect().top);
    if (popupSize.width > 0)
        point = point.offsetX(popupSize.width / -2);
    return (React.createElement(PositionPopup_1.PositionPopup, { key: id, className: "uifw-command-palette-popup-container", point: point, onSizeKnown: onSizeKnown },
        React.createElement(core_react_1.DivWithOutsideClick, { onOutsideClick: onCancel, onKeyDown: handleKeyDown },
            React.createElement(core_react_1.FocusTrap, { active: true, returnFocusOnDeactivate: true },
                React.createElement(KeyinPalettePanel_1.KeyinPalettePanel, { keyins: keyins, onKeyinExecuted: onItemExecuted })))));
}
exports.KeyinPalettePopup = KeyinPalettePopup;
//# sourceMappingURL=KeyinPalettePopup.js.map