import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2025-2026 GoodData Corporation
import { useState } from "react";
import { FormattedMessage, useIntl } from "react-intl";
import { areObjRefsEqual, dashboardFilterLocalIdentifier, dashboardFilterObjRef, isCatalogAttribute, isCatalogDateDataset, isCatalogMeasure, isDashboardAttributeFilter, isDashboardAttributeFilterItem, isDashboardCommonDateFilter, isDashboardDateFilter, isDashboardMeasureValueFilter, } from "@gooddata/sdk-model";
import { Bubble, BubbleHoverTrigger, Typography, UiButton, UiIconButton, UiTooltip, isActionKey, useIdPrefixed, } from "@gooddata/sdk-ui-kit";
import { AUTOMATION_FILTERS_DIALOG_ID, AUTOMATION_FILTERS_DIALOG_TITLE_ID, AUTOMATION_FILTERS_GROUP_LABEL_ID, } from "../../../../constants/automations.js";
import { AttributesDropdown, } from "../../../../filterBar/attributeFilter/addAttributeFilter/AttributesDropdown.js";
import { useAutomationFilters, useAutomationFiltersByTab } from "../useAutomationFilters.js";
import { useParameterAnnouncements } from "../useParameterAnnouncements.js";
import { AutomationAttributeFilter } from "./AutomationAttributeFilter.js";
import { AutomationDateFilter } from "./AutomationDateFilter.js";
import { AutomationMeasureValueFilter } from "./AutomationMeasureValueFilter.js";
import { AutomationParameter } from "./AutomationParameter.js";
const COLLAPSED_FILTERS_COUNT = 2;
function AutomationCheckboxOrNote({ isDashboardAutomation, storeFilters, handleStoreFiltersChange, handleKeyDown, automationFilterSelectTooltipId, parametersEnabled, }) {
    const intl = useIntl();
    // Keep both message ids as static literals so the i18n extractor sees them.
    const useFiltersTooltip = parametersEnabled ? (_jsx(FormattedMessage, { id: "dialogs.automation.filters.useFiltersMessage.parameters.tooltip" })) : (_jsx(FormattedMessage, { id: "dialogs.automation.filters.useFiltersMessage.tooltip" }));
    return isDashboardAutomation ? (_jsxs("label", { className: "input-checkbox-label gd-automation-filters__use-filters-checkbox s-automation-filters-use-filters-checkbox", children: [
            _jsx("input", { type: "checkbox", className: "input-checkbox s-checkbox", checked: storeFilters, onChange: (e) => handleStoreFiltersChange(e.target.checked), onKeyDown: handleKeyDown, "aria-label": intl.formatMessage({
                    id: "dialogs.automation.filters.useFiltersMessage",
                }) }), _jsxs("span", { className: "input-label-text gd-automation-filters__use-filters-message", children: [
                    _jsx("div", { id: automationFilterSelectTooltipId, className: "sr-only", children: useFiltersTooltip }), _jsx(FormattedMessage, { id: "dialogs.automation.filters.useFiltersMessage" }), _jsx(UiTooltip, { arrowPlacement: "left", triggerBy: ["hover", "focus"], optimalPlacement: true, width: 300, content: useFiltersTooltip, anchor: _jsx(UiIconButton, { icon: "question", variant: "tertiary", size: "xsmall", accessibilityConfig: {
                                ariaLabel: intl.formatMessage({
                                    id: "dialogs.automation.filters.schedule.ariaLabel",
                                }),
                                ariaDescribedBy: automationFilterSelectTooltipId,
                            } }) })
                ] })
        ] })) : (_jsx("div", { className: "gd-automation-filters__message", children: _jsx(FormattedMessage, { id: "dialogs.automation.filters.activeFilters" }) }));
}
function Divider() {
    return _jsx("div", { className: "gd-automation-filters__divider", style: { height: "20px" } });
}
export function AutomationFiltersSelect({ availableFilters = [], selectedFilters = [], onFiltersChange, isDashboardAutomation, storeFilters, onStoreFiltersChange, areFiltersMissing, overlayPositionType, hideTitle = false, showAllFilters = false, disableDateFilters = false, filtersByTab, editedFiltersByTab, onFiltersByTabChange, parameters = [], availableParameters = [], onParameterAdd, onParameterChange, onParameterDelete, parametersByTab, availableParametersByTab, onParameterAddByTab, onParameterChangeByTab, onParameterDeleteByTab, parametersEnabled, }) {
    // Determine rendering mode first
    const shouldRenderByTab = !!filtersByTab && filtersByTab.length > 1;
    // Use appropriate hook based on rendering mode
    const flatFiltersData = useAutomationFilters({
        availableFilters,
        selectedFilters,
        disableDateFilters,
        onFiltersChange,
        onStoreFiltersChange,
    });
    const tabFiltersData = useAutomationFiltersByTab({
        filtersByTab,
        editedFiltersByTab,
        onFiltersByTabChange,
        onStoreFiltersChange,
        disableDateFilters,
    });
    // Use data from the appropriate hook based on rendering mode
    const { commonDateFilterId, lockedFilters, attributeConfigs, dateConfigs, filterAnnouncement, filterGroupRef, makeFilterGroupUnfocusable, setAddFilterButtonRefs, } = shouldRenderByTab && tabFiltersData.processedFiltersByTab ? tabFiltersData : flatFiltersData;
    const { focusAddFilterButton } = flatFiltersData;
    const filters = shouldRenderByTab ? [] : flatFiltersData.visibleFilters;
    const visibleParameters = shouldRenderByTab ? [] : parameters;
    const attributes = shouldRenderByTab ? [] : flatFiltersData.attributes;
    const dateDatasets = shouldRenderByTab ? [] : flatFiltersData.dateDatasets;
    const measures = shouldRenderByTab ? [] : flatFiltersData.measures;
    const handleChangeFilter = shouldRenderByTab
        ? () => { } // Not used in tab mode
        : flatFiltersData.handleChangeFilter;
    const handleDeleteFilter = shouldRenderByTab
        ? () => { } // Not used in tab mode
        : flatFiltersData.handleDeleteFilter;
    const handleAddFilter = shouldRenderByTab
        ? () => { } // Not used in tab mode
        : flatFiltersData.handleAddFilter;
    const handleStoreFiltersChange = shouldRenderByTab
        ? tabFiltersData.handleStoreFiltersChange
        : flatFiltersData.handleStoreFiltersChange;
    const intl = useIntl();
    const [isExpanded, setIsExpanded] = useState(showAllFilters);
    const chipCount = filters.length + visibleParameters.length;
    const isExpandable = !showAllFilters && chipCount > COLLAPSED_FILTERS_COUNT;
    const handleKeyDown = (e) => {
        if (isActionKey(e)) {
            e.preventDefault();
            handleStoreFiltersChange(!storeFilters);
        }
    };
    const automationFilterSelectTooltipId = useIdPrefixed("automation-filter-select-tooltip");
    const parameterDropdownItems = availableParameters.map((parameter) => ({
        type: "parameter",
        ref: parameter.ref,
        title: parameter.title,
    }));
    const isAddButtonDisabled = availableFilters?.length === selectedFilters?.length && availableParameters.length === 0;
    const tooltipTextValues = {
        add: intl.formatMessage({ id: "dialogs.automation.filters.add" }),
        addDisabled: intl.formatMessage({ id: "dialogs.automation.filters.addDisabled" }),
    };
    const tooltipText = isAddButtonDisabled ? tooltipTextValues.addDisabled : tooltipTextValues.add;
    const searchAriaLabel = intl.formatMessage({ id: "dialogs.automation.filters.searchAriaLabel" });
    // Screen-reader announcements for parameter chip add/remove/change
    const { parameterAnnouncement, announceParameterAdded, announceParameterChanged, announceParameterRemoved, } = useParameterAnnouncements();
    const disableFilters = !storeFilters && !!isDashboardAutomation;
    return (_jsxs("div", { className: "gd-input-component gd-notification-channels-automation-filters s-gd-notifications-channels-dialog-automation-filters", children: [hideTitle ? (_jsx("div", { className: "sr-only", id: AUTOMATION_FILTERS_GROUP_LABEL_ID, children: _jsx(FormattedMessage, { id: "dialogs.schedule.email.filters", values: { count: chipCount } }) })) : (_jsx("div", { className: "gd-label", id: AUTOMATION_FILTERS_GROUP_LABEL_ID, children: isExpandable && !shouldRenderByTab ? (_jsxs(BubbleHoverTrigger, { showDelay: 500, hideDelay: 0, children: [
                        _jsx(UiButton, { label: intl.formatMessage({ id: "dialogs.schedule.email.filters" }, { count: chipCount }), variant: "tertiary", onClick: () => setIsExpanded(!isExpanded), iconAfter: isExpanded ? "navigateUp" : "navigateDown", accessibilityConfig: {
                                ariaExpanded: isExpanded,
                                ariaDescription: isExpanded
                                    ? intl.formatMessage({
                                        id: "dialogs.automation.filters.showLess.ariaDescription",
                                    })
                                    : intl.formatMessage({
                                        id: "dialogs.automation.filters.showAll.ariaDescription",
                                    }),
                                iconAriaHidden: true,
                            } }), _jsx(Bubble, { className: "bubble-primary", alignPoints: [{ align: "bc tc" }], children: isExpanded ? (_jsx(FormattedMessage, { id: "dialogs.automation.filters.showLess" })) : (_jsx(FormattedMessage, { id: "dialogs.automation.filters.showAll" })) })
                    ] })) : (_jsx(FormattedMessage, { id: "dialogs.schedule.email.filters", values: { count: chipCount } })) })), _jsxs("div", { className: "gd-automation-filters", children: [showAllFilters ? (_jsxs(_Fragment, { children: [
                            _jsx(AutomationCheckboxOrNote, { isDashboardAutomation: isDashboardAutomation, storeFilters: storeFilters, handleStoreFiltersChange: handleStoreFiltersChange, handleKeyDown: handleKeyDown, automationFilterSelectTooltipId: automationFilterSelectTooltipId, parametersEnabled: parametersEnabled }), _jsx(Divider, {})
                        ] })) : null, _jsxs("div", { className: `gd-automation-filters__wrapper${storeFilters || !isDashboardAutomation ? "" : " gd-automation-filters__wrapper--disabled"}`, children: [disableFilters ? _jsx("div", { className: "gd-automation-filters__overlay" }) : null, shouldRenderByTab && tabFiltersData.processedFiltersByTab ? (
                            // Render filters grouped by tab with dividers between sections
                            _jsx("div", { className: "gd-automation-filters__list gd-automation-filters__list--tabbed", role: "group", "aria-labelledby": AUTOMATION_FILTERS_GROUP_LABEL_ID, ref: filterGroupRef, onBlur: makeFilterGroupUnfocusable, children: tabFiltersData.processedFiltersByTab.map((tab, index) => {
                                    // Check if all filters for this tab are already selected
                                    const tabEditedFilters = editedFiltersByTab?.[tab.tabId] ?? [];
                                    const tabAvailableFilters = filtersByTab?.find((t) => t.tabId === tab.tabId)?.availableFilters ?? [];
                                    const tabParameters = parametersByTab?.[tab.tabId] ?? [];
                                    const tabAvailableParameters = availableParametersByTab?.[tab.tabId] ?? [];
                                    const tabParameterDropdownItems = tabAvailableParameters.map((parameter) => ({
                                        type: "parameter",
                                        ref: parameter.ref,
                                        title: parameter.title,
                                    }));
                                    const isTabAddButtonDisabled = tabEditedFilters.length >= tabAvailableFilters.length &&
                                        tabAvailableParameters.length === 0;
                                    const tabTooltipText = isTabAddButtonDisabled
                                        ? tooltipTextValues.addDisabled
                                        : tooltipTextValues.add;
                                    return (_jsx(AutomationFiltersTabSection, { tabTitle: tab.tabTitle, tabId: tab.tabId, canAddItems: !isTabAddButtonDisabled, filters: tab.visibleFilters, attributeConfigs: tab.attributeConfigs, commonDateFilterId: commonDateFilterId, lockedFilters: tab.lockedFilters, onChange: (filter) => tabFiltersData.handleTabFilterChange(tab.tabId, filter), onDelete: (filter) => tabFiltersData.handleTabFilterDelete(tab.tabId, filter), parameters: tabParameters, onParameterChange: (ref, value) => {
                                            announceParameterChanged(tabParameters, ref, value);
                                            onParameterChangeByTab?.(tab.tabId, ref, value);
                                        }, onParameterDelete: (ref) => {
                                            announceParameterRemoved(tabParameters, ref);
                                            onParameterDeleteByTab?.(tab.tabId, ref);
                                        }, overlayPositionType: overlayPositionType, showDivider: index < tabFiltersData.processedFiltersByTab.length - 1, readonlyFilters: disableFilters, 
                                        // Add button for each tab section
                                        addFilterButton: _jsx(AttributesDropdown, { id: `${AUTOMATION_FILTERS_DIALOG_ID}-${tab.tabId}`, onClose: () => { }, onSelect: (value) => {
                                                tabFiltersData.handleTabFilterAdd(tab.tabId, value, tab.attributes, tab.dateDatasets);
                                            }, parameters: tabParameterDropdownItems, onParameterSelect: (ref) => {
                                                announceParameterAdded(tabAvailableParameters, ref);
                                                onParameterAddByTab?.(tab.tabId, ref);
                                            }, attributes: tab.attributes, dateDatasets: tab.dateDatasets, measures: tab.measures, openOnInit: false, overlayPositionType: overlayPositionType, className: "gd-automation-filters__dropdown s-automation-filters-add-filter-dropdown", getCustomItemTitle: (item) => getCatalogItemCustomTitle(item, availableFilters, tab.dateConfigs), accessibilityConfig: {
                                                ariaLabelledBy: AUTOMATION_FILTERS_DIALOG_TITLE_ID,
                                                searchAriaLabel: searchAriaLabel,
                                            }, DropdownButtonComponent: ({ buttonRef, isOpen, onClick }) => (_jsx(UiTooltip, { arrowPlacement: "left", triggerBy: ["hover", "focus"], content: tabTooltipText, anchor: _jsx(ButtonDisabledFocusableWrapper, { isDisabled: isTabAddButtonDisabled, ariaLabel: tabTooltipText, onRefSet: (element) => setAddFilterButtonRefs(element, buttonRef), children: _jsx(UiIconButton, { icon: "plus", label: tabTooltipText, onClick: onClick, variant: "tertiary", isDisabled: isTabAddButtonDisabled, ref: (element) => {
                                                            if (!isTabAddButtonDisabled) {
                                                                setAddFilterButtonRefs(element, buttonRef);
                                                            }
                                                        }, accessibilityConfig: {
                                                            ariaLabel: tabTooltipText,
                                                            ariaControls: isOpen
                                                                ? `${AUTOMATION_FILTERS_DIALOG_ID}-${tab.tabId}`
                                                                : undefined,
                                                            ariaExpanded: isOpen,
                                                            ariaHaspopup: "dialog",
                                                        } }) }) })), DropdownTitleComponent: () => (_jsx("div", { className: "gd-automation-filters__dropdown-header", children: _jsx(Typography, { tagName: "h3", id: AUTOMATION_FILTERS_DIALOG_TITLE_ID, children: _jsx(FormattedMessage, { id: "dialogs.automation.filters.title" }) }) })), renderNoData: () => (_jsx("div", { className: "gd-automation-filters__dropdown-no-filters", children: _jsx(FormattedMessage, { id: "dialogs.automation.filters.noFilters" }) })) }) }, tab.tabId));
                                }) })) : (
                            // Render flat filter list (original behavior)
                            _jsxs("div", { className: "gd-automation-filters__list", role: "group", "aria-labelledby": AUTOMATION_FILTERS_GROUP_LABEL_ID, ref: filterGroupRef, onBlur: makeFilterGroupUnfocusable, children: [[
                                        ...filters.map((filter) => {
                                            const isCommonDateFilter = isDashboardCommonDateFilter(filter) ||
                                                (isDashboardDateFilter(filter) &&
                                                    commonDateFilterId === filter.dateFilter.localIdentifier);
                                            return (_jsx(AutomationFilter, { filter: filter, attributeConfigs: attributeConfigs, onChange: handleChangeFilter, onDelete: handleDeleteFilter, isCommonDateFilter: isCommonDateFilter, overlayPositionType: overlayPositionType, lockedFilters: lockedFilters, isReadOnly: disableFilters }, dashboardFilterLocalIdentifier(filter)));
                                        }),
                                        ...visibleParameters.map((parameter) => (_jsx(AutomationParameter, { parameter: parameter, onChange: (ref, value) => {
                                                announceParameterChanged(visibleParameters, ref, value);
                                                onParameterChange?.(ref, value);
                                            }, onDelete: (ref) => {
                                                announceParameterRemoved(visibleParameters, ref);
                                                onParameterDelete?.(ref);
                                            }, overlayPositionType: overlayPositionType, isReadOnly: disableFilters }, `parameter-${parameter.ref.identifier}`))),
                                    ].slice(0, showAllFilters || isExpanded ? undefined : COLLAPSED_FILTERS_COUNT), isExpanded || !isExpandable ? (_jsx(AttributesDropdown, { id: AUTOMATION_FILTERS_DIALOG_ID, onClose: () => { }, onSelect: (value) => {
                                            handleAddFilter(value, attributes, dateDatasets);
                                            setIsExpanded(true);
                                        }, parameters: parameterDropdownItems, onParameterSelect: (ref) => {
                                            announceParameterAdded(availableParameters, ref);
                                            onParameterAdd?.(ref);
                                            setIsExpanded(true);
                                            // Restore "+" focus, else it falls to <body> when the chip's list node unmounts.
                                            setTimeout(focusAddFilterButton);
                                        }, attributes: attributes, dateDatasets: dateDatasets, measures: measures, openOnInit: false, overlayPositionType: overlayPositionType, className: "gd-automation-filters__dropdown s-automation-filters-add-filter-dropdown", getCustomItemTitle: (item) => getCatalogItemCustomTitle(item, availableFilters, dateConfigs), accessibilityConfig: {
                                            ariaLabelledBy: AUTOMATION_FILTERS_DIALOG_TITLE_ID,
                                            searchAriaLabel: searchAriaLabel,
                                        }, DropdownButtonComponent: ({ buttonRef, isOpen, onClick }) => (_jsx(UiTooltip, { arrowPlacement: "left", triggerBy: ["hover", "focus"], content: tooltipText, anchor: _jsx(ButtonDisabledFocusableWrapper, { isDisabled: isAddButtonDisabled, ariaLabel: tooltipText, onRefSet: (element) => setAddFilterButtonRefs(element, buttonRef), children: _jsx(UiIconButton, { icon: "plus", label: tooltipText, onClick: onClick, variant: "tertiary", isDisabled: isAddButtonDisabled, ref: (element) => {
                                                        if (!isAddButtonDisabled) {
                                                            setAddFilterButtonRefs(element, buttonRef);
                                                        }
                                                    }, accessibilityConfig: {
                                                        ariaLabel: tooltipText,
                                                        ariaControls: isOpen
                                                            ? AUTOMATION_FILTERS_DIALOG_ID
                                                            : undefined,
                                                        ariaExpanded: isOpen,
                                                        ariaHaspopup: "dialog",
                                                    } }) }) })), DropdownTitleComponent: () => (_jsx("div", { className: "gd-automation-filters__dropdown-header", children: _jsx(Typography, { tagName: "h3", id: AUTOMATION_FILTERS_DIALOG_TITLE_ID, children: _jsx(FormattedMessage, { id: "dialogs.automation.filters.title" }) }) })), renderNoData: () => (_jsx("div", { className: "gd-automation-filters__dropdown-no-filters", children: _jsx(FormattedMessage, { id: "dialogs.automation.filters.noFilters" }) })) })) : null] }))] }), showAllFilters ? null : (_jsxs(_Fragment, { children: [
                            _jsx(Divider, {}), _jsx(AutomationCheckboxOrNote, { isDashboardAutomation: isDashboardAutomation, storeFilters: storeFilters, handleStoreFiltersChange: handleStoreFiltersChange, handleKeyDown: handleKeyDown, automationFilterSelectTooltipId: automationFilterSelectTooltipId, parametersEnabled: parametersEnabled })
                        ] })), areFiltersMissing ? (_jsx("div", { className: "gd-automation-filters__warning-message", children: _jsx(FormattedMessage, { id: "dialogs.automation.filters.missing", values: {
                                b: (chunk) => _jsx("strong", { children: chunk }),
                            } }) })) : null] }), _jsx("div", { className: "sr-only", "aria-live": "polite", "aria-atomic": "true", role: "status", children: filterAnnouncement }), _jsx("div", { className: "sr-only", "aria-live": "polite", "aria-atomic": "true", role: "status", children: parameterAnnouncement })
        ] }));
}
function AutomationFilter({ filter, attributeConfigs, onChange, onDelete, isCommonDateFilter, overlayPositionType, lockedFilters, isReadOnly, tabId, }) {
    if (isDashboardAttributeFilterItem(filter)) {
        const config = isDashboardAttributeFilter(filter)
            ? attributeConfigs.find((attribute) => attribute.localIdentifier === filter.attributeFilter.localIdentifier)
            : undefined;
        const displayAsLabel = config?.displayAsLabel;
        const isLocked = lockedFilters.some((f) => dashboardFilterLocalIdentifier(f) === dashboardFilterLocalIdentifier(filter));
        return (_jsx(AutomationAttributeFilter, { filter: filter, onChange: onChange, onDelete: onDelete, isLocked: isLocked, displayAsLabel: displayAsLabel, overlayPositionType: overlayPositionType, readonly: isReadOnly, tabId: tabId }, dashboardFilterLocalIdentifier(filter)));
    }
    else if (isDashboardDateFilter(filter)) {
        const isLocked = lockedFilters.some((f) => dashboardFilterLocalIdentifier(f) === dashboardFilterLocalIdentifier(filter));
        return (_jsx(AutomationDateFilter, { filter: filter, onChange: onChange, onDelete: onDelete, isLocked: isLocked, isCommonDateFilter: isCommonDateFilter, overlayPositionType: overlayPositionType, readonly: isReadOnly, tabId: tabId }, filter.dateFilter.localIdentifier));
    }
    else if (isDashboardMeasureValueFilter(filter)) {
        const isLocked = lockedFilters.some((f) => dashboardFilterLocalIdentifier(f) === dashboardFilterLocalIdentifier(filter));
        return (_jsx(AutomationMeasureValueFilter, { filter: filter, onChange: onChange, onDelete: onDelete, isLocked: isLocked, overlayPositionType: overlayPositionType, readonly: isReadOnly }, filter.dashboardMeasureValueFilter.localIdentifier));
    }
    return null;
}
/**
 * Renders filters for a single tab with a tab title header.
 * Used when displaying filters grouped by tab for whole dashboard automations.
 */
