"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _reactNative = require("react-native");
var _reactNativePaper = require("react-native-paper");
var _reactNativeSvg = require("react-native-svg");
var _icons = require("../../../core/assets/icons");
var _Typography = require("../../../core/components/Typography/Typography");
var _styles = require("./styles");
var _ModeratorBadge = _interopRequireDefault(require("../../elements/ModeratorBadge"));
var _permissions = require("../../utils/permissions");
var _native = require("@react-navigation/native");
var _utils = require("../../utils");
var _useUser = require("../../hooks/objects/user/useUser");
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
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); }
function Avatar({
  uri,
  imageProps,
  iconProps,
  userAvatarProps
}) {
  var _userAvatarProps$user;
  const {
    styles
  } = (0, _styles.useStyles)();
  const [imageError, setImageError] = (0, _react.useState)(false);
  const navigation = (0, _native.useNavigation)();
  const handlePress = () => {
    if (userAvatarProps !== null && userAvatarProps !== void 0 && userAvatarProps.viewable && uri) {
      const largeUri = (0, _utils.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(_reactNative.TouchableOpacity, {
    style: imageProps.style,
    activeOpacity: 0.7,
    onPress: handlePress
  }, /*#__PURE__*/React.createElement(_reactNative.Image, _extends({
    source: {
      uri
    }
  }, imageProps, {
    onError: () => setImageError(true)
  })), (userAvatarProps === null || userAvatarProps === void 0 ? void 0 : userAvatarProps.roles) && (0, _permissions.isModerator)(userAvatarProps === null || userAvatarProps === void 0 ? void 0 : userAvatarProps.roles) && /*#__PURE__*/React.createElement(_ModeratorBadge.default, {
    style: styles.moderatorBadge
  }))) : iconProps ? /*#__PURE__*/React.createElement(_reactNativeSvg.SvgXml, iconProps) : /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
    style: [styles.defaultUserAvatar, userAvatarProps === null || userAvatarProps === void 0 ? void 0 : userAvatarProps.style],
    activeOpacity: 0.7,
    onPress: handlePress
  }, /*#__PURE__*/React.createElement(_Typography.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) && (0, _permissions.isModerator)(userAvatarProps.roles) && /*#__PURE__*/React.createElement(_ModeratorBadge.default, {
    style: styles.moderatorBadge
  }));
}
function CategoryAvatar({
  uri,
  imageStyle,
  iconProps: $iconProps
}) {
  const theme = (0, _reactNativePaper.useTheme)();
  return /*#__PURE__*/React.createElement(Avatar, {
    uri: uri,
    imageProps: {
      style: imageStyle,
      accessibilityLabel: 'Category Avatar'
    },
    iconProps: {
      xml: (0, _icons.category)(),
      color: theme.colors.primaryShade2,
      ...$iconProps
    }
  });
}
function UserAvatar({
  uri,
  roles,
  userId,
  userName,
  viewable,
  imageStyle,
  shouldRedirectToUserProfile,
  firstCharFontSize
}) {
  var _user$avatar2;
  const {
    user
  } = (0, _useUser.useUser)({
    userId: userId ?? '',
    enabled: !!userId
  });
  const resolvedUri = (0, _react.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 _tsSdkReactNative.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;
var _default = exports.default = Avatar;
//# sourceMappingURL=Avatar.js.map