"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.activateDroppedTab = void 0;
/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
const core_bentley_1 = require("@itwin/core-bentley");
const TabLocation_1 = require("../../layout/state/TabLocation");
const WidgetStateHelpers_1 = require("../../layout/state/internal/WidgetStateHelpers");
/** Reducer factory to activate tab of a dragged widget when dropped in a container.
 * @internal
 */
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 = (0, WidgetStateHelpers_1.setWidgetActiveTabId)(state, widget.id, draggedTabId);
    }
    else if (action.type === "WIDGET_DRAG_END") {
        const draggedWidget = (0, WidgetStateHelpers_1.getWidgetState)(state, action.floatingWidgetId);
        state = reducer(state, action);
        const widget = getWidgetByTabId(state, draggedWidget.activeTabId);
        state = (0, WidgetStateHelpers_1.setWidgetActiveTabId)(state, widget.id, draggedWidget.activeTabId);
    }
    else {
        state = reducer(state, action);
    }
    return state;
};
exports.activateDroppedTab = activateDroppedTab;
function getWidgetByTabId(state, draggedWidgetTabId) {
    const location = (0, TabLocation_1.getTabLocation)(state, draggedWidgetTabId);
    (0, core_bentley_1.assert)(!!location);
    const widget = state.widgets[location.widgetId];
    return widget;
}
//# sourceMappingURL=activateDroppedTab.js.map