// (C) 2023-2026 GoodData Corporation
import { getActiveTab, getTabOrActive } from "../tabsState.js";
/**
 * Changes the config mode for the attribute filter given by its local identifier.
 */
const changeAttributeFilterConfigMode = (state, action) => {
    const { localIdentifier } = action.payload;
    const activeTab = getActiveTab(state);
    if (!activeTab) {
        return;
    }
    if (!activeTab.attributeFilterConfigs) {
        activeTab.attributeFilterConfigs = { attributeFilterConfigs: [] };
    }
    const existingConfig = activeTab.attributeFilterConfigs.attributeFilterConfigs?.find((item) => item.localIdentifier === localIdentifier);
    if (existingConfig) {
        existingConfig.mode = action.payload.mode;
        activeTab.attributeFilterConfigs.attributeFilterConfigs = [
            ...(activeTab.attributeFilterConfigs.attributeFilterConfigs ?? []),
        ];
    }
    else {
        activeTab.attributeFilterConfigs.attributeFilterConfigs = [
            ...(activeTab.attributeFilterConfigs.attributeFilterConfigs || []),
            action.payload,
        ];
    }
};
/**
 * Changes the config mode for the filter given by its local identifier.
 */
const changeDisplayAsLabel = (state, action) => {
    const { localIdentifier, displayAsLabel, tabLocalIdentifier } = action.payload;
    const targetTab = getTabOrActive(state, tabLocalIdentifier);
    if (!targetTab) {
        return;
    }
    if (!targetTab.attributeFilterConfigs) {
        targetTab.attributeFilterConfigs = { attributeFilterConfigs: [] };
    }
    const existingConfig = targetTab.attributeFilterConfigs.attributeFilterConfigs?.find((item) => item.localIdentifier === localIdentifier);
    if (existingConfig) {
        existingConfig.displayAsLabel = displayAsLabel;
        targetTab.attributeFilterConfigs.attributeFilterConfigs = [
            ...(targetTab.attributeFilterConfigs.attributeFilterConfigs ?? []),
        ];
    }
    else {
        targetTab.attributeFilterConfigs.attributeFilterConfigs = [
            ...(targetTab.attributeFilterConfigs.attributeFilterConfigs || []),
            action.payload,
        ];
    }
};
/**
 * Changes the available mode for the attribute filter given by its local identifier.
 */
const changeAttributeFilterSelectionType = (state, action) => {
    const { localIdentifier } = action.payload;
    const activeTab = getActiveTab(state);
    if (!activeTab) {
        return;
    }
    if (!activeTab.attributeFilterConfigs) {
        activeTab.attributeFilterConfigs = { attributeFilterConfigs: [] };
    }
    const existingConfig = activeTab.attributeFilterConfigs.attributeFilterConfigs?.find((item) => item.localIdentifier === localIdentifier);
    if (existingConfig) {
        existingConfig.selectionType = action.payload.selectionType;
        activeTab.attributeFilterConfigs.attributeFilterConfigs = [
            ...(activeTab.attributeFilterConfigs.attributeFilterConfigs ?? []),
        ];
    }
    else {
        activeTab.attributeFilterConfigs.attributeFilterConfigs = [
            ...(activeTab.attributeFilterConfigs.attributeFilterConfigs || []),
            action.payload,
        ];
    }
};
const setAttributeFilterConfigs = (state, action) => {
    const { attributeFilterConfigs } = action.payload;
    const activeTab = getActiveTab(state);
    if (!activeTab) {
        return;
    }
    activeTab.attributeFilterConfigs = attributeFilterConfigs
        ? {
            attributeFilterConfigs,
        }
        : undefined;
};
const removeAttributeFilterConfig = (state, action) => {
    const activeTab = getActiveTab(state);
    if (!activeTab) {
        return;
    }
    if (activeTab.attributeFilterConfigs?.attributeFilterConfigs) {
        activeTab.attributeFilterConfigs.attributeFilterConfigs =
            activeTab.attributeFilterConfigs.attributeFilterConfigs.filter((attributeFilterConfig) => attributeFilterConfig.localIdentifier !== action.payload);
    }
};
export const attributeFilterConfigsReducers = {
    changeAttributeFilterConfigMode,
    changeAttributeFilterSelectionType,
    setAttributeFilterConfigs,
    removeAttributeFilterConfig,
    changeDisplayAsLabel,
};
