/*---------------------------------------------------------------------------------------------
 * 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 { isToolbarActionItem, isToolbarCustomItem, isToolbarGroupItem, } from "../../toolbar/ToolbarItem.js";
import { ToolGroup } from "./ToolGroup.js";
import { ActionItem } from "./ActionItem.js";
import { GroupItem } from "./GroupItem.js";
import { CustomItem } from "./CustomItem.js";
/** @internal */
export function Toolbar({ items, expandsTo = "bottom", panelAlignment = "start", onItemExecuted, }) {
    const [popoverOpen, setPopoverOpen] = React.useState(false);
    const orientation = getOrientation(expandsTo);
    return (React.createElement(ToolbarContext.Provider, { value: React.useMemo(() => ({
            expandsTo,
            orientation,
            panelAlignment,
            popoverOpen,
            setPopoverOpen,
            onItemExecuted,
        }), [
            expandsTo,
            orientation,
            panelAlignment,
            popoverOpen,
            setPopoverOpen,
            onItemExecuted,
        ]) },
        React.createElement(ToolGroup, null, items.map((item, index) => {
            const nextItem = items.length > index + 1 ? items[index + 1] : undefined;
            const renderSeparator = nextItem
                ? item.groupPriority !== nextItem.groupPriority
                : false;
            return (React.createElement(ItemRenderer, { key: item.id, item: item, renderSeparator: renderSeparator }));
        }))));
}
const ItemRenderer = React.forwardRef(function ItemRenderer({ item, renderSeparator }, forwardedRef) {
    let itemElement;
    if (isToolbarActionItem(item)) {
        itemElement = React.createElement(ActionItem, { ref: forwardedRef, item: item });
    }
    if (isToolbarGroupItem(item)) {
        itemElement = React.createElement(GroupItem, { ref: forwardedRef, item: item });
    }
    if (isToolbarCustomItem(item)) {
        itemElement = React.createElement(CustomItem, { ref: forwardedRef, item: item });
    }
    return (React.createElement(ToolbarItemContext.Provider, { value: { renderSeparator } }, itemElement));
});
/** @internal */
export const ToolbarContext = React.createContext(undefined);
ToolbarContext.displayName = "uifw:ToolbarContext";
/** @internal */
export const ToolbarItemContext = React.createContext(undefined);
ToolbarContext.displayName = "uifw:ToolbarContext";
function getOrientation(expandsTo) {
    switch (expandsTo) {
        case "left":
        case "right":
            return "vertical";
        case "top":
        case "bottom":
            return "horizontal";
    }
}
//# sourceMappingURL=Toolbar.js.map