"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HierarchyNodeTitle = void 0;
var react_1 = __importStar(require("react"));
var classnames_1 = __importDefault(require("classnames"));
var Link_1 = require("../Link");
var Popper_1 = require("../Popper");
var ProfileCard_1 = require("../ProfileCard");
var MdmModuleContext_1 = require("../contexts/MdmModuleContext");
var UrlGeneratorsContext_1 = require("../contexts/UrlGeneratorsContext");
var useEntityDetails_1 = require("./useEntityDetails");
var styles_1 = require("./styles");
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 = (0, styles_1.useStyles)();
    var generateEntityUrl = (0, react_1.useContext)(UrlGeneratorsContext_1.UrlGeneratorsContext).generateEntityUrl;
    var openEntity = (0, MdmModuleContext_1.useMdmAction)('openEntity');
    var uiPath = (0, MdmModuleContext_1.useMdmUiPath)();
    var _d = (0, useEntityDetails_1.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 = (0, react_1.useCallback)(function () {
        openEntity({ uri: entityUri });
    }, [openEntity, entityUri]);
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement("span", { "data-reltio-id": "reltio-hierarchy-node-title", className: (0, classnames_1.default)(styles.link, (_b = {}, _b[styles.selected] = isSelected, _b), classNames), onMouseEnter: withEntityDetails ? showEntityDetails : undefined, onMouseLeave: withEntityDetails ? hideEntityDetails : undefined },
            react_1.default.createElement(Link_1.Link, { to: link, onClick: handleClick }, title)),
        react_1.default.createElement(Popper_1.Popper, { anchorEl: anchorEl, className: styles.popper, open: showPopper, modal: false, placement: "top-start" },
            react_1.default.createElement(ProfileCard_1.ProfileCard, { entity: entityDetails, variant: isLoading ? 'loading' : 'normal' }))));
};
exports.HierarchyNodeTitle = HierarchyNodeTitle;
