"use strict";
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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Avatar = exports.AvatarBase = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const core_1 = require("../core");
const icon_1 = __importDefault(require("../icon"));
const image_1 = __importDefault(require("../image"));
const utils_1 = require("../utils");
const images_1 = require("../utils/images");
const helpers_1 = require("./helpers");
exports.AvatarBase = core_1.styled.spanBox `
  align-items: center;
  display: inline-flex;
  flex-shrink: 0;
  font-weight: medium;
  justify-content: center;
  line-height: normal;
  outline: none;
  overflow: hidden;
  transition-duration: fast;

  &:focus {
    border-color: transparent;
  }

  &[aria-disabled='true'] {
    opacity: 0.5;
    cursor: not-allowed;
    user-select: none;
  }
`;
/** Displays user data, such as name initials or profile image. */
exports.Avatar = (0, core_1.vui)((props, ref) => {
    const _a = (0, core_1.omitThemingProps)(props), { children, className, disabled, getInitials = helpers_1.defaultGetInitials, icon, isInteractive = props.onClick !== undefined, isSquare, name = '', src } = _a, rest = __rest(_a, ["children", "className", "disabled", "getInitials", "icon", "isInteractive", "isSquare", "name", "src"]);
    const _b = (0, core_1.useStyleConfig)('Avatar', props), { activeBg, h, hoverBg, iconSize } = _b, styles = __rest(_b, ["activeBg", "h", "hoverBg", "iconSize"]);
    const borderRadius = isSquare ? (props.size === 'sm' ? '3px' : '6px') : '50%';
    const initials = getInitials(name);
    const interactiveProps = !disabled && isInteractive
        ? {
            cursor: 'pointer',
            hoverBg,
            hoverOpacity: src ? 0.85 : 1,
            activeOpacity: src ? 0.7 : 1,
            activeBg,
            focusRing: 3,
            tabIndex: 0,
            userSelect: 'none',
        }
        : {};
    const aliasedProps = (0, utils_1.filterUndefined)({
        borderRadius: borderRadius,
        'aria-disabled': disabled,
    });
    return ((0, jsx_runtime_1.jsx)(exports.AvatarBase, Object.assign({ borderRadius: borderRadius, className: (0, utils_1.cs)('vui-avatar', className), h: h, ref: ref, w: h }, styles, interactiveProps, aliasedProps, rest, { children: children ? (children) : src ? ((0, jsx_runtime_1.jsx)(LazyImageOrDefault, { borderRadius: borderRadius, icon: icon, iconSize: iconSize, initials: initials, src: src })) : icon ? ((0, jsx_runtime_1.jsx)(icon_1.default, { name: icon, size: iconSize })) : initials ? (initials) : ((0, jsx_runtime_1.jsx)(icon_1.default, { name: "falUser", size: iconSize })) })));
});
function LazyImageOrDefault({ src, borderRadius, icon, iconSize, initials, }) {
    const [imgLoaded, setImgLoaded] = (0, react_1.useState)(false);
    (0, react_1.useEffect)(() => {
        let cancelled = false;
        (0, images_1.loadImageUrl)(src).then(res => {
            if (!cancelled)
                setImgLoaded(res);
        });
        return () => {
            cancelled = true;
        };
    }, [src]);
    return imgLoaded ? ((0, jsx_runtime_1.jsx)(image_1.default, { borderRadius: borderRadius, src: src })) : icon ? ((0, jsx_runtime_1.jsx)(icon_1.default, { name: icon, size: iconSize })) : initials ? ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: initials })) : ((0, jsx_runtime_1.jsx)(icon_1.default, { name: "falUser", size: iconSize }));
}
exports.Avatar.displayName = 'Avatar';
exports.default = exports.Avatar;
//# sourceMappingURL=avatar.js.map