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 { useMemo, useState } from 'react';
import { Image, TouchableOpacity } from 'react-native';
import { useTheme } from 'react-native-paper';
import { SvgXml } from 'react-native-svg';
import { category } from '../../../core/assets/icons';
import { Typography } from '../../../core/components/Typography/Typography';
import { useStyles } from './styles';
import ModeratorBadge from '../../elements/ModeratorBadge';
import { isModerator } from '../../utils/permissions';
import { useNavigation } from '@react-navigation/native';
import { getFileUrlWithSize } from '../../utils';
import { useUser } from '../../hooks/objects/user/useUser';
import { FileRepository } from '@amityco/ts-sdk-react-native';
function Avatar({
  uri,
  imageProps,
  iconProps,
  userAvatarProps
}) {
  var _userAvatarProps$user;
  const {
    styles
  } = useStyles();
  const [imageError, setImageError] = useState(false);
  const navigation = useNavigation();
  const handlePress = () => {
    if (userAvatarProps !== null && userAvatarProps !== void 0 && userAvatarProps.viewable && uri) {
      const largeUri = getFileUrlWithSize(uri, 'large');
      navigation.navigate('ImageViewer', {
        images: [{
          uri: largeUri
        }]
      });
    } else if (userAvatarProps !== null && userAvatarProps !== void 0 && userAvatarProps.shouldRedirectToUserProfile) {
      navigation.navigate('UserProfile', {
        userId: userAvatarProps === null || userAvatarProps === void 0 ? void 0 : userAvatarProps.userId
      });
    }
  };
  return uri && !imageError ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(TouchableOpacity, {
    style: imageProps.style,
    activeOpacity: 0.7,
    onPress: handlePress
  }, /*#__PURE__*/React.createElement(Image, _extends({
    source: {
      uri
    }
  }, imageProps, {
    onError: () => setImageError(true)
  })), (userAvatarProps === null || userAvatarProps === void 0 ? void 0 : userAvatarProps.roles) && isModerator(userAvatarProps === null || userAvatarProps === void 0 ? void 0 : userAvatarProps.roles) && /*#__PURE__*/React.createElement(ModeratorBadge, {
    style: styles.moderatorBadge
  }))) : iconProps ? /*#__PURE__*/React.createElement(SvgXml, iconProps) : /*#__PURE__*/React.createElement(TouchableOpacity, {
    style: [styles.defaultUserAvatar, userAvatarProps === null || userAvatarProps === void 0 ? void 0 : userAvatarProps.style],
    activeOpacity: 0.7,
    onPress: handlePress
  }, /*#__PURE__*/React.createElement(Typography.Body, {
    style: [styles.firstChar, userAvatarProps !== null && userAvatarProps !== void 0 && userAvatarProps.firstCharFontSize ? {
      fontSize: userAvatarProps.firstCharFontSize
    } : undefined]
  }, (_userAvatarProps$user = userAvatarProps.userName) === null || _userAvatarProps$user === void 0 || (_userAvatarProps$user = _userAvatarProps$user.trim()) === null || _userAvatarProps$user === void 0 ? void 0 : _userAvatarProps$user.charAt(0).toUpperCase()), (userAvatarProps === null || userAvatarProps === void 0 ? void 0 : userAvatarProps.roles) && isModerator(userAvatarProps.roles) && /*#__PURE__*/React.createElement(ModeratorBadge, {
    style: styles.moderatorBadge
  }));
}
function CategoryAvatar({
  uri,
  imageStyle,
  iconProps: $iconProps
}) {
  const theme = useTheme();
  return /*#__PURE__*/React.createElement(Avatar, {
    uri: uri,
    imageProps: {
      style: imageStyle,
      accessibilityLabel: 'Category Avatar'
    },
    iconProps: {
      xml: category(),
      color: theme.colors.primaryShade2,
      ...$iconProps
    }
  });
}
function UserAvatar({
  uri,
  roles,
  userId,
  userName,
  viewable,
  imageStyle,
  shouldRedirectToUserProfile,
  firstCharFontSize
}) {
  var _user$avatar2;
  const {
    user
  } = useUser({
    userId: userId ?? '',
    enabled: !!userId
  });
  const resolvedUri = useMemo(() => {
    var _user$avatar;
    const url = uri ?? (user === null || user === void 0 ? void 0 : user.avatarCustomUrl) ?? (user === null || user === void 0 || (_user$avatar = user.avatar) === null || _user$avatar === void 0 ? void 0 : _user$avatar.fileUrl);
    if (!url) return undefined;
    return FileRepository.fileUrlWithSize(url, 'small');
  }, [uri, user === null || user === void 0 ? void 0 : user.avatarCustomUrl, user === null || user === void 0 || (_user$avatar2 = user.avatar) === null || _user$avatar2 === void 0 ? void 0 : _user$avatar2.fileUrl]);
  const resolvedName = userName ?? (user === null || user === void 0 ? void 0 : user.displayName) ?? (user === null || user === void 0 ? void 0 : user.userId);
  return /*#__PURE__*/React.createElement(Avatar, {
    uri: resolvedUri,
    userAvatarProps: {
      roles,
      userId,
      userName: resolvedName,
      style: imageStyle,
      shouldRedirectToUserProfile,
      viewable: viewable,
      firstCharFontSize
    },
    imageProps: {
      style: imageStyle,
      accessibilityLabel: `${resolvedName ?? 'User'} Avatar`
    }
  });
}
Avatar.Category = CategoryAvatar;
Avatar.User = UserAvatar;
export default Avatar;
//# sourceMappingURL=Avatar.js.map