"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ToolbarHelper = void 0;
const appui_abstract_1 = require("@itwin/appui-abstract");
const core_react_1 = require("@itwin/core-react");
const CommandItemDef_1 = require("../shared/CommandItemDef");
const CustomItemDef_1 = require("../shared/CustomItemDef");
const ToolItemDef_1 = require("../shared/ToolItemDef");
const GroupItem_1 = require("./GroupItem");
const ToolbarItem_1 = require("./ToolbarItem");
/**
 * Common implementation for `constructChildToolbarItems` and `createToolbarItemsFromItemDefs`.
 * @param itemDefs List of itemDefs to convert.
 * @param allowCustom false if we are building children and don't support custom items.
 * @param startingItemPriority defaults to 10.
 * @param overrides Single override to apply to each itemDefs.
 * @returns ToolbarItem[] or (ToolbarActionItem | ToolbarGroupItem) if allowCustom is false.
 */
function constructToolbarItemArray(itemDefs, allowCustom, startingItemPriority = 10, overrides) {
    let priority = startingItemPriority;
    const items = [];
    for (const itemDef of itemDefs) {
        const item = ToolbarHelper.createToolbarItemFromItemDef(priority, itemDef, overrides);
        priority = priority + 10;
        if (allowCustom || (0, ToolbarItem_1.isToolbarActionItem)(item) || (0, ToolbarItem_1.isToolbarGroupItem)(item))
            items.push(item);
    }
    return items;
}
/** Helper functions for defining a Toolbar items used in `Toolbar`, `ToolbarComposer` and `UiItemsProvider`.
 * @public
 */
class ToolbarHelper {
    /** Construct ToolbarCustomItem definitions given a CustomItemDef. */
    static createCustomDefinitionToolbarItem(itemPriority, itemDef, overrides) {
        return this.createToolbarItemFromItemDef(itemPriority, itemDef, overrides);
    }
    /** Construct ToolbarActionItem and ToolbarGroupItem definitions given an array of ItemDefs. */
    static constructChildToolbarItems(itemDefs) {
        return constructToolbarItemArray(itemDefs, false);
    }
    static getStringOrConditionalString(inString) {
        if (inString instanceof appui_abstract_1.ConditionalStringValue ||
            typeof inString === "string")
            return inString;
        return inString();
    }
    static getIconReactNode(item) {
        return core_react_1.IconHelper.getIconReactNode(item.icon, item.internalData);
    }
    /** Helper method to creates a generic toolbar item entry */
    static createToolbarItemFromItemDef(itemPriority, itemDef, overrides) {
        const { badgeType, icon, internalData, ...itemOverrides } = overrides ?? {};
        const itemBase = {
            id: itemDef.id,
            itemPriority,
            isActive: itemDef.isActive,
            isHidden: itemDef.isHidden,
            isDisabled: itemDef.isDisabled,
            icon: internalData?.get(core_react_1.IconHelper.reactIconKey) ??
                internalData?.get(core_react_1.IconHelper.conditionalIconItemKey) ??
                icon ??
                itemDef.iconSpec,
            label: this.getStringOrConditionalString(itemDef.rawLabel),
            badge: badgeType ?? itemDef.badgeType,
        };
        // istanbul ignore else
        if (itemDef instanceof CommandItemDef_1.CommandItemDef || itemDef instanceof ToolItemDef_1.ToolItemDef) {
            return {
                ...itemBase,
                execute: itemDef.execute,
                ...itemOverrides,
            };
        }
        else if (itemDef instanceof CustomItemDef_1.CustomItemDef) {
            return {
                ...itemBase,
                panelContent: itemDef.popupPanelNode,
                ...itemOverrides,
            };
        }
        else if (itemDef instanceof GroupItem_1.GroupItemDef) {
            return {
                ...itemBase,
                panelLabel: itemDef.panelLabel,
                items: this.constructChildToolbarItems(itemDef.items),
                isActive: false,
                ...itemOverrides,
            };
        }
        else {
            throw new Error(`Invalid Item type encountered, item id=${itemDef.id}`);
        }
    }
    /**
     * Helper method to creates a generic toolbar item entry list.
     */
    static createToolbarItemsFromItemDefs(itemDefs, startingItemPriority = 10, overrides) {
        return constructToolbarItemArray(itemDefs, true, startingItemPriority, overrides);
    }
}
exports.ToolbarHelper = ToolbarHelper;
//# sourceMappingURL=ToolbarHelper.js.map