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