"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 appui_abstract_1 = require("@itwin/appui-abstract");
const core_bentley_1 = require("@itwin/core-bentley");
const core_react_1 = require("@itwin/core-react");
const React = require("react");
const UiFramework_1 = require("../UiFramework");
const FrontstageDef_1 = require("../frontstage/FrontstageDef");
const Item_1 = require("../layout/backstage/Item");
const isProviderItem_1 = require("../ui-items-provider/isProviderItem");
const BackstageItem_1 = require("./BackstageItem");
const BackstageManager_1 = require("./BackstageManager");
/** @internal */
function BackstageComposerActionItem({ item, }) {
    const manager = (0, BackstageManager_1.useBackstageManager)();
    const handleClick = React.useCallback(() => {
        manager.close();
        item.execute();
    }, [manager, item]);
    return (React.createElement(Item_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: React.createElement(core_react_1.Badge, { type: item.badge }) }, appui_abstract_1.ConditionalStringValue.getValue(item.label)));
}
exports.BackstageComposerActionItem = BackstageComposerActionItem;
/** @internal */
function BackstageComposerStageLauncher({ item, }) {
    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(item.isActive ?? item.stageId === activeFrontstageId);
    return (React.createElement(Item_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: React.createElement(core_react_1.Badge, { type: 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