import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2022-2026 GoodData Corporation
import { Fragment, useCallback, useMemo } from "react";
import cx from "classnames";
import { FormattedMessage } from "react-intl";
import { simplifyText } from "@gooddata/util";
import { useDashboardSelector } from "../../../../../../model/react/DashboardStoreProvider.js";
import { selectIsCircularDependency } from "../../../../../../model/store/tabs/filterContext/filterContextSelectors.js";
import { ConnectingAttributeDropdown } from "../connectingAttribute/ConnectingAttributeDropdown.js";
import { ParentFiltersDisabledItem } from "./ParentFiltersDisabledItem.js";
export function ParentFiltersListItem({ item: { isSelected, localIdentifier, selectedConnectingAttribute }, onClick, currentFilterLocalId, connectingAttributes, onConnectingAttributeSelect, title, disabled, }) {
    const isCircularDependency = useDashboardSelector(selectIsCircularDependency(currentFilterLocalId, localIdentifier));
    const isDisabled = isCircularDependency || !connectingAttributes.length;
    const showConnectingAttributeSelect = isSelected && connectingAttributes.length > 1;
    const activeItemClasses = useMemo(() => {
        return cx("gd-list-item attribute-filter-item s-attribute-filter-dropdown-configuration-item", `s-${simplifyText(title)}`, {
            "is-selected": isSelected,
        });
    }, [isSelected, title]);
    const onSelect = useCallback(() => {
        const connectingAttributeRefs = connectingAttributes.map((attr) => attr.ref);
        onClick(localIdentifier, !isSelected, connectingAttributeRefs);
    }, [isSelected, localIdentifier, onClick, connectingAttributes]);
    if (isDisabled) {
        return (_jsx(ParentFiltersDisabledItem, { hasConnectingAttributes: !!connectingAttributes.length, itemTitle: title, itemLocalId: currentFilterLocalId }));
    }
    return (_jsxs(Fragment, { children: [
            _jsx("div", { className: activeItemClasses, onClick: () => !disabled && onSelect(), title: title, children: _jsxs("label", { className: "input-checkbox-label configuration-item-title", children: [
                        _jsx("input", { type: "checkbox", className: "input-checkbox s-checkbox", checked: isSelected, disabled: disabled, readOnly: true }), _jsx("span", { className: "input-label-text", children: title }), showConnectingAttributeSelect ? (_jsxs("span", { className: "addon", children: ["\u00A0",
                                _jsx(FormattedMessage, { id: "attributesDropdown.attributeNameWithData" })
                            ] })) : null] }) }), showConnectingAttributeSelect ? (_jsx(ConnectingAttributeDropdown, { itemLocalId: localIdentifier, connectingAttributes: connectingAttributes, selectedConnectingAttributeRef: selectedConnectingAttribute || connectingAttributes[0].ref, onSelect: onConnectingAttributeSelect })) : null] }));
}
