"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.FloatingTab = exports.FloatingTabProvider = void 0;
require("./FloatingTab.scss");
const classnames_1 = require("classnames");
const React = require("react");
const DragManager_1 = require("../base/DragManager");
const NineZone_1 = require("../base/NineZone");
const LayoutStore_1 = require("../base/LayoutStore");
const ContentRenderer_1 = require("./ContentRenderer");
/** @internal */
function FloatingTabProvider() {
    const tabNode = React.useContext(NineZone_1.TabNodeContext);
    const id = (0, LayoutStore_1.useLayout)((state) => {
        const draggedTab = state.draggedTab;
        return draggedTab?.tabId;
    });
    if (!id)
        return null;
    return React.createElement(ContentRenderer_1.TabIdContext.Provider, { value: id }, tabNode);
}
exports.FloatingTabProvider = FloatingTabProvider;
/** Component that displays a floating tab.
 * @internal
 */
function FloatingTab({ icon }) {
    const { id, position } = (0, LayoutStore_1.useLayout)((state) => {
        const draggedTab = state.draggedTab;
        return {
            id: draggedTab?.tabId,
            position: draggedTab?.position,
        };
    }, true);
    const label = (0, LayoutStore_1.useLayout)((state) => {
        const tabId = state.draggedTab?.tabId;
        const tab = tabId ? state.tabs[tabId] : undefined;
        return tab?.label;
    });
    const dispatch = React.useContext(NineZone_1.NineZoneDispatchContext);
    const onDrag = React.useCallback((dragBy) => {
        id &&
            dispatch({
                type: "WIDGET_TAB_DRAG",
                dragBy,
            });
    }, [dispatch, id]);
    const onDragEnd = React.useCallback((target) => {
        id &&
            dispatch({
                type: "WIDGET_TAB_DRAG_END",
                id,
                target,
            });
    }, [dispatch, id]);
    (0, DragManager_1.useDragTab)({
        tabId: id || "",
        onDrag,
        onDragEnd,
    });
    const showWidgetIcon = React.useContext(NineZone_1.ShowWidgetIconContext);
    const style = position && {
        transform: `translate(${position.x}px, ${position.y}px)`,
    };
    const className = (0, classnames_1.default)("nz-widget-floatingTab", !position && "nz-hidden");
    return (React.createElement("div", { className: className, style: style },
        showWidgetIcon && icon,
        React.createElement("span", null, label)));
}
exports.FloatingTab = FloatingTab;
//# sourceMappingURL=FloatingTab.js.map