import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2021-2026 GoodData Corporation
import { useCallback, useMemo, useState } from "react";
import { useIntl } from "react-intl";
import { DashboardDateFilterConfigModeValues, areObjRefsEqual, } from "@gooddata/sdk-model";
import { DateFilter, getLocalizedIcuDateFormatPattern, } from "@gooddata/sdk-ui-filters";
import { dateFilterOptionToDashboardDateFilter } from "../../../_staging/dashboard/dashboardFilterConverter.js";
import { matchDateFilterToDateFilterOptionWithPreference } from "../../../_staging/dateFilterConfig/dateFilterOptionMapping.js";
import { useDashboardSelector } from "../../../model/react/DashboardStoreProvider.js";
import { selectBackendCapabilities } from "../../../model/store/backendCapabilities/backendCapabilitiesSelectors.js";
import { selectCatalogDateDatasets } from "../../../model/store/catalog/catalogSelectors.js";
import { selectActiveCalendars, selectEnableKDEmptyDateValuesFilter, selectIsApplyFiltersAllAtOnceEnabledAndSet, selectLocale, selectSettings, selectWeekStart, } from "../../../model/store/config/configSelectors.js";
import { selectIsInEditMode } from "../../../model/store/renderMode/renderModeSelectors.js";
import { useCurrentDateFilterConfig } from "../../dragAndDrop/useCurrentDateFilterConfig.js";
import { getVisibilityIcon } from "../utils.js";
import { DateFilterConfigurationBody } from "./configuration/DateFilterConfigurationBody.js";
/**
 * Default implementation of the date filter to use on the dashboard's filter bar.
 *
 * This will use the SDK's DateFilter with the button styled same as we have it today on KD.
 *
 * @alpha
 */
