/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Item
 */
import * as React from "react";
import { ConditionalBooleanValue, UiError } from "@itwin/appui-abstract";
import { ContextMenuItem, ContextSubMenu } from "@itwin/core-react";
import { UiFramework } from "../UiFramework";
import { CommandItemDef } from "./CommandItemDef";
import { ItemDefBase } from "./ItemDefBase";
/** Menu Item
 * @alpha
 */
export class MenuItem extends ItemDefBase {
    /** onSelection is an optional parameter typically supplied to allow menu parent to close context menu when a menu item is selected. */
    constructor(props, onSelection) {
        super(props);
        this._id = "";
        this._id = props.id;
        this._submenu = new Array();
        this._onSelection = onSelection;
        if (props.item) {
            this._actionItem = new CommandItemDef(props.item);
            // Copy over icon, label & badgeType from the item
            if (!this.iconSpec)
                this.iconSpec = this._actionItem.iconSpec;
            if (!this.label)
                this.setLabel(this._actionItem.label);
            if (!this.badgeType)
                this.badgeType = this._actionItem.badgeType;
            if (!this.isDisabled)
                this.isDisabled = this._actionItem.isDisabled;
        }
        else if (props.submenu) {
            props.submenu.forEach((childProps) => {
                const childItem = new MenuItem(childProps, onSelection);
                this._submenu.push(childItem);
            });
        }
        else {
            throw new UiError(UiFramework.loggerCategory(this), `Either 'item' or 'submenu' must be specified for '${props.id}'.`);
        }
        this.iconRightSpec = props.iconRight;
    }
    get id() {
        return this._id;
    }
    get submenu() {
        return this._submenu;
    }
    get actionItem() {
        return this._actionItem;
    }
    itemPicked() {
        setTimeout(() => {
            // istanbul ignore else
            if (this._actionItem)
                this._actionItem.execute();
        });
        // istanbul ignore else
        if (this._onSelection)
            this._onSelection();
    }
}
/** Menu Item helper methods
 * @alpha
 */
export class MenuItemHelpers {
    static createMenuItems(itemPropsList, onSelection) {
        const menuItems = new Array();
        itemPropsList.forEach((itemProps) => {
            menuItems.push(new MenuItem(itemProps, onSelection));
        });
        return menuItems;
    }
    static createMenuItemNodes(itemList) {
        const itemNodes = [];
        itemList.forEach((item, index) => {
            const reactItem = this.createMenuItemNode(item, index);
            // istanbul ignore else
            if (reactItem)
                itemNodes.push(reactItem);
        });
        return itemNodes;
    }
    static createMenuItemNode(item, index) {
        let node = null;
        const label = item.label;
        const iconSpec = item.iconSpec;
        const iconRightSpec = item.iconRightSpec;
        const badgeType = item.badgeType;
        const isDisabled = ConditionalBooleanValue.getValue(item.isDisabled);
        if (item.actionItem) {
            const sel = () => item.itemPicked();
            node = (React.createElement(ContextMenuItem, { key: index, onSelect: sel, icon: iconSpec, iconRight: iconRightSpec, badgeType: badgeType, disabled: isDisabled }, label));
        }
        else {
            // istanbul ignore else
            if (item.submenu && item.submenu.length > 0) {
                const items = this.createMenuItemNodes(item.submenu);
                node = (React.createElement(ContextSubMenu, { key: index, icon: iconSpec, label: label, badgeType: badgeType, disabled: isDisabled }, items));
            }
        }
        return node;
    }
}
//# sourceMappingURL=MenuItem.js.map