// (C) 2023-2026 GoodData Corporation
import { invariant } from "ts-invariant";
import { areObjRefsEqual } from "@gooddata/sdk-model";
import { getActiveTab } from "../tabsState.js";
/**
 * Changes the config mode for the date filter given by its data seet.
 */
const changeDateFilterConfigsMode = (state, action) => {
    const { dataSet } = action.payload;
    const activeTab = getActiveTab(state);
    if (!activeTab) {
        return;
    }
    if (!activeTab.dateFilterConfigs) {
        activeTab.dateFilterConfigs = { dateFilterConfigs: [] };
    }
    const effectiveFilter = activeTab.dateFilterConfigs.dateFilterConfigs?.find((item) => areObjRefsEqual(item.dateDataSet, dataSet));
    if (effectiveFilter) {
        if (action.payload.mode) {
            effectiveFilter.config.mode = action.payload.mode;
            activeTab.dateFilterConfigs.dateFilterConfigs = [
                ...(activeTab.dateFilterConfigs.dateFilterConfigs ?? []),
            ];
        }
    }
    else {
        const newConfigItem = {
            dateDataSet: dataSet,
            config: {
                filterName: "",
                mode: action.payload.mode || "active",
            },
        };
        activeTab.dateFilterConfigs.dateFilterConfigs = [
            ...(activeTab.dateFilterConfigs.dateFilterConfigs || []),
            newConfigItem,
        ];
    }
};
/**
 * Changes the config title for the filter given by its data set.
 */
const changeDateFilterConfigsTitle = (state, action) => {
    const { dataSet } = action.payload;
    invariant(dataSet, "Date data set needs to be provided.");
    const activeTab = getActiveTab(state);
    if (!activeTab) {
        return;
    }
    if (!activeTab.dateFilterConfigs) {
        activeTab.dateFilterConfigs = { dateFilterConfigs: [] };
    }
    const effectiveFilter = activeTab.dateFilterConfigs.dateFilterConfigs?.find((item) => areObjRefsEqual(item.dateDataSet, dataSet));
    if (effectiveFilter) {
        effectiveFilter.config.filterName = action.payload.title ?? "";
        activeTab.dateFilterConfigs.dateFilterConfigs = [
            ...(activeTab.dateFilterConfigs.dateFilterConfigs ?? []),
        ];
    }
    else {
        const newConfigItem = {
            dateDataSet: dataSet,
            config: {
                filterName: action.payload.title ?? "",
                mode: "active",
            },
        };
        activeTab.dateFilterConfigs.dateFilterConfigs = [
            ...(activeTab.dateFilterConfigs.dateFilterConfigs || []),
            newConfigItem,
        ];
    }
};
const setDateFilterConfigs = (state, action) => {
    const { dateFilterConfigs } = action.payload;
    const activeTab = getActiveTab(state);
    if (!activeTab) {
        return;
    }
    activeTab.dateFilterConfigs = dateFilterConfigs
        ? {
            dateFilterConfigs,
        }
        : undefined;
};
const removeDateFilterConfigs = (state, action) => {
    if (!state.tabs || !state.activeTabLocalIdentifier) {
        return;
    }
    const activeTabIndex = state.tabs?.findIndex((tab) => tab.localIdentifier === state.activeTabLocalIdentifier);
    if (activeTabIndex === -1) {
        return;
    }
    const activeTab = state.tabs[activeTabIndex];
    if (activeTab.dateFilterConfigs?.dateFilterConfigs) {
        activeTab.dateFilterConfigs.dateFilterConfigs = activeTab.dateFilterConfigs.dateFilterConfigs.filter((dateFilterConfig) => !areObjRefsEqual(dateFilterConfig.dateDataSet, action.payload));
    }
};
export const dateFilterConfigsReducers = {
    changeDateFilterConfigsMode,
    setDateFilterConfigs,
    removeDateFilterConfigs,
    changeDateFilterConfigsTitle,
};
