import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2026 GoodData Corporation
import { useState } from "react";
import cx from "classnames";
import { FormattedMessage } from "react-intl";
import { areObjRefsEqual, isDashboardMeasureValueFilterReference, objRefToString, } from "@gooddata/sdk-model";
import { UiTooltip } from "@gooddata/sdk-ui-kit";
import { simplifyText } from "@gooddata/util";
import { FilterConfigurationItemLabel } from "./FilterConfigurationItemLabel.js";
import { useMeasureValueFilterConfigurationHandling } from "./useMeasureValueFilterConfigurationHandling.js";
export function MeasureValueFilterConfigurationItem({ widget, measureRef, title, isCompatible, isBlockedByRankingFilter, isMissingMeasure = false, }) {
    const [isApplied, setIsApplied] = useState(() => !widget.ignoreDashboardFilters.some((reference) => {
        if (!isDashboardMeasureValueFilterReference(reference)) {
            return false;
        }
        return areObjRefsEqual(reference.measure, measureRef);
    }));
    const { handleIgnoreChanged, status } = useMeasureValueFilterConfigurationHandling(widget, measureRef, setIsApplied);
    const isLoading = status === "loading";
    const isIncompatible = !isCompatible;
    const isDisabled = isIncompatible || isBlockedByRankingFilter || isMissingMeasure;
    const isChecked = !isDisabled && isApplied;
    const classNames = cx(`s-${simplifyText(title)}`, "input-checkbox-label", "filter-by-item", "attribute-filter-by-item", "measure-value-filter-by-item", {
        "filter-by-item-incompatible": isIncompatible || isBlockedByRankingFilter,
        "filter-by-item-missing-measure": isMissingMeasure,
        disabled: isDisabled,
    });
    const uniqueKey = objRefToString(measureRef);
    const checkbox = (_jsx(FilterConfigurationItemLabel, { dataTestId: `s-${simplifyText(title)}`, className: classNames, uniqueKey: uniqueKey, title: title, isApplied: isChecked, isLoading: isLoading, disabled: isDisabled, onChange: handleIgnoreChanged }));
    if (!isDisabled) {
        return _jsx("div", { children: checkbox });
    }
    // Priority: missing measure > incompatible > blocked by ranking filter
    const tooltipContent = isMissingMeasure ? (_jsx(FormattedMessage, { id: "configurationPanel.mvfMissingMeasure" })) : isIncompatible ? (_jsx(FormattedMessage, { id: "configurationPanel.vizCantBeFilteredByMeasureValueFilter" })) : (_jsx(FormattedMessage, { id: "configurationPanel.mvfBlockedByRankingFilter" }));
    return (_jsx("div", { children: _jsx(UiTooltip, { arrowPlacement: "top-start", triggerBy: ["hover", "focus"], optimalPlacement: true, anchor: checkbox, content: tooltipContent }) }));
}
