"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _reactNative = require("react-native");
var _react = require("react");
var _assets = require("../../../core/assets");
var _hooks = require("../../hooks");
var _enums = require("../../enums");
var _useConfig = _interopRequireDefault(require("../../hooks/useConfig"));
var _useAuth = _interopRequireDefault(require("../../../core/hooks/useAuth"));
var _styles = require("./styles");
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); }
const AvatarElement = ({
  avatarId,
  pageID = '*',
  componentID = '*',
  elementID,
  targetType,
  defaultAvatar,
  avatarCustomUrl,
  displayName,
  ...props
}) => {
  var _token;
  const {
    client
  } = (0, _useAuth.default)();
  const {
    styles
  } = (0, _styles.useStyles)();
  const fallbackAvatar = (0, _react.useMemo)(() => {
    if (defaultAvatar) return defaultAvatar;
    return targetType === 'community' ? _assets.defaultCommunityAvatarUri : _assets.defaultAvatarUri;
  }, [defaultAvatar, targetType]);
  const [avatarUrl, setAvatarUrl] = (0, _react.useState)(null);
  const [loaded, setLoaded] = (0, _react.useState)(false);
  const {
    excludes
  } = (0, _useConfig.default)();
  const configId = `${pageID}/${componentID}/${elementID}`;
  const {
    getImage
  } = (0, _hooks.useFile)();
  (0, _react.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: _enums.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(_reactNative.View, {
      testID: configId,
      accessibilityLabel: configId,
      style: [props.style, styles.avatarPlaceholder, {
        width: size,
        height: size,
        borderRadius: radius
      }]
    }, /*#__PURE__*/React.createElement(_reactNative.Text, {
      style: styles.avatarPlaceholderText
    }, firstChar));
  }
  return /*#__PURE__*/React.createElement(_reactNative.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));
};
var _default = exports.default = AvatarElement;
//# sourceMappingURL=AvatarElement.js.map