// (C) 2024-2026 GoodData Corporation
import { useMemo } from "react";
import { invariant } from "ts-invariant";
import { dashboardAttributeFilterItemFilterElementsByDate, objRefToString, } from "@gooddata/sdk-model";
import { useDashboardSelector } from "../../../model/react/DashboardStoreProvider.js";
import { selectIsApplyFiltersAllAtOnceEnabledAndSet } from "../../../model/store/config/configSelectors.js";
import { selectFilterContextDateFilter, selectFilterContextDateFilterForTab, selectFilterContextDateFiltersWithDimension, selectFilterContextDateFiltersWithDimensionForTab, selectWorkingFilterContextDateFilter, selectWorkingFilterContextDateFilterForTab, selectWorkingFilterContextDateFiltersWithDimension, selectWorkingFilterContextDateFiltersWithDimensionForTab, } from "../../../model/store/tabs/filterContext/filterContextSelectors.js";
/**
 * Returns depdent date filtering input props for {@link @gooddata/sdk-ui-filters#AttributeFilter} for particular dashboard attribute filter.
 *
 * @param filter - dashboard filter to get the depdendent date filter-related data
 * @param tabId - optional tab identifier to read filter context from a specific tab instead of the active one
 *
 * @beta
 */
export const useDependentDateFilters = (filter, tabId) => {
    const isApplyAllAtOnceEnabledAndSet = useDashboardSelector(selectIsApplyFiltersAllAtOnceEnabledAndSet);
    // Use tab-specific selectors when tabId is provided
    const allAppliedDateFilters = useDashboardSelector(tabId
        ? selectFilterContextDateFiltersWithDimensionForTab(tabId)
        : selectFilterContextDateFiltersWithDimension);
    const allWorkingDateFilters = useDashboardSelector(tabId
        ? selectWorkingFilterContextDateFiltersWithDimensionForTab(tabId)
        : selectWorkingFilterContextDateFiltersWithDimension);
    const allDateFilters = isApplyAllAtOnceEnabledAndSet ? allWorkingDateFilters : allAppliedDateFilters;
    // Use tab-specific selectors for common date filter when tabId is provided
    const commonAppliedDateFilter = useDashboardSelector(tabId ? selectFilterContextDateFilterForTab(tabId) : selectFilterContextDateFilter);
    const commonWorkingDateFilter = useDashboardSelector(tabId ? selectWorkingFilterContextDateFilterForTab(tabId) : selectWorkingFilterContextDateFilter);
    const commonDateFilter = isApplyAllAtOnceEnabledAndSet
        ? commonWorkingDateFilter
        : commonAppliedDateFilter;
    const commonDateFilterWithAllTime = getCommonDateFilterWithAllTime(commonDateFilter);
    const filterElementsByDate = dashboardAttributeFilterItemFilterElementsByDate(filter);
    const dependentDateFilters = useMemo(() => {
        return filterElementsByDate?.map((dependentDateFilter) => {
            if (dependentDateFilter.isCommonDate) {
                const commonDashboardDateFilter = {
                    dateFilter: {
                        type: commonDateFilterWithAllTime?.dateFilter.type,
                        granularity: commonDateFilterWithAllTime?.dateFilter.granularity,
                        from: commonDateFilterWithAllTime?.dateFilter.from,
                        to: commonDateFilterWithAllTime?.dateFilter.to,
                        dataSet: {
                            identifier: dependentDateFilter.filterLocalIdentifier,
                            type: "dataSet",
                        },
                    },
                };
                return commonDashboardDateFilter;
            }
            else {
                const matchingFilter = allDateFilters.find((filter) => objRefToString(filter.dateFilter.dataSet) ===
                    dependentDateFilter.filterLocalIdentifier);
                invariant(matchingFilter); // if this blows up, the state is inconsistent
                return matchingFilter;
            }
        });
    }, [allDateFilters, commonDateFilterWithAllTime, filterElementsByDate]);
    return {
        dependentDateFilters,
    };
};
const getCommonDateFilterWithAllTime = (commonDate) => {
    if (commonDate) {
        return commonDate;
    }
    else {
        return {
            dateFilter: {
                granularity: "GDC.time.date",
                type: "relative",
            },
        };
    }
};
//# sourceMappingURL=useDependentDateFilters.js.map