/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Admin
 */
import * as React from "react";
import { IModelApp } from "@itwin/core-frontend";
import { RelativePosition, UiAdmin } from "@itwin/appui-abstract";
import { CursorInformation } from "../cursor/CursorInformation";
import { PopupManager } from "../popup/PopupManager";
import { UiFramework } from "../UiFramework";
import { getKeyinsFromToolList, KeyinFieldLocalization, } from "../keyins/Keyins";
import { mapToPlacement } from "../utils/Placement";
/** Subclass of `UiAdmin` in `@itwin/core-frontend` to be used to initialize `IModelApp`.
 *
 * This implementation uses themed react components that blends in AppUI look and feel.
 *
 * ```ts
 * await IModelApp.startup({
 *   uiAdmin: new FrameworkUiAdmin()
 * });
 * ```
 * @public
 */
export class FrameworkUiAdmin extends UiAdmin {
    constructor() {
        super(...arguments);
        this._localizedKeyinPreference = KeyinFieldLocalization.NonLocalized;
    }
    /**
     * @deprecated in 4.11.0. Gathering and filtering of keyins is now left up to the user.
     */
    get localizedKeyinPreference() {
        return this._localizedKeyinPreference;
    }
    set localizedKeyinPreference(preference) {
        this._localizedKeyinPreference = preference;
    }
    /** Gets the cursor X and Y position, which is mouseEvent.pageX and mouseEvent.pageY.
     * @deprecated in 4.11.0. Please use {@link CursorInformation.cursorPosition}
     */
    get cursorPosition() {
        return CursorInformation.cursorPosition;
    }
    /** Determines if focus is set to Home
     * @deprecated in 4.11.0. Please use {@link UiFramework.keyboardShortcuts().isFocusOnHome}
     */
    get isFocusOnHome() {
        return UiFramework.keyboardShortcuts.isFocusOnHome;
    }
    /** Sets focus to Home
     * @deprecated in 4.11.0. Please use {@link UiFramework.keyboardShortcuts().setFocusToHome}
     */
    setFocusToHome() {
        UiFramework.keyboardShortcuts.setFocusToHome();
    }
    /** Show a context menu at a particular location.
     * @param items Properties of the menu items to display.
     * @param location Location of the context menu, relative to the origin of htmlElement or the overall window.
     * @param htmlElement The HTMLElement that anchors the context menu. If undefined, the location is relative to the overall window.
     * @return true if the menu was displayed, false if the menu could not be displayed.
     * @deprecated in 4.11.0. Use {@link UiFramework.openContextMenu}
     */
    showContextMenu(items, location, htmlElement) {
        return UiFramework.openContextMenu(items, location, htmlElement);
    }
    /**
     * @deprecated in 4.11.0. Construct your own {@link KeyinEntry[]} via {@link @itwin/core-frontend#IModelApp.tools.getToolList}.
     */
    getKeyins() {
        const tools = IModelApp.tools.getToolList();
        return getKeyinsFromToolList(tools);
    }
    /** Show the Key-in Palette to display key-in from all registered Tools.
     * @param htmlElement The HTMLElement that anchors the Popup. If undefined, the location is relative to the overall window.
     * @return true if the Command Palette was displayed, false if it could not be displayed.
     * @deprecated in 4.11.0. Use {@link UiFramework.showKeyinPalette}. Please note, the use of feature flags to control whether the KeyinPalette can be opened has been deprecated.
     */
    showKeyinPalette(htmlElement) {
        if (!this.featureFlags.allowKeyinPalette)
            return false;
        const tools = IModelApp.tools.getToolList();
        const keyIns = getKeyinsFromToolList(tools);
        return UiFramework.showKeyinPalette(keyIns, htmlElement);
    }
    /**
     * Hides the Key-in Palette.
     * @deprecated in 4.11.0. Use {@link UiFramework.hideKeyinPalette}
     */
    hideKeyinPalette() {
        if (!this.featureFlags.allowKeyinPalette)
            return false;
        return UiFramework.hideKeyinPalette();
    }
    /** Show a Toolbar at a particular location.
     * @param toolbarProps Properties of the Toolbar to display.
     * @param location Location of the Toolbar, relative to the origin of htmlElement or the overall window.
     * @param offset Offset of the Toolbar from the location.
     * @param onItemExecuted Function invoked after a Toolbar item is executed
     * @param onCancel Function invoked when the Escape key is pressed or a click occurs outside the Toolbar
     * @param relativePosition Position relative to the given location. Defaults to TopRight.
     * @param htmlElement The HTMLElement that anchors the Toolbar. If undefined, the location is relative to the overall window.
     * @return true if the Toolbar was displayed, false if the Toolbar could not be displayed.
     * @deprecated in 4.11.0. Please use {@link UiFramework.showToolbar}.
     */
    showToolbar(toolbarProps, location, offset, onItemExecuted, onCancel, relativePosition, htmlElement) {
        const placement = mapToPlacement(relativePosition);
        return UiFramework.showToolbar(toolbarProps, location, offset, onItemExecuted, onCancel, placement, htmlElement);
    }
    /** Hides the toolbar.
     * @deprecated in 4.11.0. Please use {@link UiFramework.hideToolbar}.
     */
    hideToolbar() {
        return UiFramework.hideToolbar();
    }
    /** Show a menu button at a particular location. A menu button opens a context menu.
     * @param id Id of the menu button. Multiple menu buttons may be displayed.
     * @param menuItemsProps Properties of the menu items to display.
     * @param location Location of the context menu, relative to the origin of htmlElement or the window.
     * @param htmlElement The HTMLElement that anchors the context menu. If undefined, the location is relative to the overall window.
     * @return true if the menu was displayed, false if the menu could not be displayed.
     * @deprecated in 4.11.0. Please use {@link UiFramework.showMenuButton}.
     */
    showMenuButton(id, menuItemsProps, location, htmlElement) {
        return UiFramework.showMenuButton(id, menuItemsProps, location, htmlElement);
    }
    /** Hides a menu button.
     * @param id Id of the menu button. Multiple menu buttons may be displayed.
     * @return true if the menu was hidden, false if the menu could not be hidden.
     * @deprecated in 4.11.0. Please use {@link UiFramework.hideMenuButton}.
     */
    hideMenuButton(id) {
        return UiFramework.hideMenuButton(id);
    }
    /** Show a calculator at a particular location.
     * @param initialValue Value initially displayed in the calculator.
     * @param resultIcon Icon displayed to the left of the value.
     * @param location Location of the calculator, relative to the origin of htmlElement or the window.
     * @param onOk Function called when the OK button or the Enter key is pressed.
     * @param onCancel Function called when the Cancel button or the Escape key  is pressed.
     * @param htmlElement The HTMLElement that anchors the context menu. If undefined, the location is relative to the overall window.
     * @return true if the menu was displayed, false if the menu could not be displayed.
     * @deprecated in 4.11.0. Use {@link UiFramework.showCalculator}.
     */
    showCalculator(initialValue, resultIcon, location, onOk, onCancel, htmlElement) {
        return UiFramework.showCalculator(initialValue, resultIcon, location, onOk, onCancel, htmlElement);
    }
    /** Hides the calculator.
     * @deprecated in 4.11.0. Use {@link UiFramework.hideCalculator}.
     */
    hideCalculator() {
        return UiFramework.hideCalculator();
    }
    /** Show an input editor for an angle value at a particular location.
     * @param initialValue Value initially displayed in the editor.
     * @param location Location of the editor, relative to the origin of htmlElement or the window.
     * @param onCommit Function called when the OK button or the Enter key is pressed.
     * @param onCancel Function called when the Cancel button or the Escape key  is pressed.
     * @param htmlElement The HTMLElement that anchors the context menu. If undefined, the location is relative to the overall window.
     * @return true if the editor was displayed, false if the editor could not be displayed.
     * @deprecated in 4.11.0. Use {@link UiFramework.showAngleEditor}.
     */
    showAngleEditor(initialValue, location, onCommit, onCancel, htmlElement) {
        return UiFramework.showAngleEditor(initialValue, location, onCommit, onCancel, htmlElement);
    }
    /** Show an input editor for a length value at a particular location.
     * @param initialValue Value initially displayed in the editor.
     * @param location Location of the editor, relative to the origin of htmlElement or the window.
     * @param onCommit Function called when the OK button or the Enter key is pressed.
     * @param onCancel Function called when the Cancel button or the Escape key  is pressed.
     * @param htmlElement The HTMLElement that anchors the context menu. If undefined, the location is relative to the overall window.
     * @return true if the editor was displayed, false if the editor could not be displayed.
     * @deprecated in 4.11.0. Use {@link UiFramework.showDimensionEditor("length")}.
     */
    showLengthEditor(initialValue, location, onCommit, onCancel, htmlElement) {
        return UiFramework.showDimensionEditor("length", initialValue, location, onCommit, onCancel, htmlElement);
    }
    /** Show an input editor for a height value at a particular location.
     * @param initialValue Value initially displayed in the editor.
     * @param location Location of the editor, relative to the origin of htmlElement or the window.
     * @param onCommit Function called when the OK button or the Enter key is pressed.
     * @param onCancel Function called when the Cancel button or the Escape key  is pressed.
     * @param htmlElement The HTMLElement that anchors the context menu. If undefined, the location is relative to the overall window.
     * @return true if the editor was displayed, false if the editor could not be displayed.
     * @deprecated in 4.11.0. Use {@link UiFramework.showDimensionEditor("height")}.
     */
    showHeightEditor(initialValue, location, onCommit, onCancel, htmlElement) {
        return UiFramework.showDimensionEditor("height", initialValue, location, onCommit, onCancel, htmlElement);
    }
    /** Show an input editor for a primitive value at a particular location.
     * @param initialValue Value initially displayed in the editor.
     * @param propertyDescription Description of the primitive value property
     * @param location Location of the editor, relative to the origin of htmlElement or the window.
     * @param onCommit Function called when the OK button or the Enter key is pressed.
     * @param onCancel Function called when the Cancel button or the Escape key  is pressed.
     * @param htmlElement The HTMLElement that anchors the context menu. If undefined, the location is relative to the overall window.
     * @return true if the editor was displayed, false if the editor could not be displayed.
     */
    showInputEditor(initialValue, propertyDescription, location, onCommit, onCancel, htmlElement) {
        return UiFramework.showInputEditor({
            initialValue,
            propertyDescription,
            location,
            onCommit,
            onCancel,
            anchorElement: htmlElement,
        });
    }
    /** Hides the input editor.*/
    hideInputEditor() {
        return UiFramework.hideInputEditor();
    }
    /** Show an HTML element at a particular location.
     * @param displayElement The HTMLElement to display
     * @param location Location of the display element, relative to the origin of htmlElement or the window
     * @param offset Offset of the display element from the location
     * @param onCancel Function invoked when the Escape key is pressed or a click occurs outside the display element
     * @param relativePosition Position relative to the given location. Defaults to TopRight.
     * @param anchorElement The HTMLElement that anchors the display element. If undefined, the location is relative to the overall window.
     * @return true if the display element was displayed, false if the display element could not be displayed.
     * @deprecated in 4.11.0. Use {@link UiFramework.showComponent}
     */
    showHTMLElement(displayElement, location, offset, onCancel, relativePosition, anchorElement) {
        const el = anchorElement ?? UiFramework.controls.getWrapperElement();
        if (relativePosition === undefined)
            relativePosition = RelativePosition.TopRight;
        return PopupManager.showHTMLElement(displayElement, el, location, offset, onCancel, relativePosition);
    }
    /**
     *  Hides the HTML Element.
     * @deprecated in 4.11.0. Use {@link UiFramework.hideComponent}
     */
    hideHTMLElement() {
        return PopupManager.hideHTMLElement();
    }
    /** Show a Card containing content, a title and a toolbar at a particular location.
     * @param content The HTMLElement of the content to display
     * @param title Title to display at the top of the card.
     * @param toolbarProps Properties of the Toolbar to display.
     * @param location Location of the Card, relative to the origin of htmlElement or the window.
     * @param offset Offset of the Card from the location.
     * @param onItemExecuted Function invoked after a Toolbar item is executed
     * @param onCancel Function invoked when the Escape key is pressed or a click occurs outside the Card
     * @param relativePosition Position relative to the given location. Defaults to TopRight.
     * @param anchorElement The HTMLElement that anchors the Card. If undefined, the location is relative to the overall window.
     * @return true if the Card was displayed, false if the Card could not be displayed.
     */
    showCard(content, title, toolbarProps, location, offset, onItemExecuted, onCancel, relativePosition, anchorElement) {
        const placement = mapToPlacement(relativePosition);
        return UiFramework.showCard(React.createElement(CardRenderer, { content: content }), title, toolbarProps
            ? AbstractToolbarPropsToToolbarProps(toolbarProps)
            : { items: [] }, location, offset, onItemExecuted, onCancel, placement, anchorElement);
    }
    /** Show a Card containing React-based content, a title and a toolbar at a particular location.
     * @param content The React node of the content to display
     * @param title Title to display at the top of the card.
     * @param toolbarProps Properties of the Toolbar to display.
     * @param location Location of the Card, relative to the origin of htmlElement or the window.
     * @param offset Offset of the Card from the location.
     * @param onItemExecuted Function invoked after a Toolbar item is executed
     * @param onCancel Function invoked when the Escape key is pressed or a click occurs outside the Card
     * @param relativePosition Position relative to the given location. Defaults to TopRight.
     * @param anchorElement The HTMLElement that anchors the Card. If undefined, the location is relative to the overall window.
     * @return true if the Card was displayed, false if the Card could not be displayed.
     */
    showReactCard(content, title, toolbarProps, location, offset, onItemExecuted, onCancel, relativePosition, anchorElement) {
        const placement = mapToPlacement(relativePosition);
        return UiFramework.showCard(content, title, toolbarProps
            ? AbstractToolbarPropsToToolbarProps(toolbarProps)
            : { items: [] }, location, offset, onItemExecuted, onCancel, placement, anchorElement);
    }
    /**
     * Hides the Card.
     * */
    hideCard() {
        return UiFramework.hideCard();
    }
    /** Opens a Tool Settings Ui popup at a particular location.
     * @param dataProvider The DialogLayoutDataProvider for the tool settings popup dialog.
     * @param location Location of the tool settings, relative to the origin of anchorElement or the window
     * @param offset Offset of the tool settings from the location
     * @param onCancel Function invoked when the Escape key is pressed or a click occurs outside the tool settings
     * @param relativePosition Position relative to the given location. Defaults to TopRight.
     * @param anchorElement The HTMLElement that anchors the tool settings. If undefined, the location is relative to the overall window.
     * @return true if the tool settings were displayed, false if the tool settings could not be displayed.
     */
    openToolSettingsPopup(dataProvider, location, offset, onCancel, relativePosition, anchorElement) {
        const placement = mapToPlacement(relativePosition);
        return UiFramework.openToolSettingsPopup(dataProvider, location, offset, onCancel, placement, anchorElement);
    }
    /** Closes the Tool Settings Ui popup. */
    closeToolSettingsPopup() {
        return UiFramework.closeToolSettingsPopup();
    }
    /** Opens a Dialog and automatically populates it using the properties defined by the UiDataProvider.
     * @param uiDataProvider The UiDataProvider for the tool settings
     * @param title Specify title for dialog.
     * @param isModal Specify if the dialog is opened as a modal or modeless.
     * @param id Id of the dialog that is used to close it.
     * @param optionalProps Optional props for Dialog construction.
     * @return true if the tool settings were displayed, false if the tool settings could not be displayed.
     */
    openDialog(uiDataProvider, title, isModal, id, optionalProps) {
        return UiFramework.openDialog(uiDataProvider, title, isModal, id, optionalProps);
    }
    /** Closes the Tool Settings Ui popup.*/
    closeDialog(dialogId) {
        return UiFramework.closeDialog(dialogId);
    }
}
// utility functions to wean off appui-abstract
function AbstractToolbarPropsToToolbarProps(props) {
    return {
        items: props.items.map(CommonToolbarItemToToolBarItem),
        itemId: props.toolbarId,
    };
}
function CommonToolbarItemToToolBarItem(item) {
    return {
        ...item,
        badge: item.badgeType,
    };
}
function CardRenderer({ content }) {
    const ref = React.useRef(null);
    React.useEffect(() => {
        const el = ref.current;
        el?.appendChild(content);
        return () => {
            el?.removeChild(content);
        };
    }, [content]);
    return React.createElement("div", { ref: ref });
}
//# sourceMappingURL=FrameworkUiAdmin.js.map