import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2022-2026 GoodData Corporation
import { useCallback, useState } from "react";
import { useDashboardSelector } from "../../../../model/react/DashboardStoreProvider.js";
import { selectAllCatalogDateDatasetsMap, selectCatalogDateDatasets, } from "../../../../model/store/catalog/catalogSelectors.js";
import { selectEnableUnavailableItemsVisibility } from "../../../../model/store/config/configSelectors.js";
import { selectFilterContextDateFilterByDataSet } from "../../../../model/store/tabs/filterContext/filterContextSelectors.js";
import { DateDatasetDuplicityWarning } from "./DateDatasetDuplicityWarning.js";
import { DateDatasetPicker } from "./DateDatasetPicker.js";
import { DateFilterCheckbox } from "./DateFilterCheckbox.js";
import { useDateFilterConfigurationHandling } from "./useDateFilterConfigurationHandling.js";
import { useIsSelectedDatasetHidden } from "./useIsSelectedDatasetHidden.js";
import { getUnrelatedDateDataset, getUnrelatedDateDatasets } from "./utils.js";
export function DateDatasetFilter({ relatedDateDatasets, widget, dateFilterCheckboxDisabled, dateFromVisualization, isDatasetsLoading, shouldPickDateDataset, shouldOpenDateDatasetPicker, onDateDatasetChanged, isLoadingAdditionalData, }) {
    const enableUnrelatedItemsVisibility = useDashboardSelector(selectEnableUnavailableItemsVisibility);
    const catalogDatasetsMap = useDashboardSelector(selectAllCatalogDateDatasetsMap);
    const selectedDateDataset = widget.dateDataSet && catalogDatasetsMap.get(widget.dateDataSet);
    const dateDatasets = useDashboardSelector(selectCatalogDateDatasets);
    const duplicatedDateDatasetFilter = useDashboardSelector(selectFilterContextDateFilterByDataSet(widget.dateDataSet));
    const { selectedDateDatasetHiddenByObjectAvailability, status: visibleDateDatasetsStatus } = useIsSelectedDatasetHidden(selectedDateDataset?.dataSet.ref);
    const [isDateFilterEnabled, setIsDateFilterEnabled] = useState(!!widget.dateDataSet || shouldPickDateDataset || isLoadingAdditionalData);
    const { handleDateDatasetChanged: handleDateDatasetChangedCore, handleDateFilterEnabled, status, } = useDateFilterConfigurationHandling(widget, relatedDateDatasets, setIsDateFilterEnabled);
    const handleDateDatasetChanged = useCallback((id) => {
        onDateDatasetChanged?.(id);
        handleDateDatasetChangedCore(id);
    }, [handleDateDatasetChangedCore, onDateDatasetChanged]);
    const isFilterLoading = status === "loading";
    const isDropdownLoading = isDatasetsLoading || visibleDateDatasetsStatus === "loading";
    const shouldRenderDateDataSetsDropdown = !dateFilterCheckboxDisabled &&
        !(!isDateFilterEnabled || isFilterLoading) &&
        (relatedDateDatasets?.length ||
            isDropdownLoading ||
            selectedDateDatasetHiddenByObjectAvailability ||
            (dateDatasets.length && enableUnrelatedItemsVisibility));
    const unrelatedDateDataset = relatedDateDatasets &&
        getUnrelatedDateDataset(relatedDateDatasets, selectedDateDataset, selectedDateDatasetHiddenByObjectAvailability);
    const unrelatedDateDatasets = getUnrelatedDateDatasets(dateDatasets, relatedDateDatasets);
    return (_jsxs("div", { children: [
            _jsx(DateFilterCheckbox, { relatedDateDatasets: relatedDateDatasets, widget: widget, dateFilterCheckboxDisabled: dateFilterCheckboxDisabled, dateFilterEnabled: isDateFilterEnabled, isDropdownLoading: isDropdownLoading, isFilterLoading: isFilterLoading, selectedDateDataset: selectedDateDataset, selectedDateDatasetHidden: selectedDateDatasetHiddenByObjectAvailability, onDateDatasetFilterEnabled: handleDateFilterEnabled, enableUnrelatedItemsVisibility: enableUnrelatedItemsVisibility }), !!shouldRenderDateDataSetsDropdown && (_jsx(DateDatasetPicker, { relatedDateDatasets: relatedDateDatasets, dateFromVisualization: dateFromVisualization, widget: widget, selectedDateDataset: selectedDateDataset, selectedDateDatasetHidden: selectedDateDatasetHiddenByObjectAvailability, unrelatedDateDataset: unrelatedDateDataset, onDateDatasetChange: handleDateDatasetChanged, autoOpen: shouldOpenDateDatasetPicker, isLoading: isDropdownLoading, enableUnrelatedItemsVisibility: enableUnrelatedItemsVisibility, unrelatedDateDatasets: unrelatedDateDatasets })), !isDropdownLoading && !!duplicatedDateDatasetFilter && _jsx(DateDatasetDuplicityWarning, {})] }));
}
