/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Backstage
 */
import * as React from "react";
import { Logger } from "@itwin/core-bentley";
import { ConditionalBooleanValue, ConditionalStringValue, } from "@itwin/appui-abstract";
import { BadgeUtilities, Icon } from "@itwin/core-react";
import { BackstageItem as NZ_BackstageItem } from "@itwin/appui-layout-react";
import { useBackstageManager } from "./BackstageManager";
import { UiFramework } from "../UiFramework";
import { useActiveFrontstageId } from "../frontstage/FrontstageDef";
import { isBackstageStageLauncher } from "./BackstageItem";
import { isProviderItem } from "../ui-items-provider/isProviderItem";
/** @internal */
export function BackstageComposerActionItem({ item, }) {
    const manager = useBackstageManager();
    const handleClick = React.useCallback(() => {
        manager.close();
        item.execute();
    }, [manager, item]);
    return (React.createElement(NZ_BackstageItem, { itemId: item.id, providerId: isProviderItem(item) ? item.providerId : undefined, itemPriority: item.itemPriority, groupPriority: item.groupPriority, icon: React.createElement(Icon, { iconSpec: item.icon }), isActive: ConditionalBooleanValue.getValue(item.isActive), isDisabled: ConditionalBooleanValue.getValue(item.isDisabled), onClick: handleClick, subtitle: ConditionalStringValue.getValue(item.subtitle), badge: BadgeUtilities.getComponentForBadgeType(item.badge) }, ConditionalStringValue.getValue(item.label)));
}
/** @internal */
export function BackstageComposerStageLauncher({ item, }) {
    var _a;
    const manager = useBackstageManager();
    const handleClick = React.useCallback(() => {
        manager.close();
        if (!UiFramework.frontstages.hasFrontstage(item.stageId))
            return Logger.logError("BackstageComposerStageLauncher", `Frontstage with id '${item.stageId}' not found`);
        void UiFramework.frontstages.setActiveFrontstage(item.stageId);
    }, [manager, item.stageId]);
    const activeFrontstageId = useActiveFrontstageId();
    const isActive = ConditionalBooleanValue.getValue((_a = item.isActive) !== null && _a !== void 0 ? _a : item.stageId === activeFrontstageId);
    return (React.createElement(NZ_BackstageItem, { itemId: item.id, providerId: isProviderItem(item) ? item.providerId : undefined, itemPriority: item.itemPriority, groupPriority: item.groupPriority, icon: React.createElement(Icon, { iconSpec: item.icon }), isActive: isActive, isDisabled: ConditionalBooleanValue.getValue(item.isDisabled), onClick: handleClick, subtitle: ConditionalStringValue.getValue(item.subtitle), badge: BadgeUtilities.getComponentForBadgeType(item.badge) }, ConditionalStringValue.getValue(item.label)));
}
/** Item of [[BackstageComposer]].
 * @internal
 */
export function BackstageComposerItem({ item }) {
    if (isBackstageStageLauncher(item)) {
        return React.createElement(BackstageComposerStageLauncher, { item: item });
    }
    return React.createElement(BackstageComposerActionItem, { item: item });
}
//# sourceMappingURL=BackstageComposerItem.js.map