"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 Widget
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.BackstageAppButton = void 0;
const React = require("react");
const home_svg_1 = require("@bentley/icons-generic/icons/home.svg");
const appui_abstract_1 = require("@itwin/appui-abstract");
const core_react_1 = require("@itwin/core-react");
const appui_layout_react_1 = require("@itwin/appui-layout-react");
const BackstageManager_1 = require("../backstage/BackstageManager");
const useFrameworkVersion_1 = require("../hooks/useFrameworkVersion");
const UiFramework_1 = require("../UiFramework");
/**
 * BackstageAppButton used to toggle display of Backstage and is shown in the corner of the ToolWidget.
 * @public
 */
function BackstageAppButton(props) {
    const backstageToggleCommand = React.useMemo(() => BackstageManager_1.BackstageManager.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 : appui_abstract_1.IconSpecUtilities.createWebComponentIconSpec(home_svg_1.default));
    const isInitialMount = React.useRef(true);
    const useSmallAppButton = "1" !== (0, useFrameworkVersion_1.useFrameworkVersion)(); // eslint-disable-line deprecation/deprecation
    const divClassName = useSmallAppButton ? "uifw-app-button-small" : undefined;
    const { onElementRef, proximityScale } = (0, core_react_1.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 : appui_abstract_1.IconSpecUtilities.createWebComponentIconSpec(home_svg_1.default));
        }
    }, [props.icon, onElementRef]);
    let buttonProximityScale;
    if ("1" !== (0, useFrameworkVersion_1.useFrameworkVersion)() && UiFramework_1.UiFramework.visibility.useProximityOpacity && !UiFramework_1.UiFramework.isMobile()) { // eslint-disable-line deprecation/deprecation
        buttonProximityScale = proximityScale;
    }
    return (React.createElement("div", { ref: ref, className: divClassName },
        React.createElement(appui_layout_react_1.AppButton, { small: useSmallAppButton, mouseProximity: buttonProximityScale, onClick: handleClick, icon: React.createElement(core_react_1.Icon, { iconSpec: icon }), title: backstageLabel })));
}
exports.BackstageAppButton = BackstageAppButton;
//# sourceMappingURL=BackstageAppButton.js.map