import { ConditionalStringValue } from "@itwin/appui-abstract";
import { assert } from "@itwin/core-bentley";
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";
/* eslint-disable deprecation/deprecation */
/**
 * 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
 * @deprecated in 4.15.0. Use {@link ToolbarItemUtilities} instead.
 */
export class ToolbarHelper {
    /** Construct ToolbarCustomItem definitions given a CustomItemDef.
     * @deprecated in 4.15.0. Use {@link ToolbarItemUtilities.createCustomItem} instead.
     */
    static createCustomDefinitionToolbarItem(itemPriority, itemDef, overrides) {
        return this.createToolbarItemFromItemDef(itemPriority, itemDef, overrides);
    }
    /** Construct ToolbarActionItem and ToolbarGroupItem definitions given an array of ItemDefs.
     * @deprecated in 4.15.0. Use {@link ToolbarItemUtilities} instead.
     */
    static constructChildToolbarItems(itemDefs) {
        return constructToolbarItemArray(itemDefs, false);
    }
    static getStringOrConditionalString(inString) {
        if (inString instanceof ConditionalStringValue ||
            typeof inString === "string")
            return inString;
        return inString();
    }
    /** @deprecated in 4.15.0. Use {@link @itwin/core-react#IconHelper} instead. */
    static getIconReactNode(item) {
        // eslint-disable-next-line deprecation/deprecation
        return IconHelper.getIconReactNode(item.icon, item.internalData);
    }
    /** Helper method to creates a generic toolbar item entry.
     * @deprecated in 4.15.0. Use {@link ToolbarItemUtilities} instead.
     */
    static createToolbarItemFromItemDef(itemPriority, itemDef, overrides) {
        const { badgeType, description, icon, internalData, ...itemOverrides } = overrides ?? {};
        const itemBase = {
            id: itemDef.id,
            itemPriority,
            isActive: itemDef.isActive,
            isHidden: itemDef.isHidden,
            isDisabled: itemDef.isDisabled,
            icon: internalData?.get(IconHelper.reactIconKey) ?? // eslint-disable-line deprecation/deprecation
                internalData?.get(IconHelper.conditionalIconItemKey) ?? // eslint-disable-line deprecation/deprecation
                icon ??
                itemDef.iconSpec,
            label: this.getStringOrConditionalString(itemDef.rawLabel),
            badge: badgeType ?? itemDef.badgeType,
            description: description ?? itemDef.description,
        };
        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.
     * @deprecated in 4.15.0. Use {@link ToolbarItemUtilities} instead.
     */
    static createToolbarItemsFromItemDefs(itemDefs, startingItemPriority = 10, overrides) {
        return constructToolbarItemArray(itemDefs, true, startingItemPriority, overrides);
    }
}
/** @internal */
export function itemDefToToolbarActionItem(itemDef, overrides) {
    const item = ToolbarHelper.createToolbarItemFromItemDef(0, itemDef);
    assert(isToolbarActionItem(item));
    return {
        ...item,
        ...overrides,
    };
}
/** @internal */
export function itemDefToToolbarGroupItem(itemDef, overrides) {
    const item = ToolbarHelper.createToolbarItemFromItemDef(0, itemDef);
    assert(isToolbarGroupItem(item));
    return {
        ...item,
        ...overrides,
    };
}
//# sourceMappingURL=ToolbarHelper.js.map