"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isAllowedSideTarget = exports.useAllowedSideTarget = void 0;
/*---------------------------------------------------------------------------------------------
 * 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
 */
const React = require("react");
const DragManager_1 = require("../base/DragManager");
const LayoutStore_1 = require("../base/LayoutStore");
/** Check the docking side against allowed regions
 * @internal
 */
function useAllowedSideTarget(side) {
    const draggedWidgetId = React.useContext(DragManager_1.DraggedWidgetIdContext);
    return (0, LayoutStore_1.useLayout)((state) => isAllowedSideTarget(state, draggedWidgetId, side));
}
exports.useAllowedSideTarget = useAllowedSideTarget;
/** @internal */
function isAllowedSideTarget(state, draggedWidget, side) {
    const draggedTab = state.draggedTab;
    const tabsState = state.tabs;
    const widgetsState = state.widgets;
    let allowedPanelTargets;
    if (draggedTab) {
        const tab = tabsState[draggedTab.tabId];
        allowedPanelTargets = tab.allowedPanelTargets;
    }
    else if (draggedWidget && draggedWidget in widgetsState) {
        // handle a case where DraggedWidgetIdContext exists, but dragged widget is not in WidgetsStateContet
        const widget = widgetsState[draggedWidget];
        const activeTabId = widget.activeTabId;
        const activeTab = tabsState[activeTabId];
        allowedPanelTargets = activeTab.allowedPanelTargets;
        widget.tabs.forEach((tabId) => {
            const tab = tabsState[tabId];
            if (!allowedPanelTargets) {
                allowedPanelTargets = tab.allowedPanelTargets;
            }
            else if (tab.allowedPanelTargets !== undefined) {
                const tabPanelTargets = tab.allowedPanelTargets;
                allowedPanelTargets = allowedPanelTargets.filter((x) => tabPanelTargets.includes(x));
            }
        });
    }
    if (allowedPanelTargets) {
        return allowedPanelTargets.includes(side);
    }
    return true;
}
exports.isAllowedSideTarget = isAllowedSideTarget;
//# sourceMappingURL=useAllowedSideTarget.js.map