"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.TabTarget = void 0;
require("./TabTarget.scss");
const classnames_1 = require("classnames");
const React = require("react");
const core_bentley_1 = require("@itwin/core-bentley");
const DragManager_1 = require("../base/DragManager");
const NineZone_1 = require("../base/NineZone");
const CursorOverlay_1 = require("../widget-panels/CursorOverlay");
const Widget_1 = require("../widget/Widget");
const ContentRenderer_1 = require("../widget/ContentRenderer");
const useAllowedWidgetTarget_1 = require("./useAllowedWidgetTarget");
const LayoutStore_1 = require("../base/LayoutStore");
const WidgetStateHelpers_1 = require("../state/internal/WidgetStateHelpers");
/** @internal */
function TabTarget() {
    const cursorType = React.useContext(NineZone_1.CursorTypeContext);
    const draggedTab = (0, LayoutStore_1.useLayout)((state) => !!state.draggedTab);
    const draggedWidgetId = React.useContext(DragManager_1.DraggedWidgetIdContext);
    const widgetId = React.useContext(Widget_1.WidgetIdContext);
    (0, core_bentley_1.assert)(!!widgetId);
    const tabIndex = useTabIndex();
    const [ref, targeted] = (0, DragManager_1.useTarget)(useTargetArgs(widgetId, tabIndex));
    const allowedTarget = (0, useAllowedWidgetTarget_1.useAllowedWidgetTarget)(widgetId);
    const hidden = !allowedTarget ||
        (!draggedTab && !draggedWidgetId) ||
        draggedWidgetId === widgetId;
    const className = (0, classnames_1.default)("nz-target-tabTarget", hidden && "nz-hidden", targeted && "nz-targeted", cursorType && (0, CursorOverlay_1.getCursorClassName)(cursorType));
    return React.createElement("div", { className: className, ref: ref });
}
exports.TabTarget = TabTarget;
function useTabIndex() {
    const widgetId = React.useContext(Widget_1.WidgetIdContext);
    const tabId = React.useContext(ContentRenderer_1.TabIdContext);
    (0, core_bentley_1.assert)(!!widgetId);
    return (0, LayoutStore_1.useLayout)((state) => {
        const widget = (0, WidgetStateHelpers_1.getWidgetState)(state, widgetId);
        return widget.tabs.findIndex((id) => id === tabId);
    });
}
function useTargetArgs(widgetId, tabIndex) {
    return React.useMemo(() => {
        return {
            type: "tab",
            widgetId,
            tabIndex,
        };
    }, [widgetId, tabIndex]);
}
//# sourceMappingURL=TabTarget.js.map