/*---------------------------------------------------------------------------------------------
 * 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 "./CardPopup.scss";
import * as React from "react";
import classnames from "classnames";
import { Key } from "ts-key-enum";
import { DivWithOutsideClick, FocusTrap, MessageRenderer, Point, Size, } from "@itwin/core-react";
import { Text } from "@itwin/itwinui-react";
import { CursorPopup } from "../cursor/cursorpopup/CursorPopup";
import { isReactContent, PopupManager } from "./PopupManager";
import { PositionPopup } from "./PositionPopup";
import { Direction, PropertyValueRendererManager, ToolbarOpacitySetting, ToolbarPanelAlignment, } from "@itwin/components-react";
import { ToolbarWithOverflow } from "../toolbar/ToolbarWithOverflow";
import { mapToPlacement } from "../utils/Placement";
import { WrapperContext } from "../configurableui/ConfigurableUiContent";
/** Popup component for Input Editor
 * @beta
 */
export class CardPopup extends React.PureComponent {
    constructor() {
        super(...arguments);
        /** @internal */
        this.state = {
            size: new Size(-1, -1),
        };
        this._onSizeKnown = (newSize) => {
            if (!this.state.size.equals(newSize))
                this.setState({ size: Size.create(newSize) });
        };
        this._handleKeyDown = (event) => {
            switch (event.key) {
                case Key.Escape.valueOf():
                    this._cancel();
                    break;
            }
        };
    }
    _cancel() {
        if (this.props.onCancel) {
            this.props.onCancel();
        }
    }
    render() {
        let point = PopupManager.getPopupPosition(this.props.el ?? this.context, this.props.pt, new Point(), this.state.size);
        const popupRect = CursorPopup.getPopupRect(point, this.props.offset, this.state.size, this.props.placement ?? mapToPlacement(this.props.relativePosition));
        point = new Point(popupRect.left, popupRect.top);
        return (React.createElement(PositionPopup, { key: this.props.id, className: classnames("uifw-no-border", "uifw-card"), point: point, onSizeKnown: this._onSizeKnown },
            React.createElement(DivWithOutsideClick, { onOutsideClick: this.props.onCancel, onKeyDown: this._handleKeyDown },
                React.createElement(FocusTrap, { active: true, returnFocusOnDeactivate: true },
                    React.createElement(Card, { content: this.props.content, title: this.props.title, items: this.props.items, 
                        // toolbarItems={this.props.toolbarItems}
                        onItemExecuted: this.props.onItemExecuted })))));
    }
}
/** @internal */
CardPopup.contextType = WrapperContext;
/** Card component
 * @beta */
export function Card(props) {
    let titleNode;
    if (props.title) {
        if (typeof props.title === "string")
            titleNode = React.createElement(Text, { variant: "leading" }, props.title);
        else {
            const propertyValueRendererManager = PropertyValueRendererManager.defaultManager;
            const titleValue = propertyValueRendererManager.render(props.title);
            titleNode = React.createElement(Text, { variant: "leading" }, titleValue);
        }
    }
    const content = isReactContent(props.content) ? (props.content.reactNode) : (
    // eslint-disable-next-line deprecation/deprecation
    React.createElement(MessageRenderer, { 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(ToolbarWithOverflow, { expandsTo: Direction.Bottom, panelAlignment: ToolbarPanelAlignment.Start, items: props.items, useDragInteraction: true, toolbarOpacitySetting: ToolbarOpacitySetting.Transparent, onItemExecuted: props.onItemExecuted })))));
}
//# sourceMappingURL=CardPopup.js.map