"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.useSendBack = exports.SendBack = exports.useSendBackHomeState = exports.getSendBackHomeState = exports.useActiveSendBackWidgetIdStore = void 0;
const React = require("react");
const zustand_1 = require("zustand");
const core_bentley_1 = require("@itwin/core-bentley");
const itwinui_icons_react_1 = require("@itwin/itwinui-icons-react");
const NineZone_1 = require("../base/NineZone");
const LayoutStore_1 = require("../base/LayoutStore");
const FloatingWidget_1 = require("./FloatingWidget");
const PanelState_1 = require("../state/PanelState");
const useIsToolSettingsTab_1 = require("./useIsToolSettingsTab");
const Button_1 = require("../../preview/widget-action-dropdown/Button");
const useMaximizedWidget_1 = require("../../preview/enable-maximized-widget/useMaximizedWidget");
/** @internal */
exports.useActiveSendBackWidgetIdStore = (0, zustand_1.create)(() => undefined);
/** @internal */
function getSendBackHomeState(state, widgetId) {
    const floatingWidget = state.floatingWidgets.byId[widgetId];
    const home = floatingWidget.home;
    const panel = state.panels[home.side];
    const destinationWidgetId = home.widgetId
        ? home.widgetId
        : (0, PanelState_1.getWidgetPanelSectionId)(panel.side, home.widgetIndex);
    let destinationWidget = state.widgets[destinationWidgetId];
    if (!destinationWidget && panel.widgets.length === panel.maxWidgetCount) {
        const id = panel.widgets[home.widgetIndex];
        destinationWidget = state.widgets[id];
    }
    // Widget would be added to an existing panel widget.
    if (destinationWidget) {
        return {
            side: home.side,
            widgetId: destinationWidget.id,
        };
    }
    // Widget would be added to a panel as it's first panel widget.
    if (panel.widgets.length === 0) {
        return {
            side: home.side,
        };
    }
    // Widget would be added to a panel with an existing panel widget as a separate panel widget.
    return {
        side: home.side,
        sectionIndex: destinationWidgetId.endsWith("End") ? 1 : 0,
    };
}
exports.getSendBackHomeState = getSendBackHomeState;
/** @internal */
function useSendBackHomeState() {
    const widgetId = (0, exports.useActiveSendBackWidgetIdStore)((state) => state);
    return (0, LayoutStore_1.useLayout)((state) => widgetId ? getSendBackHomeState(state, widgetId) : undefined);
}
exports.useSendBackHomeState = useSendBackHomeState;
function Icon() {
    const id = (0, FloatingWidget_1.useFloatingWidgetId)();
    (0, core_bentley_1.assert)(!!id);
    const home = (0, LayoutStore_1.useLayout)((state) => state.floatingWidgets.byId[id].home);
    return home.side === "left" ? (React.createElement(itwinui_icons_react_1.SvgDockLeft, null)) : home.side === "right" ? (React.createElement(itwinui_icons_react_1.SvgDockRight, null)) : home.side === "top" ? (React.createElement(itwinui_icons_react_1.SvgDockTop, null)) : (React.createElement(itwinui_icons_react_1.SvgDockBottom, null));
}
/** @internal */
function SendBack() {
    const id = (0, FloatingWidget_1.useFloatingWidgetId)();
    (0, core_bentley_1.assert)(!!id);
    const dispatch = React.useContext(NineZone_1.NineZoneDispatchContext);
    const label = (0, NineZone_1.useLabel)("sendWidgetHomeTitle");
    const setActiveWidgetId = (newId) => exports.useActiveSendBackWidgetIdStore.setState(newId);
    const onClick = () => {
        setActiveWidgetId(undefined);
        dispatch({
            type: "FLOATING_WIDGET_SEND_BACK",
            id,
        });
    };
    const onMouseOver = () => {
        setActiveWidgetId(id);
    };
    const onFocus = () => {
        setActiveWidgetId(id);
    };
    const onMouseOut = () => {
        setActiveWidgetId(undefined);
    };
    const onBlur = () => {
        setActiveWidgetId(undefined);
    };
    const eventHandlers = { onMouseOver, onFocus, onMouseOut, onBlur };
    return (React.createElement(Button_1.ActionButton, { icon: React.createElement(Icon, null), label: label, onClick: onClick, buttonProps: eventHandlers, 
        // TODO: can not pass down event handlers due to iTwinUI type issues.
        menuProps: eventHandlers }));
}
exports.SendBack = SendBack;
/** @internal */
function useSendBack() {
    const maximizedWidget = (0, useMaximizedWidget_1.useIsMaximizedWidget)();
    const isToolSettings = (0, useIsToolSettingsTab_1.useIsToolSettingsTab)();
    const isFloatingWidget = !!(0, FloatingWidget_1.useFloatingWidgetId)();
    const canBeDocked = (0, FloatingWidget_1.useWidgetAllowedToDock)();
    return !maximizedWidget && isFloatingWidget && !isToolSettings && canBeDocked;
}
exports.useSendBack = useSendBack;
//# sourceMappingURL=SendBack.js.map