import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import cx from "classnames";
import { useIntl } from "react-intl";
import { getHierarchyAttributes, getHierarchyTitle, isCatalogAttribute, isCatalogDateAttributeHierarchy, } from "@gooddata/sdk-model";
import { messages } from "@gooddata/sdk-ui";
import { AttributeHierarchyDetailBubble, AttributeHierarchyDetailPanel, } from "@gooddata/sdk-ui-ext";
import { ShortenedText } from "@gooddata/sdk-ui-kit";
import { simplifyText } from "@gooddata/util";
import { useDashboardSelector } from "../../../../../model/react/DashboardStoreProvider.js";
import { selectAllCatalogAttributesMap } from "../../../../../model/store/catalog/catalogSelectors.js";
import { selectCanManageAttributeHierarchy } from "../../../../../model/store/permissions/permissionsSelectors.js";
const TOOLTIP_ALIGN_POINTS = [{ align: "cr cl", offset: { x: 56, y: 0 } }];
function buildAttributeHierarchyDetailItems(hierarchy, allCatalogAttributes) {
    const attributeRefs = getHierarchyAttributes(hierarchy);
    const items = [];
    attributeRefs.forEach((ref) => {
        const attribute = allCatalogAttributes.get(ref);
        if (attribute) {
            items.push({
                title: attribute.attribute.title,
                isDate: !isCatalogAttribute(attribute),
            });
        }
    });
    return items;
}
export function AttributeHierarchyListItem(props) {
    const intl = useIntl();
    const { onClick, item, isDisabled } = props;
    const allCatalogAttributes = useDashboardSelector(selectAllCatalogAttributesMap);
    const canManageAttributeHierarchy = useDashboardSelector(selectCanManageAttributeHierarchy);
    const handleEdit = (event) => {
        if (event) {
            event.stopPropagation();
            event.preventDefault();
        }
        props.onEdit(item);
    };
    const hierarchyTitle = getHierarchyTitle(item);
    const hierarchyListItemClassname = cx("attribute-hierarchy-list-item s-attribute-hierarchy-list-item", `s-${simplifyText(hierarchyTitle)}`, {
        "is-disabled s-is-disable": isDisabled,
    });
    const attributeDetailItems = buildAttributeHierarchyDetailItems(item, allCatalogAttributes);
    const isAdhocDateHierarchy = isCatalogDateAttributeHierarchy(item);
    const isEditEnabled = canManageAttributeHierarchy && !isAdhocDateHierarchy;
    const hierarchyDescription = isAdhocDateHierarchy
        ? intl.formatMessage(messages["hierarchyListGenericDateInfo"])
        : undefined;
    return (_jsxs("div", { className: hierarchyListItemClassname, onClick: onClick, children: [
            _jsx("div", { className: "attribute-hierarchy-list-item-content s-attribute-hierarchy-list-item-content", children: _jsx(ShortenedText, { className: "attribute-hierarchy-title s-attribute-hierarchy-title", tooltipAlignPoints: TOOLTIP_ALIGN_POINTS, displayTooltip: !isDisabled, children: hierarchyTitle }) }), _jsxs("div", { className: "attribute-hierarchy-list-item-actions s-attribute-hierarchy-list-item-actions", children: [isEditEnabled ? (_jsx("div", { className: "gd-icon-pencil attribute-hierarchy-item-edit-button s-attribute-hierarchy-item-edit-button", onClick: handleEdit })) : null, _jsx("div", { className: "attribute-hierarchy-list-item-description s-attribute-hierarchy-list-item-description", children: _jsx(AttributeHierarchyDetailBubble, { children: _jsx(AttributeHierarchyDetailPanel, { title: hierarchyTitle, description: hierarchyDescription, attributes: attributeDetailItems, onEdit: isEditEnabled ? handleEdit : undefined }) }) })
                ] })
        ] }));
}