export function DefaultDashboardDateFilter({ filter, workingFilter, onFilterChanged, config, readonly, autoOpen, ButtonComponent, overlayPositionType, tabId, }) {
    const intl = useIntl();
    const settings = useDashboardSelector(selectSettings);
    const capabilities = useDashboardSelector(selectBackendCapabilities);
    const locale = useDashboardSelector(selectLocale);
    const isInEditMode = useDashboardSelector(selectIsInEditMode);
    const weekStart = useDashboardSelector(selectWeekStart);
    const isApplyAllAtOnceEnabledAndSet = useDashboardSelector(selectIsApplyFiltersAllAtOnceEnabledAndSet);
    const activeCalendars = useDashboardSelector(selectActiveCalendars);
    const enableEmptyDateValues = useDashboardSelector(selectEnableKDEmptyDateValuesFilter);
    const allDateDatasets = useDashboardSelector(selectCatalogDateDatasets);
    let defaultDateFilterName;
    if (filter?.dateFilter.dataSet) {
        const dateDataSetName = allDateDatasets.find((ds) => areObjRefsEqual(ds.dataSet.ref, filter?.dateFilter.dataSet))?.dataSet?.title;
        defaultDateFilterName = config.customFilterName ?? dateDataSetName ?? "";
    }
    else {
        defaultDateFilterName =
            config.customFilterName ?? intl.formatMessage({ id: "dateFilterDropdown.title" });
    }
    const { title, mode } = useCurrentDateFilterConfig(filter?.dateFilter.dataSet, defaultDateFilterName, tabId);
    const [lastSelectedOptionId, setLastSelectedOptionId] = useState("");
    const dateFilterOptions = useMemo(() => {
        if (enableEmptyDateValues) {
            return config.dateFilterOptions;
        }
        const { emptyValues: _emptyValues, ...rest } = config.dateFilterOptions;
        return rest;
    }, [config.dateFilterOptions, enableEmptyDateValues]);
    const { dateFilterOption, excludeCurrentPeriod } = useMemo(() => matchDateFilterToDateFilterOptionWithPreference(filter, dateFilterOptions, lastSelectedOptionId), [filter, dateFilterOptions, lastSelectedOptionId]);
    const { dateFilterOption: workingFilterOption, excludeCurrentPeriod: workingExcludeCurrentPeriod } = useMemo(() => matchDateFilterToDateFilterOptionWithPreference(workingFilter, dateFilterOptions, lastSelectedOptionId), [workingFilter, dateFilterOptions, lastSelectedOptionId]);
    const onApply = useCallback((option, exclude) => {
        setLastSelectedOptionId(option.localIdentifier);
        onFilterChanged(dateFilterOptionToDashboardDateFilter(option, exclude, filter?.dateFilter.dataSet, filter?.dateFilter.localIdentifier), option.localIdentifier);
    }, [onFilterChanged, filter?.dateFilter.dataSet, filter?.dateFilter.localIdentifier]);
    const onSelect = useCallback((option, exclude) => {
        setLastSelectedOptionId(option.localIdentifier);
        onFilterChanged(dateFilterOptionToDashboardDateFilter(option, exclude, filter?.dateFilter.dataSet, filter?.dateFilter.localIdentifier), option.localIdentifier, true);
    }, [onFilterChanged, filter?.dateFilter.dataSet, filter?.dateFilter.localIdentifier]);
    const dateFormat = settings.formatLocale
        ? getLocalizedIcuDateFormatPattern(settings.formatLocale)
        : settings.responsiveUiDateFormat;
    const visibilityIcon = getVisibilityIcon(mode, isInEditMode, !!capabilities.supportsHiddenAndLockedFiltersOnUI, intl);
    const hoursMinutesGranularities = ["GDC.time.minute", "GDC.time.hour"];
    const hasHoursMinutesGranularities = hoursMinutesGranularities.every((granularity) => config.availableGranularities.includes(granularity));
    const isTimeForAbsoluteRangeEnabled = !!capabilities.supportsTimeGranularities && hasHoursMinutesGranularities;
    const isConfigurationEnabled = isInEditMode &&
        (!!capabilities.supportsHiddenAndLockedFiltersOnUI || !!capabilities.supportsMultipleDateFilters);
    const FilterConfigurationComponent = useMemo(() => {
        function ElementsSelect(props) {
            return (_jsx(DateFilterConfigurationBody, { ...props, intl: intl, dateDataSet: filter?.dateFilter.dataSet, defaultDateFilterName: defaultDateFilterName }));
        }
        return ElementsSelect;
    }, [intl, filter?.dateFilter.dataSet, defaultDateFilterName]);
    return (_jsx(DateFilter, { excludeCurrentPeriod: excludeCurrentPeriod, selectedFilterOption: dateFilterOption, workingExcludeCurrentPeriod: isApplyAllAtOnceEnabledAndSet ? workingExcludeCurrentPeriod : undefined, workingSelectedFilterOption: isApplyAllAtOnceEnabledAndSet ? workingFilterOption : undefined, dateFilterMode: readonly
            ? DashboardDateFilterConfigModeValues.READONLY
            : DashboardDateFilterConfigModeValues.ACTIVE, filterOptions: dateFilterOptions, availableGranularities: config.availableGranularities, customFilterName: title, onApply: onApply, onSelect: isApplyAllAtOnceEnabledAndSet ? onSelect : undefined, dateFormat: dateFormat, locale: locale, isTimeForAbsoluteRangeEnabled: isTimeForAbsoluteRangeEnabled, isEditMode: isInEditMode, openOnInit: autoOpen, weekStart: weekStart, customIcon: visibilityIcon, showDropDownHeaderMessage: !filter?.dateFilter.dataSet, FilterConfigurationComponent: isConfigurationEnabled ? FilterConfigurationComponent : undefined, withoutApply: isApplyAllAtOnceEnabledAndSet, ButtonComponent: ButtonComponent, overlayPositionType: overlayPositionType, activeCalendars: activeCalendars, enableEmptyDateValues: enableEmptyDateValues, hideDisabledExclude: true }));
}
//# sourceMappingURL=DefaultDashboardDateFilter.js.map