import { ConditionalStringValue, ToolbarItemUtilities, } from "@itwin/appui-abstract";
import { IconHelper } from "@itwin/core-react";
import { CommandItemDef } from "../shared/CommandItemDef";
import { CustomItemDef } from "../shared/CustomItemDef";
import { ToolItemDef } from "../shared/ToolItemDef";
import { ActionButtonItem } from "./ActionButtonItem";
import { GroupButtonItem } from "./GroupButtonItem";
import { GroupItemDef } from "./GroupItem";
/** Helper functions for defining an ToolbarComposer.
 * @public
 */
export 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 = 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 (ToolbarItemUtilities.isActionButton(item) || ToolbarItemUtilities.isGroupButton(item))
                items.push(item);
        }
        return items;
    }
    static getStringOrConditionalString(inString) {
        if (inString instanceof ConditionalStringValue || typeof inString === "string")
            return inString;
        return inString();
    }
    static getIconReactNode(item) {
        return 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 = IconHelper.getIconData(itemDef.iconSpec, internalData);
        const label = this.getStringOrConditionalString(itemDef.rawLabel);
        const badgeType = itemDef.badgeType;
        // istanbul ignore else
        if (itemDef instanceof CommandItemDef) {
            return {
                id: itemDef.id,
                itemPriority,
                icon,
                label,
                isHidden,
                isDisabled,
                isActive: itemDef.isActive,
                execute: itemDef.execute,
                badgeType,
                internalData,
                ...overrides,
            };
        }
        else if (itemDef instanceof CustomItemDef) {
            return ToolbarHelper.createCustomDefinitionToolbarItem(itemPriority, itemDef, overrides);
        }
        else if (itemDef instanceof 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) {
            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 (ToolbarItemUtilities.isActionButton(item)) {
            return ActionButtonItem({ item, onItemExecuted });
        }
        if (ToolbarItemUtilities.isGroupButton(item)) {
            return GroupButtonItem({ item, onItemExecuted });
        }
        if (ToolbarHelper.isCustomToolbarButton(item)) {
            return item.buttonNode ? /* istanbul ignore next */ item.buttonNode : null;
        }
        return null;
    }
}
/** CustomToolbarButton type guard.
 * @internal
 */
ToolbarHelper.isCustomToolbarButton = (item) => {
    return !!item.isCustom && ("buttonNode" in item);
};
//# sourceMappingURL=ToolbarHelper.js.map