import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2023-2026 GoodData Corporation
import { useIntl } from "react-intl";
import { areObjRefsEqual, getHierarchyAttributes, getHierarchyRef, getHierarchyTitle, objRefToString, } from "@gooddata/sdk-model";
import { messages } from "@gooddata/sdk-ui";
import { Dropdown, DropdownButton } from "@gooddata/sdk-ui-kit";
import { useDashboardSelector } from "../../../../../model/react/DashboardStoreProvider.js";
import { useDashboardUserInteraction } from "../../../../../model/react/useDashboardUserInteraction.js";
import { selectAllCatalogAttributeHierarchies } from "../../../../../model/store/catalog/catalogSelectors.js";
import { selectIgnoredDrillDownHierarchiesByWidgetRef } from "../../../../../model/store/tabs/layout/layoutSelectors.js";
import { existBlacklistHierarchyPredicate } from "../../../../../model/utils/attributeHierarchyUtils.js";
import { AttributeHierarchyList } from "./AttributeHierarchyList.js";
const DROPDOWN_ALIGN_POINTS = [
    {
        align: "bl tl",
        offset: {
            x: 0,
            y: 4,
        },
    },
    {
        align: "tl bl",
        offset: {
            x: 0,
            y: -4,
        },
    },
];
const isHierarchyInBlackList = (ignoredDrillDownHierarchies, hierarchy, attributeRef) => {
    return ignoredDrillDownHierarchies.some((ref) => existBlacklistHierarchyPredicate(ref, hierarchy, attributeRef));
};
function buildHierarchyItemList(attributeDescriptor, catalogAttributeHierarchies, ignoredDrillDownHierarchies) {
    const items = [];
    catalogAttributeHierarchies.forEach((hierarchy) => {
        const attributesRef = getHierarchyAttributes(hierarchy);
        const isInBlacklist = isHierarchyInBlackList(ignoredDrillDownHierarchies, hierarchy, attributeDescriptor?.attributeHeader.formOf.ref);
        const indexInHierarchy = attributesRef.findIndex((ref) => objRefToString(ref) === attributeDescriptor?.attributeHeader.formOf.identifier);
        if (indexInHierarchy >= 0 && indexInHierarchy < attributesRef.length - 1) {
            items.push({
                isDisabled: !isInBlacklist,
                hierarchy: hierarchy,
            });
        }
    });
    return items;
}
export function AttributeHierarchyDropdown({ config, attributeDescriptor, onSelect, onOpenAttributeHierarchyDialog, }) {
    const { formatMessage } = useIntl();
    const catalogAttributeHierarchies = useDashboardSelector(selectAllCatalogAttributeHierarchies);
    const ignoredDrillDownHierarchies = useDashboardSelector(selectIgnoredDrillDownHierarchiesByWidgetRef(config.widgetRef));
    const userInteraction = useDashboardUserInteraction();
    const selectedCatalogAttributeHierarchy = config.complete
        ? catalogAttributeHierarchies.find((hierarchy) => {
            const hierarchyRef = getHierarchyRef(hierarchy);
            return areObjRefsEqual(hierarchyRef, config.attributeHierarchyRef);
        })
        : null;
    const items = buildHierarchyItemList(attributeDescriptor, catalogAttributeHierarchies, ignoredDrillDownHierarchies);
    const selectHierarchyTitle = selectedCatalogAttributeHierarchy && getHierarchyTitle(selectedCatalogAttributeHierarchy);
    const buttonText = selectHierarchyTitle ?? formatMessage(messages["drilldownSelectHierarchy"]);
    return (_jsx(Dropdown, { className: "drill-config-hierarchy-target-select s-drill-config-hierarchy-target-select", closeOnMouseDrag: false, closeOnParentScroll: true, closeOnOutsideClick: true, alignPoints: DROPDOWN_ALIGN_POINTS, renderButton: ({ isOpen, toggleDropdown }) => (_jsx(DropdownButton, { value: buttonText, onClick: toggleDropdown, isOpen: isOpen, isSmall: false, iconLeft: config.complete ? "gd-icon-attribute-hierarchy" : "", className: "gd-button-small s-visualization-button-target-hierarchy" })), renderBody: ({ closeDropdown }) => {
            return (_jsx(AttributeHierarchyList, { hierarchies: items, closeDropdown: closeDropdown, onOpenAttributeHierarchyDialog: onOpenAttributeHierarchyDialog, onSelect: (hierarchy) => {
                    onSelect(hierarchy);
                    closeDropdown();
                    userInteraction.attributeHierarchiesInteraction("attributeHierarchyDrillDownSelected");
                } }));
        } }));
}
//# sourceMappingURL=AttributeHierarchyDropdown.js.map