"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 Toolbar
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.GroupMenuItem = exports.usePopoverPlacement = exports.GroupItem = void 0;
const React = require("react");
const core_react_1 = require("@itwin/core-react");
const itwinui_react_1 = require("@itwin/itwinui-react");
const ToolbarItem_1 = require("../../toolbar/ToolbarItem");
const useConditionalProp_1 = require("../../hooks/useConditionalProp");
const ExpandIndicator_1 = require("./ExpandIndicator");
const Item_1 = require("./Item");
const Badge_1 = require("./Badge");
const OverflowButton_1 = require("./OverflowButton");
const Toolbar_1 = require("./Toolbar");
/** @internal */
exports.GroupItem = React.forwardRef(function GroupItem({ item }, ref) {
    const placement = usePopoverPlacement();
    const context = React.useContext(Toolbar_1.ToolbarContext);
    const overflowContext = React.useContext(OverflowButton_1.ToolGroupOverflowContext);
    if (overflowContext) {
        return React.createElement(GroupMenuItem, { item: item, onClose: overflowContext.onClose });
    }
    return (React.createElement(itwinui_react_1.DropdownMenu, { menuItems: (close) => {
            return toMenuItems(item, close);
        }, placement: placement, onVisibleChange: (newVisible) => {
            context?.setPopoverOpen(newVisible);
        } },
        React.createElement(Item_1.Item, { item: item, ref: ref },
            React.createElement(ExpandIndicator_1.ExpandIndicator, null))));
});
/** @internal */
function usePopoverPlacement() {
    const context = React.useContext(Toolbar_1.ToolbarContext);
    if (!context)
        return undefined;
    return `${context.expandsTo}-${context.panelAlignment}`;
}
exports.usePopoverPlacement = usePopoverPlacement;
/** @internal */
function GroupMenuItem({ item, onClose }) {
    // eslint-disable-next-line deprecation/deprecation
    const iconSpec = (0, useConditionalProp_1.useConditionalProp)(item.icon);
    const label = (0, useConditionalProp_1.useConditionalProp)(item.label);
    const isDisabled = (0, useConditionalProp_1.useConditionalProp)(item.isDisabled);
    const isHidden = (0, useConditionalProp_1.useConditionalProp)(item.isHidden);
    const isActive = (0, useConditionalProp_1.useConditionalProp)(item.isActive);
    if (isHidden) {
        return null;
    }
    const subMenuItems = isDisabled ? undefined : toMenuItems(item, onClose);
    const startIcon = item.iconNode ? (React.createElement(React.Fragment, null, item.iconNode)) : (
    // eslint-disable-next-line deprecation/deprecation
    React.createElement(core_react_1.Icon, { iconSpec: iconSpec }));
    return (React.createElement(itwinui_react_1.MenuItem, { startIcon: startIcon, disabled: isDisabled, subMenuItems: subMenuItems, onClick: () => {
            if ((0, ToolbarItem_1.isToolbarActionItem)(item)) {
                item.execute();
                onClose?.();
            }
        }, isSelected: isActive },
        React.createElement(Badge_1.Badge, { badge: item.badge, badgeKind: item.badgeKind }),
        label));
}
exports.GroupMenuItem = GroupMenuItem;
function toMenuItems(item, onClose) {
    if ((0, ToolbarItem_1.isToolbarGroupItem)(item)) {
        return item.items.map((groupItem) => {
            return (React.createElement(GroupMenuItem, { key: groupItem.id, item: groupItem, onClose: onClose }));
        });
    }
    if ((0, ToolbarItem_1.isToolbarCustomItem)(item)) {
        return [
            React.createElement(itwinui_react_1.MenuExtraContent, { key: item.id }, item.panelContent),
        ];
    }
    return [];
}
//# sourceMappingURL=GroupItem.js.map