"use strict";
/*---------------------------------------------------------------------------------------------
 * 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
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.BackstageComposerItem = exports.BackstageComposerStageLauncher = exports.BackstageComposerActionItem = void 0;
const React = require("react");
const core_bentley_1 = require("@itwin/core-bentley");
const appui_abstract_1 = require("@itwin/appui-abstract");
const core_react_1 = require("@itwin/core-react");
const appui_layout_react_1 = require("@itwin/appui-layout-react");
const BackstageManager_1 = require("./BackstageManager");
const UiFramework_1 = require("../UiFramework");
const FrontstageDef_1 = require("../frontstage/FrontstageDef");
const BackstageItem_1 = require("./BackstageItem");
const isProviderItem_1 = require("../ui-items-provider/isProviderItem");
/** @internal */
function BackstageComposerActionItem({ item, }) {
    const manager = (0, BackstageManager_1.useBackstageManager)();
    const handleClick = React.useCallback(() => {
        manager.close();
        item.execute();
    }, [manager, item]);
    return (React.createElement(appui_layout_react_1.BackstageItem, { itemId: item.id, providerId: (0, isProviderItem_1.isProviderItem)(item) ? item.providerId : undefined, itemPriority: item.itemPriority, groupPriority: item.groupPriority, icon: React.createElement(core_react_1.Icon, { iconSpec: item.icon }), isActive: appui_abstract_1.ConditionalBooleanValue.getValue(item.isActive), isDisabled: appui_abstract_1.ConditionalBooleanValue.getValue(item.isDisabled), onClick: handleClick, subtitle: appui_abstract_1.ConditionalStringValue.getValue(item.subtitle), badge: core_react_1.BadgeUtilities.getComponentForBadgeType(item.badge) }, appui_abstract_1.ConditionalStringValue.getValue(item.label)));
}
exports.BackstageComposerActionItem = BackstageComposerActionItem;
/** @internal */
function BackstageComposerStageLauncher({ item, }) {
    var _a;
    const manager = (0, BackstageManager_1.useBackstageManager)();
    const handleClick = React.useCallback(() => {
        manager.close();
        if (!UiFramework_1.UiFramework.frontstages.hasFrontstage(item.stageId))
            return core_bentley_1.Logger.logError("BackstageComposerStageLauncher", `Frontstage with id '${item.stageId}' not found`);
        void UiFramework_1.UiFramework.frontstages.setActiveFrontstage(item.stageId);
    }, [manager, item.stageId]);
    const activeFrontstageId = (0, FrontstageDef_1.useActiveFrontstageId)();
    const isActive = appui_abstract_1.ConditionalBooleanValue.getValue((_a = item.isActive) !== null && _a !== void 0 ? _a : item.stageId === activeFrontstageId);
    return (React.createElement(appui_layout_react_1.BackstageItem, { itemId: item.id, providerId: (0, isProviderItem_1.isProviderItem)(item) ? item.providerId : undefined, itemPriority: item.itemPriority, groupPriority: item.groupPriority, icon: React.createElement(core_react_1.Icon, { iconSpec: item.icon }), isActive: isActive, isDisabled: appui_abstract_1.ConditionalBooleanValue.getValue(item.isDisabled), onClick: handleClick, subtitle: appui_abstract_1.ConditionalStringValue.getValue(item.subtitle), badge: core_react_1.BadgeUtilities.getComponentForBadgeType(item.badge) }, appui_abstract_1.ConditionalStringValue.getValue(item.label)));
}
exports.BackstageComposerStageLauncher = BackstageComposerStageLauncher;
/** Item of [[BackstageComposer]].
 * @internal
 */
function BackstageComposerItem({ item }) {
    if ((0, BackstageItem_1.isBackstageStageLauncher)(item)) {
        return React.createElement(BackstageComposerStageLauncher, { item: item });
    }
    return React.createElement(BackstageComposerActionItem, { item: item });
}
exports.BackstageComposerItem = BackstageComposerItem;
//# sourceMappingURL=BackstageComposerItem.js.map