import { ConditionalStringValue } 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 { GroupItemDef } from "./GroupItem";
import { isToolbarActionItem, isToolbarGroupItem } from "./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 || isToolbarActionItem(item) || isToolbarGroupItem(item))
            items.push(item);
    }
    return items;
}
/** Helper functions for defining a Toolbar items used in `Toolbar`, `ToolbarComposer` and `UiItemsProvider`.
 * @public
 */
export 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 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) {
        var _a, _b, _c;
        const { badgeType, icon, internalData, ...itemOverrides } = overrides !== null && overrides !== void 0 ? overrides : {};
        const itemBase = {
            id: itemDef.id,
            itemPriority,
            isActive: itemDef.isActive,
            isHidden: itemDef.isHidden,
            isDisabled: itemDef.isDisabled,
            icon: (_c = (_b = (_a = internalData === null || internalData === void 0 ? void 0 : internalData.get(IconHelper.reactIconKey)) !== null && _a !== void 0 ? _a : internalData === null || internalData === void 0 ? void 0 : internalData.get(IconHelper.conditionalIconItemKey)) !== null && _b !== void 0 ? _b : icon) !== null && _c !== void 0 ? _c : itemDef.iconSpec,
            label: this.getStringOrConditionalString(itemDef.rawLabel),
            badge: badgeType !== null && badgeType !== void 0 ? badgeType : itemDef.badgeType,
        };
        // istanbul ignore else
        if (itemDef instanceof CommandItemDef || itemDef instanceof ToolItemDef) {
            return {
                ...itemBase,
                execute: itemDef.execute,
                ...itemOverrides,
            };
        }
        else if (itemDef instanceof CustomItemDef) {
            return {
                ...itemBase,
                panelContent: itemDef.popupPanelNode,
                ...itemOverrides,
            };
        }
        else if (itemDef instanceof 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);
    }
}
//# sourceMappingURL=ToolbarHelper.js.map