// (C) 2021-2026 GoodData Corporation
import { initializeFilterContext } from "./filterContext/filterContextUtils.js";
/**
 * Applies initialization logic to a tab's filterContext if present.
 * Ensures filters have local identifiers and proper ordering.
 */
function initializeTabFilterContext(tab) {
    if (!tab.filterContext?.filterContextDefinition) {
        return tab;
    }
    return {
        ...tab,
        filterContext: initializeFilterContext(tab.filterContext.filterContextDefinition, tab.filterContext.originalFilterContextDefinition, tab.filterContext.attributeFilterDisplayForms, tab.filterContext.filterContextIdentity),
    };
}
/**
 * Sets the tabs and active tab ID in the state.
 * Applies initialization logic to each tab's filter context to ensure proper state structure.
 *
 * @remarks
 * This reducer ensures that all filter contexts are properly initialized with:
 * - Local identifiers for all attribute filters
 * - Proper filter ordering (common date filter first)
 * - Initialized working filter context
 * - Default values for all required fields
 *
 * Note: We return a new state object instead of mutating to ensure Immer properly handles the initialization.
 */
const setTabs = (state, action) => {
    const { tabs, activeTabLocalIdentifier } = action.payload;
    // Process each tab to apply initialization logic from respective config reducers
    const processedTabs = tabs?.map((tab) => {
        // Apply filterContext initialization logic
        return initializeTabFilterContext(tab);
    });
    return {
        ...state,
        tabs: processedTabs,
        activeTabLocalIdentifier,
    };
};
/**
 * Sets only the active tab local identifier without changing the tabs array.
 */
const setActiveTabLocalIdentifier = (state, action) => {
    state.activeTabLocalIdentifier = action.payload;
};
/**
 * Updates a specific tab by identifier.
 * Applies initialization logic to ensure the tab's state is properly structured.
 */
const updateTab = (state, action) => {
    const updatedTab = action.payload;
    if (state.tabs) {
        const index = state.tabs.findIndex((tab) => tab.localIdentifier === updatedTab.localIdentifier);
        if (index !== -1) {
            // Apply initialization logic to the updated tab
            state.tabs[index] = initializeTabFilterContext(updatedTab);
        }
    }
};
/**
 * Removes a tab by its identifier. If the tab does not exist, no changes are made.
 */
const removeTabById = (state, action) => {
    const tabId = action.payload;
    state.tabs = state.tabs?.filter((tab) => tab.localIdentifier !== tabId);
    // Do not touch activeTabId here; handlers set it explicitly based on UX flow.
};
/**
 * Clears all tabs from the state.
 */
const clearTabs = (state) => {
    state.tabs = undefined;
    state.activeTabLocalIdentifier = undefined;
};
/**
 * Sets isRenaming flag on a particular tab.
 * Note: this is internal UI state and not part of the public model
 */
const setTabIsRenaming = (state, action) => {
    const { tabId, isRenaming } = action.payload;
    const tab = state.tabs?.find((tab) => tab.localIdentifier === tabId);
    if (!tab) {
        return;
    }
    tab.isRenaming = isRenaming;
};
/**
 * Renames a tab by id
 */
const renameTab = (state, action) => {
    const { tabId, title } = action.payload;
    const tab = state.tabs?.find((tab) => tab.localIdentifier === tabId);
    if (!tab) {
        return;
    }
    tab.title = title;
};
export const tabsReducers = {
    setTabs,
    setActiveTabLocalIdentifier,
    updateTab,
    removeTabById,
    clearTabs,
    setTabIsRenaming,
    renameTab,
};
//# sourceMappingURL=tabsReducers.js.map