// (C) 2021-2026 GoodData Corporation
import { isEmpty } from "lodash-es";
import { getActiveTab } from "../tabsState.js";
const emptyDateFilterConfig = {
    dateFilterConfig: undefined,
    effectiveDateFilterConfig: undefined,
    isUsingDashboardOverrides: undefined,
    dateFilterConfigValidationWarnings: undefined,
};
const setDateFilterConfig = (state, action) => {
    const { dateFilterConfig, effectiveDateFilterConfig, isUsingDashboardOverrides } = action.payload;
    const activeTab = getActiveTab(state);
    if (!activeTab) {
        return;
    }
    // Initialize dateFilterConfig if it doesn't exist
    if (!activeTab.dateFilterConfig) {
        activeTab.dateFilterConfig = {
            ...emptyDateFilterConfig,
        };
    }
    // Update the active tab's date filter config state
    activeTab.dateFilterConfig.dateFilterConfig = dateFilterConfig;
    activeTab.dateFilterConfig.effectiveDateFilterConfig = effectiveDateFilterConfig;
    activeTab.dateFilterConfig.isUsingDashboardOverrides = isUsingDashboardOverrides;
};
const setDateFilterConfigMode = (state, action) => {
    const activeTab = getActiveTab(state);
    if (!activeTab) {
        return;
    }
    if (!activeTab.dateFilterConfig) {
        activeTab.dateFilterConfig = {
            ...emptyDateFilterConfig,
        };
    }
    const mode = action.payload;
    const currentConfig = activeTab.dateFilterConfig.dateFilterConfig;
    const newDateFilterConfig = isEmpty(currentConfig)
        ? {
            mode,
            filterName: "", // will fallback to default name
        }
        : {
            ...currentConfig,
            mode,
        };
    activeTab.dateFilterConfig.dateFilterConfig = newDateFilterConfig;
};
const setDateFilterConfigTitle = (state, action) => {
    const activeTab = getActiveTab(state);
    if (!activeTab) {
        return;
    }
    if (!activeTab.dateFilterConfig) {
        activeTab.dateFilterConfig = {
            ...emptyDateFilterConfig,
        };
    }
    const title = action.payload ?? "";
    const currentConfig = activeTab.dateFilterConfig.dateFilterConfig;
    const newDateFilterConfig = isEmpty(currentConfig)
        ? {
            mode: "active",
            filterName: title,
        }
        : {
            ...currentConfig,
            filterName: title,
        };
    activeTab.dateFilterConfig.dateFilterConfig = newDateFilterConfig;
};
const addDateFilterConfigValidationWarning = (state, action) => {
    const activeTab = getActiveTab(state);
    if (!activeTab) {
        return;
    }
    if (!activeTab.dateFilterConfig) {
        activeTab.dateFilterConfig = {
            ...emptyDateFilterConfig,
        };
    }
    if (!activeTab.dateFilterConfig.dateFilterConfigValidationWarnings) {
        activeTab.dateFilterConfig.dateFilterConfigValidationWarnings = [];
    }
    activeTab.dateFilterConfig.dateFilterConfigValidationWarnings.push(action.payload);
};
const clearDateFilterConfigValidationWarning = (state) => {
    const activeTab = getActiveTab(state);
    if (!activeTab) {
        return;
    }
    if (activeTab.dateFilterConfig) {
        activeTab.dateFilterConfig.dateFilterConfigValidationWarnings = [];
    }
};
const updateDateFilterConfig = (state, action) => {
    const activeTab = getActiveTab(state);
    if (!activeTab) {
        return;
    }
    if (!activeTab.dateFilterConfig) {
        activeTab.dateFilterConfig = {
            ...emptyDateFilterConfig,
        };
    }
    activeTab.dateFilterConfig.dateFilterConfig = action.payload;
};
export const dateFilterConfigReducers = {
    setDateFilterConfig,
    setDateFilterConfigMode,
    setDateFilterConfigTitle,
    updateDateFilterConfig,
    addDateFilterConfigValidationWarning,
    clearDateFilterConfigValidationWarning,
};
//# sourceMappingURL=dateFilterConfigReducers.js.map