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 { memo, useEffect, useState } from 'react';
import useAuth from '../../../core/hooks/useAuth';
import { useFile } from '../../hooks';
import { UserRepository } from '@amityco/ts-sdk-react-native';
import { ImageSizeState } from '../../enums';
import { useStyles } from './styles';
const MyAvatar = props => {
  const {
    client
  } = useAuth();
  const {
    getImage
  } = useFile();
  const {
    styles
  } = useStyles();
  const myId = client.userId;
  const [avatarUrl, setAvatarUrl] = useState(null);
  const [displayName, setDisplayName] = useState('');
  useEffect(() => {
    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: 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(View, {
        style: [styles.avatarPlaceholder, {
          width: size,
          height: size,
          borderRadius: size / 2
        }]
      }, /*#__PURE__*/React.createElement(Text, {
        style: styles.avatarPlaceholderText
      }, firstChar));
    }
    // displayName not yet loaded — render nothing (avoids flash of default PNG)
    return /*#__PURE__*/React.createElement(View, {
      style: [styles.placeholder, {
        width: size,
        height: size,
        borderRadius: size / 2
      }]
    });
  }
  return /*#__PURE__*/React.createElement(Image, _extends({
    source: {
      uri: avatarUrl
    },
    style: styles.img
  }, props));
};
export default /*#__PURE__*/memo(MyAvatar);
//# sourceMappingURL=MyAvatar.js.map