import React, { useCallback, useContext } from 'react';
import classnames from 'classnames';
import { Link } from '../Link';
import { Popper } from '../Popper';
import { ProfileCard } from '../ProfileCard';
import { useMdmAction, useMdmUiPath } from '../contexts/MdmModuleContext';
import { UrlGeneratorsContext } from '../contexts/UrlGeneratorsContext';
import { useEntityDetails } from './useEntityDetails';
import { useStyles } from './styles';
export var HierarchyNodeTitle = function (_a) {
    var _b;
    var title = _a.title, isSelected = _a.isSelected, entityUri = _a.entityUri, anchorEl = _a.anchorEl, _c = _a.withEntityDetails, withEntityDetails = _c === void 0 ? true : _c, classNames = _a.classNames;
    var styles = useStyles();
    var generateEntityUrl = useContext(UrlGeneratorsContext).generateEntityUrl;
    var openEntity = useMdmAction('openEntity');
    var uiPath = useMdmUiPath();
    var _d = useEntityDetails(entityUri), isLoading = _d.isLoading, entityDetails = _d.entityDetails, showEntityDetails = _d.showEntityDetails, hideEntityDetails = _d.hideEntityDetails;
    var link = generateEntityUrl({ uiPath: uiPath, uri: entityUri });
    var showPopper = Boolean(anchorEl) && (Boolean(entityDetails) || isLoading);
    var handleClick = useCallback(function () {
        openEntity({ uri: entityUri });
    }, [openEntity, entityUri]);
    return (React.createElement(React.Fragment, null,
        React.createElement("span", { "data-reltio-id": "reltio-hierarchy-node-title", className: classnames(styles.link, (_b = {}, _b[styles.selected] = isSelected, _b), classNames), onMouseEnter: withEntityDetails ? showEntityDetails : undefined, onMouseLeave: withEntityDetails ? hideEntityDetails : undefined },
            React.createElement(Link, { to: link, onClick: handleClick }, title)),
        React.createElement(Popper, { anchorEl: anchorEl, className: styles.popper, open: showPopper, modal: false, placement: "top-start" },
            React.createElement(ProfileCard, { entity: entityDetails, variant: isLoading ? 'loading' : 'normal' }))));
};
