import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2022-2026 GoodData Corporation
import { useCallback, useMemo } from "react";
import classNames from "classnames";
import { isDashboardAttributeFilter, } from "@gooddata/sdk-model";
import { convertDashboardAttributeFilterElementsUrisToValues } from "../../../_staging/dashboard/legacyFilterConvertors.js";
import { useDashboardSelector } from "../../../model/react/DashboardStoreProvider.js";
import { selectSupportsElementUris } from "../../../model/store/backendCapabilities/backendCapabilitiesSelectors.js";
import { selectIsApplyFiltersAllAtOnceEnabledAndSet } from "../../../model/store/config/configSelectors.js";
import { selectIsInEditMode } from "../../../model/store/renderMode/renderModeSelectors.js";
import { selectCanAddMoreFilters } from "../../../model/store/tabs/filterContext/filterContextSelectors.js";
import { DraggableFilterDropZoneHint } from "../draggableFilterDropZone/DraggableFilterDropZoneHint.js";
import { useDashboardDrag } from "../useDashboardDrag.js";
export function DraggableAttributeFilter({ FilterComponent, filter, workingFilter, filterIndex, autoOpen, readonly, displayAsLabel, onAttributeFilterChanged, onAttributeFilterAdded, onAttributeFilterClose, }) {
    const isInEditMode = useDashboardSelector(selectIsInEditMode);
    const [{ isDragging }, dragRef] = useDashboardDrag({
        dragItem: {
            type: "attributeFilter",
            filter,
            filterIndex,
        },
        canDrag: isInEditMode,
    }, [filter, filterIndex, isInEditMode]);
    const supportElementUris = useDashboardSelector(selectSupportsElementUris);
    const canAddMoreAttributeFilters = useDashboardSelector(selectCanAddMoreFilters);
    const filterToUse = useMemo(() => {
        if (supportElementUris || !isDashboardAttributeFilter(filter)) {
            return filter;
        }
        return convertDashboardAttributeFilterElementsUrisToValues(filter);
    }, [filter, supportElementUris]);
    const workingFilterToUse = useMemo(() => {
        const effective = workingFilter ?? filter;
        if (supportElementUris || !isDashboardAttributeFilter(effective)) {
            return effective;
        }
        return convertDashboardAttributeFilterElementsUrisToValues(effective);
    }, [workingFilter, filter, supportElementUris]);
    const onClose = useCallback(() => {
        onAttributeFilterClose();
    }, [onAttributeFilterClose]);
    const showDropZones = isInEditMode && !isDragging;
    const isApplyAllAtOnceEnabledAndSet = useDashboardSelector(selectIsApplyFiltersAllAtOnceEnabledAndSet);
    return (_jsxs("div", { className: "draggable-attribute-filter", children: [showDropZones ? (_jsx(DraggableFilterDropZoneHint, { hintPosition: "prev", targetIndex: filterIndex, onAddAttributePlaceholder: onAttributeFilterAdded, acceptPlaceholder: canAddMoreAttributeFilters })) : null, _jsx("div", { className: classNames("dash-filters-notdate", "dash-filters-attribute", {
                    "dash-filter-is-edit-mode": isInEditMode,
                    "is-dragging": isDragging,
                }), ref: dragRef, children: _jsx(FilterComponent, { filter: filterToUse, workingFilter: isApplyAllAtOnceEnabledAndSet ? workingFilterToUse : undefined, onFilterChanged: onAttributeFilterChanged, isDraggable: isInEditMode, readonly: readonly, displayAsLabel: displayAsLabel, autoOpen: autoOpen, onClose: onClose }) }), showDropZones ? (_jsx(DraggableFilterDropZoneHint, { hintPosition: "next", targetIndex: filterIndex, onAddAttributePlaceholder: onAttributeFilterAdded, acceptPlaceholder: canAddMoreAttributeFilters })) : null] }));
}
//# sourceMappingURL=DraggableAttributeFilter.js.map