"use strict";
/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.useWidget = void 0;
const core_bentley_1 = require("@itwin/core-bentley");
const React = require("react");
const FrontstageDef_1 = require("../frontstage/FrontstageDef");
const InternalFrontstageManager_1 = require("../frontstage/InternalFrontstageManager");
const TabLocation_1 = require("../layout/state/TabLocation");
const ContentRenderer_1 = require("../layout/widget/ContentRenderer");
const Content_1 = require("../widget-panels/Content");
/** Hook that returns information about the Widget in the current context.
 * @returns object that contains the WidgetLocation, WidgetState, and
 * the ability to set the state of the widget.
 * @alpha
 */
function useWidget() {
    const tabId = React.useContext(ContentRenderer_1.TabIdContext);
    (0, core_bentley_1.assert)(!!tabId);
    const frontstage = (0, FrontstageDef_1.useActiveFrontstageDef)();
    (0, core_bentley_1.assert)(!!frontstage);
    const widgetDef = (0, Content_1.useWidgetDef)();
    (0, core_bentley_1.assert)(!!widgetDef);
    const [widgetLocation, setWidgetLocation] = React.useState(findLocation(tabId, frontstage));
    const [state, setState] = React.useState(widgetDef.state);
    React.useEffect(() => {
        return InternalFrontstageManager_1.InternalFrontstageManager.onFrontstageNineZoneStateChangedEvent.addListener(() => {
            if (widgetDef)
                return setState(widgetDef.state);
        });
    }, [frontstage, widgetDef]);
    React.useEffect(() => {
        return InternalFrontstageManager_1.InternalFrontstageManager.onFrontstageNineZoneStateChangedEvent.addListener(() => {
            setWidgetLocation(findLocation(tabId, frontstage));
        });
    }, [frontstage, tabId]);
    const setWidgetState = React.useCallback((widgetState) => {
        widgetDef?.setWidgetState(widgetState);
    }, [widgetDef]);
    return {
        /** State of the Widget */
        state,
        /** Where the widget is located ("docked" | "popout" | "floating"). */
        widgetLocation,
        /** Set widget to different state
         * @param WidgetState that you want to set the widget to.
         */
        setState: setWidgetState,
    };
}
exports.useWidget = useWidget;
function findLocation(locationTabId, frontstage) {
    (0, core_bentley_1.assert)(!!frontstage.nineZoneState);
    const tabLocation = (0, TabLocation_1.getTabLocation)(frontstage.nineZoneState, locationTabId);
    if (tabLocation) {
        if ((0, TabLocation_1.isFloatingTabLocation)(tabLocation)) {
            return "floating";
        }
        if ((0, TabLocation_1.isPopoutTabLocation)(tabLocation)) {
            return "popout";
        }
        if ((0, TabLocation_1.isPanelTabLocation)(tabLocation)) {
            return "docked";
        }
    }
    return "floating";
}
//# sourceMappingURL=useWidget.js.map