// (C) 2023-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 ?? [])]);
/**
 * Returns attribute filter config overrides keyed by tab identifier.
 *
 * @internal
 */
export const selectAttributeFilterConfigsOverridesByTab = createSelector(selectTabsArray, (tabs) => {
    if (!tabs.length) {
        return {};
    }
    return tabs.reduce((acc, tab) => {
        const identifier = tab.localIdentifier ?? DEFAULT_TAB_ID;
        acc[identifier] = tab.attributeFilterConfigs?.attributeFilterConfigs ?? [];
        return acc;
    }, {});
});
/**
 * Returns attribute filter configs that is specified on the loaded dashboard.
 *
 * The dashboard-level attribute filter configuration MAY contain overrides and additional configuration to apply
 * on top of the workspace-level attribute filter config. If the dashboard-level overrides are not specified, then
 * the workspace-level config should be taken as-is.
 *
 * @alpha
 */
export const selectAttributeFilterConfigsOverrides = createSelector(selectAttributeFilterConfigsOverridesByTab, selectActiveTabLocalIdentifier, (overridesByTab, activeTabId) => {
    if (!overridesByTab || Object.keys(overridesByTab).length === 0) {
        return [];
    }
    const resolvedActiveTabId = activeTabId ?? Object.keys(overridesByTab)[0];
    return overridesByTab[resolvedActiveTabId] ?? [];
});
/**
 * Get a map of attribute filter modes directly from dashboard attribute filter configurations.
 *
 * @alpha
 */
export const selectAttributeFilterConfigsModeMap = createSelector(selectAttributeFilterConfigsOverrides, (attributeFilterConfigs) => {
    return attributeFilterConfigs.reduce((map, config) => {
        map.set(config.localIdentifier, config.mode ?? DashboardAttributeFilterConfigModeValues.ACTIVE);
        return map;
    }, new Map());
});
/**
 * Get a map of attribute filter modes based on the current screen mode (edit or view).
 *
 * @remarks
 * The mode will always be active in edit mode; otherwise, the mode is taken from the dashboard attribute filter.
 *
 * @alpha
 */
export const selectEffectiveAttributeFiltersModeMap = createSelector(selectIsInEditMode, selectAttributeFilterConfigsOverrides, (isInEditMode, attributeFilterConfigs) => {
    return attributeFilterConfigs.reduce((map, config) => {
        const mode = isInEditMode || !config.mode ? DashboardAttributeFilterConfigModeValues.ACTIVE : config.mode;
        map.set(config.localIdentifier, mode);
        return map;
    }, new Map());
});
/**
 * Get a map of attribute filter displayAsLabels
 *
 * @alpha
 */
export const selectAttributeFilterConfigsDisplayAsLabelMap = createSelector(selectAttributeFilterConfigsOverrides, (attributeFilterConfigs) => {
    return attributeFilterConfigs.reduce((map, config) => {
        map.set(config.localIdentifier, config.displayAsLabel);
        return map;
    }, new Map());
});
/**
 * Get a map of attribute filter modes keyed by tab identifier.
 *
 * @remarks
 * Returns a record where each key is a tab identifier and the value is a Map of attribute filter modes
 * for that tab. Useful when you need to access filter modes for all tabs at once.
 *
 * @internal
 */
export const selectAttributeFilterConfigsModeMapByTab = createSelector(selectAttributeFilterConfigsOverridesByTab, (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;
});
/**
 * Get a map of attribute filter selection types keyed by tab identifier.
 *
 * @remarks
 * Returns a record where each key is a tab identifier and the value is a Map of attribute filter
 * `selectionType` values for that tab. Use when rendering filters for a specific tab (not only the active one).
 *
 * @internal
 */
export const selectAttributeFilterConfigsSelectionTypeMapByTab = createSelector(selectAttributeFilterConfigsOverridesByTab, (overridesByTab) => {
    const result = {};
    for (const [tabIdentifier, configs] of Object.entries(overridesByTab)) {
        result[tabIdentifier] = configs.reduce((map, config) => {
            map.set(config.localIdentifier, config.selectionType);
            return map;
        }, new Map());
    }
    return result;
});
/**
 * Get a map of attribute filter available modes from dashboard attribute filter configurations.
 *
 * @internal
 */
export const selectAttributeFilterConfigsSelectionTypeMap = createSelector(selectAttributeFilterConfigsOverrides, (attributeFilterConfigs) => {
    return attributeFilterConfigs.reduce((map, config) => {
        map.set(config.localIdentifier, config.selectionType);
        return map;
    }, new Map());
});
