"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 AccuDraw
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.MenuButton = void 0;
require("./MenuButton.scss");
const React = require("react");
const core_react_1 = require("@itwin/core-react");
const SquareButton_1 = require("./SquareButton");
const itwinui_icons_react_1 = require("@itwin/itwinui-icons-react");
/** @public */
class MenuButton extends React.PureComponent {
    constructor(props) {
        super(props);
        // eslint-disable-next-line deprecation/deprecation
        this._menu = null;
        this._open = () => {
            this.setState({ expanded: true }, () => {
                if (this._menu)
                    this._menu.focus();
            });
        };
        this._handleClick = (event) => {
            if (this.state.expanded) {
                event.stopPropagation();
                this.setState({ expanded: false });
            }
            else {
                this._open();
            }
        };
        this._handleClose = () => {
            this.setState({ expanded: false });
        };
        this.state = {
            expanded: false,
        };
    }
    render() {
        const { point, className, style, onSizeKnown, ...buttonProps } = this.props;
        const divStyle = {
            top: point.y,
            left: point.x,
        };
        return (React.createElement("div", { className: "uifw-menu-button", style: divStyle, ref: (e) => this.setDivRef(e) },
            React.createElement(SquareButton_1.SquareButton, { ...buttonProps, className: className, style: style, onClick: this._handleClick },
                React.createElement(core_react_1.Icon, { iconSpec: React.createElement(itwinui_icons_react_1.SvgMore, null) })),
            React.createElement(core_react_1.ContextMenu, { ref: (el) => {
                    this._menu = el;
                }, selectedIndex: 0, onSelect: this._handleClose, onOutsideClick: this._handleClose, onEsc: this._handleClose, opened: this.state.expanded, autoflip: false }, this.props.children)));
    }
    setDivRef(div) {
        if (div) {
            const rect = div.getBoundingClientRect();
            const size = new core_react_1.Size(rect.width, rect.height);
            if (this.props.onSizeKnown)
                this.props.onSizeKnown(size);
        }
    }
}
exports.MenuButton = MenuButton;
//# sourceMappingURL=MenuButton.js.map