import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2024-2026 GoodData Corporation
import { useMemo } from "react";
import cx from "classnames";
import { FormattedMessage, useIntl } from "react-intl";
import { serializeObjRef } from "@gooddata/sdk-model";
import { DropdownList, ShortenedText } from "@gooddata/sdk-ui-kit";
import { simplifyText } from "@gooddata/util";
import { messages } from "../../../../../../../locales.js";
import { useDashboardUserInteraction } from "../../../../../../../model/react/useDashboardUserInteraction.js";
import { isDashboardDependentDateFilter, } from "../../../../../../../model/types/dateFilterTypes.js";
import { UnknownItemTitle } from "../shared/LimitingItem.js";
import { useCommonDateItems, useDependentDateFilterTitle, } from "../shared/limitingItemsHook.js";
import { PopupHeader } from "./PopupHeader.js";
import { WithDisabledParentFilterTooltip } from "./WithDisabledParentFilterTooltip.js";
export function DateAttributeListItem({ item: { item, isDisabled, title }, dependentDateFilters, onSelect, onClose, }) {
    const { attributeFilterInteraction } = useDashboardUserInteraction();
    const classNames = useMemo(() => {
        return cx("gd-list-item date-filter__limit__popup__item", `s-${simplifyText(title ?? "unknown")}`, {
            "is-disabled": isDisabled,
        });
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [item]);
    const dataSet = isDashboardDependentDateFilter(item) ? item.dataSet : undefined;
    const dependentDateFilterTitle = useDependentDateFilterTitle(dataSet, dependentDateFilters);
    const onClick = () => {
        if (!isDisabled) {
            onSelect(item);
            onClose();
            attributeFilterInteraction("attributeFilterLimitDependentDateFilterClicked");
        }
    };
    return (_jsx(WithDisabledParentFilterTooltip, { formattedMessage: _jsx(FormattedMessage, { id: "attributesDropdown.valuesLimiting.disableDataSet", values: {
                dateFilterTitle: dependentDateFilterTitle ?? _jsx(UnknownItemTitle, {}),
                strong: (chunks) => _jsx("strong", { children: chunks }),
            } }), isDisabled: !!isDisabled, children: _jsx("div", { className: classNames, onClick: onClick, children: _jsx(ShortenedText, { children: title }) }, serializeObjRef(item)) }));
}
export function DateFiltersPage({ availableDatasets, dependentCommonDateFilter, dependentDateFilters, onSelect, onGoBack, onClose, }) {
    const intl = useIntl();
    const commonDateItems = useCommonDateItems(availableDatasets, dependentCommonDateFilter, dependentDateFilters);
    return (_jsxs(_Fragment, { children: [
            _jsx(PopupHeader, { title: intl.formatMessage(messages.filterAddDateTitle), onGoBack: onGoBack, onClose: onClose }), _jsx("div", { className: "attribute-filter__limit__popup__list--dates", children: _jsx(DropdownList, { width: 250, isMobile: false, showSearch: false, items: commonDateItems, renderItem: ({ item }) => (_jsx(DateAttributeListItem, { dependentDateFilters: dependentDateFilters, item: item, onSelect: onSelect, onClose: onClose })) }) })
        ] }));
}
