function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
import { Image, View, Text } from 'react-native';
import { useLayoutEffect, useMemo, useState } from 'react';
import { defaultAvatarUri, defaultCommunityAvatarUri } from '../../../core/assets';
import { useFile } from '../../hooks';
import { ImageSizeState } from '../../enums';
import useConfig from '../../hooks/useConfig';
import useAuth from '../../../core/hooks/useAuth';
import { useStyles } from './styles';
const AvatarElement = ({
  avatarId,
  pageID = '*',
  componentID = '*',
  elementID,
  targetType,
  defaultAvatar,
  avatarCustomUrl,
  displayName,
  ...props
}) => {
  var _token;
  const {
    client
  } = useAuth();
  const {
    styles
  } = useStyles();
  const fallbackAvatar = useMemo(() => {
    if (defaultAvatar) return defaultAvatar;
    return targetType === 'community' ? defaultCommunityAvatarUri : defaultAvatarUri;
  }, [defaultAvatar, targetType]);
  const [avatarUrl, setAvatarUrl] = useState(null);
  const [loaded, setLoaded] = useState(false);
  const {
    excludes
  } = useConfig();
  const configId = `${pageID}/${componentID}/${elementID}`;
  const {
    getImage
  } = useFile();
  useLayoutEffect(() => {
    const loadAvatar = async () => {
      if (avatarCustomUrl) {
        setAvatarUrl(avatarCustomUrl);
        setLoaded(true);
        return;
      }
      if (!avatarId) {
        setAvatarUrl(null);
        setLoaded(true);
        return;
      }
      const avatar = await getImage({
        fileId: avatarId,
        imageSize: ImageSizeState.small
      });
      setAvatarUrl(avatar ?? null);
      setLoaded(true);
    };
    loadAvatar();
  }, [avatarId, fallbackAvatar, getImage, avatarCustomUrl]);
  if (excludes.includes(configId)) return null;

  // Show first-char placeholder for user avatars when no image is available
  const firstChar = targetType === 'user' && displayName ? displayName.trim().charAt(0).toUpperCase() : null;
  if (loaded && !avatarUrl && firstChar) {
    const style = props.style;
    const size = (style === null || style === void 0 ? void 0 : style.width) ?? 40;
    const radius = (style === null || style === void 0 ? void 0 : style.borderRadius) ?? size / 2;
    return /*#__PURE__*/React.createElement(View, {
      testID: configId,
      accessibilityLabel: configId,
      style: [props.style, styles.avatarPlaceholder, {
        width: size,
        height: size,
        borderRadius: radius
      }]
    }, /*#__PURE__*/React.createElement(Text, {
      style: styles.avatarPlaceholderText
    }, firstChar));
  }
  return /*#__PURE__*/React.createElement(Image, _extends({
    testID: configId,
    accessibilityLabel: configId,
    source: {
      uri: avatarUrl ?? fallbackAvatar,
      headers: {
        Authorization: `Bearer ${client === null || client === void 0 || (_token = client.token) === null || _token === void 0 ? void 0 : _token.accessToken}`
      }
    }
  }, props));
};
export default AvatarElement;
//# sourceMappingURL=AvatarElement.js.map