"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.Card = exports.CardPopup = void 0;
require("./CardPopup.scss");
const React = require("react");
const classnames_1 = require("classnames");
const appui_abstract_1 = require("@itwin/appui-abstract");
const core_react_1 = require("@itwin/core-react");
const itwinui_react_1 = require("@itwin/itwinui-react");
const CursorPopup_1 = require("../cursor/cursorpopup/CursorPopup");
const PopupManager_1 = require("./PopupManager");
const PositionPopup_1 = require("./PositionPopup");
const MessageSpan_1 = require("../messages/MessageSpan");
const components_react_1 = require("@itwin/components-react");
/** Popup component for Input Editor
 * @beta
 */
class CardPopup extends React.PureComponent {
    constructor() {
        super(...arguments);
        /** @internal */
        this.state = {
            size: new core_react_1.Size(-1, -1),
        };
        this._onSizeKnown = (newSize) => {
            // istanbul ignore else
            if (!this.state.size.equals(newSize))
                this.setState({ size: core_react_1.Size.create(newSize) });
        };
        this._handleKeyDown = (event) => {
            switch (event.key) {
                case appui_abstract_1.SpecialKey.Escape:
                    this._cancel();
                    break;
            }
        };
    }
    _cancel() {
        // istanbul ignore else
        if (this.props.onCancel) {
            this.props.onCancel();
        }
    }
    render() {
        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, className: (0, classnames_1.default)("uifw-no-border", "uifw-card"), point: point, onSizeKnown: this._onSizeKnown },
            React.createElement(core_react_1.DivWithOutsideClick, { onOutsideClick: this.props.onCancel, onKeyDown: this._handleKeyDown },
                React.createElement(core_react_1.FocusTrap, { active: true, returnFocusOnDeactivate: true },
                    React.createElement(Card, { content: this.props.content, title: this.props.title, items: this.props.items, onItemExecuted: this.props.onItemExecuted })))));
    }
}
exports.CardPopup = CardPopup;
/** Card component
 * @beta */
function Card(props) {
    let titleNode;
    // istanbul ignore else
    if (props.title) {
        if (typeof props.title === "string")
            titleNode = React.createElement(itwinui_react_1.Text, { variant: "leading" }, props.title);
        else {
            const propertyValueRendererManager = components_react_1.PropertyValueRendererManager.defaultManager;
            const titleValue = propertyValueRendererManager.render(props.title);
            titleNode = React.createElement(itwinui_react_1.Text, { variant: "leading" }, titleValue);
        }
    }
    const content = (0, PopupManager_1.isReactContent)(props.content) ? (props.content.reactNode) : (React.createElement(MessageSpan_1.MessageDiv, { message: props.content }));
    return (React.createElement(React.Fragment, null,
        React.createElement("div", { className: "uifw-card-content" },
            props.title && (React.createElement(React.Fragment, null,
                titleNode,
                React.createElement("div", { className: "uifw-card-gap" }))),
            content),
        props.items && (React.createElement(React.Fragment, null,
            React.createElement("div", { className: "uifw-card-separator" }),
            React.createElement(components_react_1.ToolbarWithOverflow // eslint-disable-line deprecation/deprecation
            , { expandsTo: components_react_1.Direction.Bottom, panelAlignment: components_react_1.ToolbarPanelAlignment.Start, items: props.items, useDragInteraction: true, toolbarOpacitySetting: components_react_1.ToolbarOpacitySetting.Transparent, onItemExecuted: props.onItemExecuted })))));
}
exports.Card = Card;
//# sourceMappingURL=CardPopup.js.map