import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import cx from "classnames";
import { FormattedMessage, useIntl } from "react-intl";
import { serializeObjRef, } from "@gooddata/sdk-model";
import { DropdownList, NoData } from "@gooddata/sdk-ui-kit";
import { simplifyText } from "@gooddata/util";
import { messages } from "../../../../../../../locales.js";
import { useDashboardSelector } from "../../../../../../../model/react/DashboardStoreProvider.js";
import { useDashboardUserInteraction } from "../../../../../../../model/react/useDashboardUserInteraction.js";
import { selectEnableKDAttributeFilterDatesValidation } from "../../../../../../../model/store/config/configSelectors.js";
import { isDashboardDependentDateFilter, } from "../../../../../../../model/types/dateFilterTypes.js";
import { LimitingItemTitle, UnknownItemTitle } from "../shared/LimitingItem.js";
import { useFilterItems } from "../shared/limitingItemsHook.js";
import { PopupHeader } from "./PopupHeader.js";
import { WithDisabledParentFilterTooltip } from "./WithDisabledParentFilterTooltip.js";
function NoParentFilterFound({ hasNoMatchingData }) {
    const intl = useIntl();
    return (_jsx(NoData, { className: "attribute-filter__limit__popup__no-data", hasNoMatchingData: hasNoMatchingData, notFoundLabel: intl.formatMessage(messages.filterAddValuesLimitPopupSearchNoMatch), noDataLabel: intl.formatMessage(messages.filterAddValuesLimitPopupNoFilters) }));
}
const getFormattedMessage = (commonDateFilterTitle, attributeTitle, parentFilterTitle, isDisabledDateFilterTooltip) => {
    if (isDisabledDateFilterTooltip) {
        return (_jsx(FormattedMessage, { id: "attributesDropdown.valuesLimiting.disableDateFilter", values: {
                dateFilterTitle: commonDateFilterTitle,
                strong: (chunks) => _jsx("strong", { children: chunks }),
            } }));
    }
    else {
        return (_jsx(FormattedMessage, { id: "attributesDropdown.noConnectionMessage", values: {
                childTitle: attributeTitle ?? _jsx(UnknownItemTitle, {}),
                parentTitle: parentFilterTitle ?? _jsx(UnknownItemTitle, {}),
                strong: (chunks) => _jsx("strong", { children: chunks }),
            } }));
    }
};
function ParentFilter({ item: { title, item, type, isDisabled, isDisabledDateFilterTooltip }, attributeTitle, commonDateFilterTitle, onSelect, onClose, onCommonDateSelect, }) {
    const { attributeFilterInteraction } = useDashboardUserInteraction();
    const classNames = cx("gd-list-item attribute-filter__limit__popup__item", `s-dashboard-filter-${simplifyText(title ?? "unknown")}`, {
        "is-disabled": isDisabled,
    });
    const onClick = () => {
        if (type === "commonDate") {
            onCommonDateSelect();
            return;
        }
        if (!isDisabled) {
            onSelect(item);
            onClose();
            if (isDashboardDependentDateFilter(item)) {
                attributeFilterInteraction("attributeFilterLimitDependentDateFilterClicked");
            }
            else {
                attributeFilterInteraction("attributeFilterLimitParentFilterClicked");
            }
        }
    };
    const formattedMessage = getFormattedMessage(commonDateFilterTitle, attributeTitle, title, isDisabledDateFilterTooltip);
    return (_jsx("div", { className: classNames, onClick: onClick, children: _jsx(WithDisabledParentFilterTooltip, { formattedMessage: formattedMessage, isDisabled: !!isDisabled, children: _jsx(LimitingItemTitle, { item: item, title: title }) }) }, serializeObjRef(item)));
}
export function ParentFiltersPage({ attributeTitle, parentFilters, validParentFilters, dependentDateFilters, dependentCommonDateFilter, availableDatasets, commonDateFilterTitle, onSelect, onGoBack, onClose, onCommonDateSelect, }) {
    const intl = useIntl();
    const isEnabledKDAttributeFilterDatesValidation = useDashboardSelector(selectEnableKDAttributeFilterDatesValidation);
    const items = useFilterItems(parentFilters, validParentFilters, dependentDateFilters, availableDatasets, dependentCommonDateFilter, isEnabledKDAttributeFilterDatesValidation, false, intl);
    return (_jsxs(_Fragment, { children: [
            _jsx(PopupHeader, { title: intl.formatMessage(messages.filterAddFilterTitle), onGoBack: onGoBack, onClose: onClose }), _jsx("div", { className: "attribute-filter__limit__popup__list", children: _jsx(DropdownList, { width: 250, isMobile: false, showSearch: false, renderNoData: (props) => _jsx(NoParentFilterFound, { ...props }), items: items, renderItem: ({ item }) => (_jsx(ParentFilter, { attributeTitle: attributeTitle, commonDateFilterTitle: commonDateFilterTitle, item: item, onSelect: onSelect, onClose: onClose, onCommonDateSelect: onCommonDateSelect })) }) })
        ] }));
}
//# sourceMappingURL=ParentFiltersPage.js.map