import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2007-2026 GoodData Corporation
import { useEffect } from "react";
import { invariant } from "ts-invariant";
import { widgetRef } from "@gooddata/sdk-model";
import { queryDateDatasetsForInsight, } from "../../../../model/queries/insights.js";
import { useDashboardSelector } from "../../../../model/react/DashboardStoreProvider.js";
import { useDashboardQueryProcessing } from "../../../../model/react/useDashboardQueryProcessing.js";
import { selectInsightByRef } from "../../../../model/store/insights/insightsSelectors.js";
import { selectIsWidgetLoadingAdditionalDataByWidgetRef } from "../../../../model/store/ui/uiSelectors.js";
import { DateDatasetFilter } from "../../common/configuration/DateDatasetFilter.js";
import { useDateDatasetFilter } from "../../common/configuration/useDateDatasetFilter.js";
export function InsightDateDataSetFilter({ widget }) {
    const { status, run: queryDateDatasets, result, } = useDashboardQueryProcessing({
        queryCreator: queryDateDatasetsForInsight,
    });
    const isLoadingAdditionalData = useDashboardSelector(selectIsWidgetLoadingAdditionalDataByWidgetRef(widgetRef(widget)));
    const insight = useDashboardSelector(selectInsightByRef(widget.insight));
    invariant(insight, "inconsistent state in InsightDateDataSetFilter");
    useEffect(() => {
        // use the whole insight to improve cache hits: other calls to this query also use whole insights
        queryDateDatasets(insight);
    }, [queryDateDatasets, insight]);
    const { handleDateDatasetChanged, shouldOpenDateDatasetPicker } = useDateDatasetFilter(result?.dateDatasets);
    return (_jsx(DateDatasetFilter, { widget: widget, dateFilterCheckboxDisabled: false, isDatasetsLoading: status === "running" || status === "pending" || isLoadingAdditionalData, relatedDateDatasets: result?.dateDatasetsOrdered, isLoadingAdditionalData: isLoadingAdditionalData, shouldOpenDateDatasetPicker: shouldOpenDateDatasetPicker, onDateDatasetChanged: handleDateDatasetChanged }));
}