function AutomationFiltersTabSection({ tabId, tabTitle, filters, attributeConfigs, commonDateFilterId, lockedFilters, onChange, onDelete, parameters, onParameterChange, onParameterDelete, overlayPositionType, showDivider = false, addFilterButton, canAddItems, readonlyFilters, }) {
    const intl = useIntl();
    const tabSectionLabelId = useIdPrefixed(`automation-filters-tab-${tabId}`);
    // Display tab title or fallback to generic "Tab" label
    const displayTitle = tabTitle || intl.formatMessage({ id: "dialogs.automation.filters.tab.untitled" });
    const tabLabel = intl.formatMessage({ id: "dialogs.automation.filters.tab.label" });
    if (filters.length === 0 && parameters.length === 0 && !canAddItems) {
        return null;
    }
    const content = (_jsx("span", { className: "gd-automation-filters__tab-title s-automation-filters-tab-title", children: displayTitle }));
    return (_jsxs(_Fragment, { children: [
            _jsxs("div", { className: "gd-automation-filters__tab-section s-automation-filters-tab-section", role: "group", "aria-labelledby": tabSectionLabelId, children: [
                    _jsxs("div", { className: "gd-automation-filters__tab-header", children: [
                            _jsx("span", { className: "gd-automation-filters__tab-label", children: tabLabel }), _jsx(UiTooltip, { content: content, triggerBy: ["hover", "focus"], optimalPlacement: true, anchor: _jsx("span", { className: "gd-automation-filters__tab-title s-automation-filters-tab-title", id: tabSectionLabelId, children: displayTitle }) })
                        ] }), _jsxs("div", { className: "gd-automation-filters__tab-filters", children: [filters.map((filter) => {
                                const isCommonDateFilter = isDashboardCommonDateFilter(filter) ||
                                    (isDashboardDateFilter(filter) &&
                                        commonDateFilterId === filter.dateFilter.localIdentifier);
                                return (_jsx(AutomationFilter, { filter: filter, attributeConfigs: attributeConfigs, onChange: onChange, onDelete: onDelete, isCommonDateFilter: isCommonDateFilter, overlayPositionType: overlayPositionType, lockedFilters: lockedFilters, isReadOnly: readonlyFilters, tabId: tabId }, dashboardFilterLocalIdentifier(filter)));
                            }), parameters.map((parameter) => (_jsx(AutomationParameter, { parameter: parameter, onChange: onParameterChange, onDelete: onParameterDelete, overlayPositionType: overlayPositionType, isReadOnly: readonlyFilters }, `parameter-${parameter.ref.identifier}`))), addFilterButton] })
                ] }), showDivider ? _jsx("div", { className: "gd-automation-filters__tab-divider" }) : null] }));
}
/**
 * Conditionally wraps a disabled button to make it focusable with keyboard,
 * since disabled buttons cannot be focused
 */
