"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 ActionButtonItem_1 = require("./ActionButtonItem");
const GroupButtonItem_1 = require("./GroupButtonItem");
const GroupItem_1 = require("./GroupItem");
/** Helper functions for defining an ToolbarComposer.
 * @public
 */
class ToolbarHelper {
    /** Construct CustomToolbarItem definitions given a CustomItemDef. */
    static createCustomDefinitionToolbarItem(itemPriority, itemDef, overrides) {
        const isHidden = itemDef.isHidden;
        const isDisabled = itemDef.isDisabled;
        const internalData = new Map(); // used to store ReactNode if iconSpec hold a ReactNode
        const icon = core_react_1.IconHelper.getIconData(itemDef.iconSpec, internalData);
        const label = this.getStringOrConditionalString(itemDef.rawLabel);
        const badgeType = itemDef.badgeType;
        // istanbul ignore else
        return {
            id: itemDef.id,
            itemPriority,
            icon,
            label,
            isCustom: true,
            isHidden,
            isDisabled,
            internalData,
            badgeType,
            buttonNode: itemDef.toolbarReactNode(),
            panelContentNode: itemDef.popupPanelNode,
            ...overrides,
        };
    }
    /** Construct ActionButton and GroupButton definitions given an array to ItemDefs. */
    static constructChildToolbarItems(itemDefs) {
        let count = 10;
        const items = [];
        for (const itemDef of itemDefs) {
            const item = this.createToolbarItemFromItemDef(count, itemDef);
            count = count + 10;
            // istanbul ignore else
            if (appui_abstract_1.ToolbarItemUtilities.isActionButton(item) || appui_abstract_1.ToolbarItemUtilities.isGroupButton(item))
                items.push(item);
        }
        return items;
    }
    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 isHidden = itemDef.isHidden;
        const isDisabled = itemDef.isDisabled;
        const internalData = new Map(); // used to store ReactNode if iconSpec hold a ReactNode
        const icon = core_react_1.IconHelper.getIconData(itemDef.iconSpec, internalData);
        const label = this.getStringOrConditionalString(itemDef.rawLabel);
        const badgeType = itemDef.badgeType;
        // istanbul ignore else
        if (itemDef instanceof CommandItemDef_1.CommandItemDef) {
            return {
                id: itemDef.id,
                itemPriority,
                icon,
                label,
                isHidden,
                isDisabled,
                isActive: itemDef.isActive,
                execute: itemDef.execute,
                badgeType,
                internalData,
                ...overrides,
            };
        }
        else if (itemDef instanceof CustomItemDef_1.CustomItemDef) {
            return ToolbarHelper.createCustomDefinitionToolbarItem(itemPriority, itemDef, overrides);
        }
        else if (itemDef instanceof GroupItem_1.GroupItemDef) {
            const children = this.constructChildToolbarItems(itemDef.items);
            return {
                id: itemDef.id,
                itemPriority,
                icon,
                label,
                panelLabel: itemDef.panelLabel,
                isHidden,
                isDisabled,
                items: children,
                isActive: false,
                badgeType,
                internalData,
                ...overrides,
            };
        }
        else if (itemDef instanceof ToolItemDef_1.ToolItemDef) {
            return {
                id: itemDef.id,
                itemPriority,
                icon,
                label,
                isHidden,
                isDisabled,
                isActive: itemDef.isActive,
                execute: itemDef.execute,
                badgeType,
                internalData,
                ...overrides,
            };
        }
        else {
            throw new Error(`Invalid Item type encountered, item id=${itemDef.id}`);
        }
    }
    static createToolbarItemsFromItemDefs(itemDefs, startingItemPriority = 10, overrides) {
        let itemPriority = startingItemPriority;
        const items = itemDefs.map((itemDef) => {
            const item = ToolbarHelper.createToolbarItemFromItemDef(itemPriority, itemDef, overrides);
            itemPriority = itemPriority + 10;
            return item;
        });
        return items;
    }
    static createNodeForToolbarItem(item, onItemExecuted) {
        if (appui_abstract_1.ToolbarItemUtilities.isActionButton(item)) {
            return (0, ActionButtonItem_1.ActionButtonItem)({ item, onItemExecuted });
        }
        if (appui_abstract_1.ToolbarItemUtilities.isGroupButton(item)) {
            return (0, GroupButtonItem_1.GroupButtonItem)({ item, onItemExecuted });
        }
        if (ToolbarHelper.isCustomToolbarButton(item)) {
            return item.buttonNode ? /* istanbul ignore next */ item.buttonNode : null;
        }
        return null;
    }
}
exports.ToolbarHelper = ToolbarHelper;
/** CustomToolbarButton type guard.
 * @internal
 */
ToolbarHelper.isCustomToolbarButton = (item) => {
    return !!item.isCustom && ("buttonNode" in item);
};
//# sourceMappingURL=ToolbarHelper.js.map