/*---------------------------------------------------------------------------------------------
 * 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 { assert } from "@itwin/core-bentley";
import { Icon } from "@itwin/core-react";
import { IconButton } from "@itwin/itwinui-react";
import { useConditionalValue } from "../../hooks/useConditionalValue";
import { Badge } from "./Badge";
import { ToolbarContext } from "./Toolbar";
/** @internal */
export const Item = React.forwardRef(function Item(props, ref) {
    const { item, ...other } = props;
    const label = useConditionalValue(item.label);
    const isDisabled = useConditionalValue(item.isDisabled);
    const isHidden = useConditionalValue(item.isHidden);
    const iconSpec = useConditionalValue(item.icon);
    const labelProps = useLabelProps();
    if (isHidden)
        return null;
    return (React.createElement(IconButton, { className: props.className, styleType: "borderless", disabled: isDisabled, isActive: item.isActive, label: label, labelProps: labelProps, style: props.style, ref: ref, ...other },
        React.createElement(Icon, { iconSpec: iconSpec }),
        React.createElement(Badge, { badge: item.badge }),
        props.children));
});
/** @internal */
export function useExpandsTo() {
    const context = React.useContext(ToolbarContext);
    assert(!!context);
    const { expandsTo } = context;
    return expandsTo;
}
/** @internal */
export function useLabelProps() {
    const context = React.useContext(ToolbarContext);
    const [internalVisible, setInternalVisible] = React.useState(false);
    const visible = context?.popoverOpen ? false : internalVisible;
    const placement = useExpandsTo();
    return {
        placement,
        visible,
        onVisibleChange: setInternalVisible,
    };
}
//# sourceMappingURL=Item.js.map