"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntityAvatar = void 0;
var react_1 = __importDefault(require("react"));
var classnames_1 = __importDefault(require("classnames"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var Avatar_1 = __importDefault(require("@mui/material/Avatar"));
var no_photo_svg_1 = require("./no-photo.svg");
var MdmModuleContext_1 = require("../contexts/MdmModuleContext");
var EntityAvatar = function (_a) {
    var entity = _a.entity, avatarClassName = _a.avatarClassName, imageClassName = _a.imageClassName;
    var metadata = (0, MdmModuleContext_1.useMdmMetadata)();
    var absoluteImagePath = (0, MdmModuleContext_1.useMdmAbsoluteImagePath)() || '';
    var entityImageSource = (0, mdm_sdk_1.getEntityImage)(metadata, absoluteImagePath, entity);
    var fallbackImage = (0, mdm_sdk_1.getAbsoluteImageUrl)(absoluteImagePath, (0, mdm_sdk_1.getEntityTypeImage)(metadata, entity.type));
    var onImageLoadFail = function (e) {
        e.target.onError = null;
        e.target.src = (0, mdm_sdk_1.svg2Url)(no_photo_svg_1.DEFAULT_PROFILE_IMAGE);
    };
    return (react_1.default.createElement(Avatar_1.default, { className: (0, classnames_1.default)(avatarClassName, imageClassName), src: entityImageSource, imgProps: { onError: onImageLoadFail } },
        react_1.default.createElement("img", { className: imageClassName, src: fallbackImage, alt: "fallback entity avatar", onError: onImageLoadFail })));
};
exports.EntityAvatar = EntityAvatar;
