var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import React, { memo, useState, useEffect } from 'react';
import classnames from 'classnames';
import NotAvailableImage from '@mui/icons-material/BrokenImage';
import DefaultImage from '../icons/DefaultImage';
import { isEmptyValue } from '@reltio/mdm-sdk';
import { ImageSizesStyles } from './constants';
import { RequestStates } from '../types';
import { useStyles } from './styles';
var Image = function (_a) {
    var _b;
    var src = _a.src, className = _a.className, containerClassName = _a.containerClassName, _c = _a.size, size = _c === void 0 ? ImageSizesStyles.SMALL : _c, _d = _a.overlay, overlay = _d === void 0 ? null : _d, otherProps = __rest(_a, ["src", "className", "containerClassName", "size", "overlay"]);
    var sizeStyles = size || ImageSizesStyles.SMALL;
    var styles = useStyles(sizeStyles);
    var _e = useState(RequestStates.LOADING), imageLoadingState = _e[0], setImageLoadingState = _e[1];
    useEffect(function () {
        setImageLoadingState(RequestStates.LOADING);
    }, [src]);
    var onImageLoadFail = function () {
        setImageLoadingState(RequestStates.ERROR);
    };
    var onImageLoad = function () {
        setImageLoadingState(RequestStates.LOADED);
    };
    var imageIsLoading = imageLoadingState === RequestStates.LOADING;
    var imageIsLoaded = imageLoadingState === RequestStates.LOADED;
    var imageLoadingError = imageLoadingState === RequestStates.ERROR;
    return imageLoadingError ? (React.createElement("div", __assign({ className: classnames(styles.defaultContainer, styles.image, className) }, otherProps),
        React.createElement(NotAvailableImage, { "data-reltio-id": "not-available-image", className: styles.defaultImage }))) : (React.createElement("div", { className: classnames(styles.root, containerClassName) },
        !isEmptyValue(src) && (React.createElement("img", __assign({ src: src, className: classnames(styles.image, className, (_b = {},
                _b[styles.hideImage] = imageIsLoading,
                _b)), onLoad: onImageLoad, onError: onImageLoadFail, alt: 'image' }, otherProps))),
        imageIsLoading && (React.createElement("div", __assign({ className: classnames(styles.defaultContainer, styles.image, className) }, otherProps),
            React.createElement(DefaultImage, { className: styles.defaultImage }))),
        imageIsLoaded && overlay));
};
export default memo(Image);