function ButtonDisabledFocusableWrapper({ isDisabled, ariaLabel, onRefSet, children, }) {
    if (!isDisabled) {
        return _jsx(_Fragment, { children: children });
    }
    return (_jsx("div", { tabIndex: 0, "aria-disabled": true, role: "button", "aria-label": ariaLabel, ref: (element) => onRefSet(element), onKeyDown: (e) => {
            if (isActionKey(e)) {
                e.stopPropagation();
            }
        }, children: children }));
}
/**
 * Get custom dashboard filter title for relevant catalog item,
 * so we can show it in the catalog items dropdown.
 */
function getCatalogItemCustomTitle(item, availableFilters, dateConfigs) {
    if (isCatalogAttribute(item)) {
        const availableFilter = availableFilters?.find((filter) => isDashboardAttributeFilter(filter) &&
            areObjRefsEqual(filter.attributeFilter.displayForm, item.defaultDisplayForm.ref));
        return availableFilter?.attributeFilter.title;
    }
    else if (isCatalogDateDataset(item)) {
        const selectedConfig = dateConfigs?.find((config) => areObjRefsEqual(config.dateDataSet, item.dataSet.ref));
        return selectedConfig?.config.filterName;
    }
    else if (isCatalogMeasure(item)) {
        const availableFilter = availableFilters?.find((filter) => isDashboardMeasureValueFilter(filter) &&
            areObjRefsEqual(dashboardFilterObjRef(filter), item.measure.ref));
        return availableFilter?.dashboardMeasureValueFilter.title;
    }
    return undefined;
}
