"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _reactNative = require("react-native");
var _react = require("react");
var _useAuth = _interopRequireDefault(require("../../../core/hooks/useAuth"));
var _hooks = require("../../hooks");
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _enums = require("../../enums");
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 MyAvatar = props => {
  const {
    client
  } = (0, _useAuth.default)();
  const {
    getImage
  } = (0, _hooks.useFile)();
  const {
    styles
  } = (0, _styles.useStyles)();
  const myId = client.userId;
  const [avatarUrl, setAvatarUrl] = (0, _react.useState)(null);
  const [displayName, setDisplayName] = (0, _react.useState)('');
  (0, _react.useEffect)(() => {
    _tsSdkReactNative.UserRepository.getUser(myId, async ({
      data,
      loading,
      error
    }) => {
      if (loading || error || !data) return;
      setDisplayName(data.displayName ?? data.userId ?? '');
      if (data.avatarCustomUrl) {
        setAvatarUrl(data.avatarCustomUrl);
        return;
      }
      const avatar = await getImage({
        fileId: data.avatarFileId,
        imageSize: _enums.ImageSizeState.small
      });
      setAvatarUrl(avatar ?? null);
    });
  }, [getImage, myId]);
  const style = props.style;
  const size = (style === null || style === void 0 ? void 0 : style.width) ?? 32;
  if (!avatarUrl) {
    const firstChar = displayName.trim().charAt(0).toUpperCase();
    if (firstChar) {
      return /*#__PURE__*/React.createElement(_reactNative.View, {
        style: [styles.avatarPlaceholder, {
          width: size,
          height: size,
          borderRadius: size / 2
        }]
      }, /*#__PURE__*/React.createElement(_reactNative.Text, {
        style: styles.avatarPlaceholderText
      }, firstChar));
    }
    // displayName not yet loaded — render nothing (avoids flash of default PNG)
    return /*#__PURE__*/React.createElement(_reactNative.View, {
      style: [styles.placeholder, {
        width: size,
        height: size,
        borderRadius: size / 2
      }]
    });
  }
  return /*#__PURE__*/React.createElement(_reactNative.Image, _extends({
    source: {
      uri: avatarUrl
    },
    style: styles.img
  }, props));
};
var _default = exports.default = /*#__PURE__*/(0, _react.memo)(MyAvatar);
//# sourceMappingURL=MyAvatar.js.map