// (C) 2026 GoodData Corporation
import { createSelector } from "@reduxjs/toolkit";
import { DashboardAttributeFilterConfigModeValues, } from "@gooddata/sdk-model";
import { selectIsInEditMode } from "../../renderMode/renderModeSelectors.js";
import { selectActiveTabLocalIdentifier, selectTabs } from "../tabsSelectors.js";
import { DEFAULT_TAB_ID } from "../tabsState.js";
const selectTabsArray = createSelector(selectTabs, (tabs) => [...(tabs ?? [])]);
/**
 * @internal
 */
export const selectMeasureValueFilterConfigsOverridesByTab = createSelector(selectTabsArray, (tabs) => {
    if (!tabs.length) {
        return {};
    }
    return tabs.reduce((acc, tab) => {
        const identifier = tab.localIdentifier ?? DEFAULT_TAB_ID;
        acc[identifier] = tab.measureValueFilterConfigs?.measureValueFilterConfigs ?? [];
        return acc;
    }, {});
});
/**
 * @alpha
 */
export const selectMeasureValueFilterConfigsOverrides = createSelector(selectMeasureValueFilterConfigsOverridesByTab, selectActiveTabLocalIdentifier, (overridesByTab, activeTabId) => {
    if (!overridesByTab || Object.keys(overridesByTab).length === 0) {
        return [];
    }
    const resolvedActiveTabId = activeTabId ?? Object.keys(overridesByTab)[0];
    return overridesByTab[resolvedActiveTabId] ?? [];
});
/**
 * @alpha
 */
export const selectMeasureValueFilterConfigsModeMap = createSelector(selectMeasureValueFilterConfigsOverrides, (measureValueFilterConfigs) => {
    return measureValueFilterConfigs.reduce((map, config) => {
        map.set(config.localIdentifier, config.mode ?? DashboardAttributeFilterConfigModeValues.ACTIVE);
        return map;
    }, new Map());
});
/**
 * @alpha
 */
export const selectEffectiveMeasureValueFiltersModeMap = createSelector(selectIsInEditMode, selectMeasureValueFilterConfigsOverrides, (isInEditMode, measureValueFilterConfigs) => {
    return measureValueFilterConfigs.reduce((map, config) => {
        const mode = isInEditMode || !config.mode ? DashboardAttributeFilterConfigModeValues.ACTIVE : config.mode;
        map.set(config.localIdentifier, mode);
        return map;
    }, new Map());
});
/**
 * @internal
 */
export const selectMeasureValueFilterConfigsModeMapByTab = createSelector(selectMeasureValueFilterConfigsOverridesByTab, (overridesByTab) => {
    const result = {};
    for (const [tabId, configs] of Object.entries(overridesByTab)) {
        result[tabId] = configs.reduce((map, config) => {
            map.set(config.localIdentifier, config.mode ?? DashboardAttributeFilterConfigModeValues.ACTIVE);
            return map;
        }, new Map());
    }
    return result;
});
//# sourceMappingURL=measureValueFilterConfigsSelectors.js.map