import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2019-2026 GoodData Corporation
import { FormattedMessage } from "react-intl";
import { invariant } from "ts-invariant";
import { areObjRefsEqual, } from "@gooddata/sdk-model";
import { AttributeFilterConfigurationButton } from "@gooddata/sdk-ui-filters";
import { Button, UiTooltip, useIdPrefixed } from "@gooddata/sdk-ui-kit";
import { useAttributeFilterDisplayFormFromMap } from "../../../_staging/sharedHooks/useAttributeFilterDisplayFormFromMap.js";
import { useDashboardSelector } from "../../../model/react/DashboardStoreProvider.js";
import { selectAllCatalogAttributesMap } from "../../../model/store/catalog/catalogSelectors.js";
import { selectIsApplyFiltersAllAtOnceEnabledAndSet } from "../../../model/store/config/configSelectors.js";
import { selectIsInEditMode } from "../../../model/store/renderMode/renderModeSelectors.js";
function useIsConfigButtonVisible(filterDisplayFormRef, attributes) {
    const isEditMode = useDashboardSelector(selectIsInEditMode);
    const getAttributeFilterDisplayFormFromMap = useAttributeFilterDisplayFormFromMap();
    const filterDisplayForm = getAttributeFilterDisplayFormFromMap(filterDisplayFormRef);
    invariant(filterDisplayForm);
    const attributesMap = useDashboardSelector(selectAllCatalogAttributesMap);
    if (!attributes) {
        return false;
    }
    const attributeByDisplayForm = attributes?.find((attribute) => areObjRefsEqual(attribute.ref, filterDisplayForm.attribute));
    const filterAttribute = attributesMap.get(filterDisplayForm.attribute) || attributeByDisplayForm;
    invariant(filterAttribute);
    return isEditMode;
}
/**
 * @internal
 */
export function CustomAttributeFilterDropdownActions({ isApplyDisabled, isSelectionChanged, onApplyButtonClick, onCancelButtonClick, onConfigurationButtonClick, cancelText, applyText, filterDisplayFormRef, attributes, filterSelectionMode, }) {
    const isEditMode = useDashboardSelector(selectIsInEditMode);
    const withoutApply = useDashboardSelector(selectIsApplyFiltersAllAtOnceEnabledAndSet);
    const isConfigButtonVisible = useIsConfigButtonVisible(filterDisplayFormRef, attributes);
    const isSingleSelect = filterSelectionMode === "single";
    const tooltipId = useIdPrefixed("save-tooltip");
    if (!isEditMode && isSingleSelect) {
        return null;
    }
    return (_jsxs("div", { className: "gd-attribute-filter-dropdown-actions__next", children: [
            _jsx("div", { className: "gd-attribute-filter-dropdown-actions-left-content__next", children: isConfigButtonVisible ? (_jsx(AttributeFilterConfigurationButton, { onConfiguration: onConfigurationButtonClick })) : null }), isSingleSelect ? null : (_jsxs("div", { className: "gd-attribute-filter-dropdown-actions-right-content__next", children: [
                    _jsx(Button, { className: "gd-attribute-filter-cancel-button__next gd-button-secondary gd-button-small cancel-button s-cancel", onClick: onCancelButtonClick, value: cancelText }), withoutApply ? null : (_jsx(UiTooltip, { id: tooltipId, arrowPlacement: "left", offset: 15, content: _jsx(FormattedMessage, { id: "attributesDropdown.noChanges" }), triggerBy: ["hover", "focus"], disabled: isSelectionChanged, optimalPlacement: true, anchor: _jsx(Button, { disabled: isApplyDisabled, className: "gd-attribute-filter-apply-button__next gd-button-action gd-button-small s-apply", onClick: onApplyButtonClick, value: applyText, accessibilityConfig: {
                                ariaDescribedBy: isSelectionChanged ? undefined : tooltipId,
                            }, describedByFromValidation: true }) }))] }))] }));
}
/**
 * @internal
 */
export function CustomConfigureAttributeFilterDropdownActions({ isSaveDisabled, onSaveButtonClick, onCancelButtonClick, cancelText, saveText, }) {
    return (_jsxs("div", { className: "gd-attribute-filter-dropdown-actions__next", children: [
            _jsx("div", { className: "gd-attribute-filter-dropdown-actions-left-content__next" }), _jsxs("div", { className: "gd-attribute-filter-dropdown-actions-right-content__next", children: [
                    _jsx(Button, { className: "gd-attribute-filter-cancel-button__next gd-button-secondary gd-button-small cancel-button s-cancel", onClick: onCancelButtonClick, value: cancelText, title: cancelText }), _jsx(Button, { disabled: isSaveDisabled, className: "gd-attribute-filter-apply-button__next gd-button-action gd-button-small s-apply", onClick: onSaveButtonClick, value: saveText, title: saveText })
                ] })
        ] }));
}
//# sourceMappingURL=CustomDropdownActions.js.map