/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Widget
 */
import * as React from "react";
import widgetIconSvg from "@bentley/icons-generic/icons/home.svg";
import { Icon, useWidgetOpacityContext } from "@itwin/core-react";
import { AppButton } from "@itwin/appui-layout-react";
import { UiFramework } from "../UiFramework";
/**
 * BackstageAppButton used to toggle display of Backstage and is shown in the corner of the ToolWidget.
 * @public
 */
export function BackstageAppButton(props) {
    const backstageToggleCommand = React.useMemo(() => UiFramework.backstage.getBackstageToggleCommand(props.icon), [props.icon]);
    const backstageLabel = React.useMemo(() => props.label || backstageToggleCommand.tooltip, [backstageToggleCommand.tooltip, props.label]);
    const [icon, setIcon] = React.useState(props.icon ? props.icon : widgetIconSvg);
    const isInitialMount = React.useRef(true);
    const divClassName = "uifw-app-button-small";
    const { onElementRef, proximityScale } = useWidgetOpacityContext();
    const ref = React.useRef(null);
    const handleClick = React.useCallback(() => {
        if (props.execute)
            props.execute();
        else
            backstageToggleCommand.execute();
    }, [backstageToggleCommand, props]);
    React.useEffect(() => {
        if (isInitialMount.current) {
            isInitialMount.current = false;
            onElementRef(ref);
        }
        else {
            setIcon(props.icon ? props.icon : widgetIconSvg);
        }
    }, [props.icon, onElementRef]);
    let buttonProximityScale;
    if (UiFramework.visibility.useProximityOpacity && !UiFramework.isMobile()) {
        buttonProximityScale = proximityScale;
    }
    return (React.createElement("div", { ref: ref, className: divClassName },
        React.createElement(AppButton, { small: true, mouseProximity: buttonProximityScale, onClick: handleClick, icon: React.createElement(Icon, { iconSpec: icon }), title: backstageLabel })));
}
//# sourceMappingURL=BackstageAppButton.js.map