/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
import { assert } from "@itwin/core-bentley";
import { getTabLocation } from "../../layout/state/TabLocation";
import { getWidgetState, setWidgetActiveTabId, } from "../../layout/state/internal/WidgetStateHelpers";
/** Reducer factory to activate tab of a dragged widget when dropped in a container.
 * @internal
 */
export const activateDroppedTab = (reducer) => (state, action) => {
    if (action.type === "WIDGET_TAB_DRAG_END") {
        const draggedTabId = action.id;
        state = reducer(state, action);
        const widget = getWidgetByTabId(state, draggedTabId);
        state = setWidgetActiveTabId(state, widget.id, draggedTabId);
    }
    else if (action.type === "WIDGET_DRAG_END") {
        const draggedWidget = getWidgetState(state, action.floatingWidgetId);
        state = reducer(state, action);
        const widget = getWidgetByTabId(state, draggedWidget.activeTabId);
        state = setWidgetActiveTabId(state, widget.id, draggedWidget.activeTabId);
    }
    else {
        state = reducer(state, action);
    }
    return state;
};
function getWidgetByTabId(state, draggedWidgetTabId) {
    const location = getTabLocation(state, draggedWidgetTabId);
    assert(!!location);
    const widget = state.widgets[location.widgetId];
    return widget;
}
//# sourceMappingURL=activateDroppedTab.js.map