// (C) 2023-2026 GoodData Corporation
import { areObjRefsEqual } from "@gooddata/sdk-model";
import { setDashboardDateFilterConfigMode, setDashboardDateFilterWithDimensionConfigMode, } from "../../../model/commands/dashboard.js";
import { useDashboardSelector } from "../../../model/react/DashboardStoreProvider.js";
import { useDispatchDashboardCommand } from "../../../model/react/useDispatchDashboardCommand.js";
import { selectDateFilterConfigOverrides } from "../../../model/store/tabs/dateFilterConfig/dateFilterConfigSelectors.js";
import { selectDateFilterConfigsOverrides } from "../../../model/store/tabs/dateFilterConfigs/dateFilterConfigsSelectors.js";
import { useDateFilterTitleConfiguration } from "./configuration/hooks/useDateFilterTitleConfiguration.js";
export const useDateFilterConfig = (dateDataSet, defaultDateFilterTitle) => {
    const filterConfig = useDashboardSelector(selectDateFilterConfigOverrides);
    const filterConfigByDimension = useDashboardSelector(selectDateFilterConfigsOverrides);
    const changeConfigMode = useDispatchDashboardCommand(setDashboardDateFilterConfigMode);
    const changeConfigModeForDimension = useDispatchDashboardCommand(setDashboardDateFilterWithDimensionConfigMode);
    const { title, titleChanged, onTitleChange, onTitleUpdate, onTitleReset, onConfigurationClose } = useDateFilterTitleConfiguration(dateDataSet, defaultDateFilterTitle);
    const usedConfig = dateDataSet
        ? filterConfigByDimension.find((config) => areObjRefsEqual(config.dateDataSet, dateDataSet))?.config
        : filterConfig;
    return {
        mode: usedConfig?.mode ?? "active",
        changeConfigMode: (mode) => {
            if (dateDataSet) {
                changeConfigModeForDimension(dateDataSet, mode);
            }
            else {
                changeConfigMode(mode);
            }
        },
        title,
        titleChanged,
        onTitleChange,
        onTitleUpdate,
        onTitleReset,
        onConfigurationClose,
    };
};
