import { useEffect, useState } from 'react';
import { Image, Text, TouchableOpacity, View } from 'react-native';
import { useStyles } from './styles';
import { SvgXml } from 'react-native-svg';
import { communityIcon, userIcon } from '../../../../core/assets/icons/xml';
import { CategoryRepository } from '@amityco/ts-sdk-react-native';
import { useNavigation } from '@react-navigation/native';
import useAuth from '../../../../core/hooks/useAuth';
export default function SearchItem({
  target,
  onPress,
  userProfileNavigateEnabled = true
}) {
  const styles = useStyles();
  const {
    apiRegion
  } = useAuth();
  const [isChecked, setIsChecked] = useState(false);
  const [categoryName, setCategoryName] = useState('');
  const navigation = useNavigation();
  const maxLength = 25;
  const handleToggle = () => {
    setIsChecked(!isChecked);
    if (onPress) {
      onPress(target);
    }
    if (userProfileNavigateEnabled) {
      if (target.targetType === 'community') {
        navigation.navigate('CommunityHome', {
          communityId: target.targetId,
          communityName: target.displayName
        });
      } else {
        navigation.navigate('UserProfile', {
          userId: target.targetId
        });
      }
    }
  };
  useEffect(() => {
    getCategory();
  }, [target.categoryIds]);
  async function getCategory() {
    if (target.categoryIds) {
      const {
        data: category
      } = await CategoryRepository.getCategory(target.categoryIds[0]);
      category && setCategoryName(category.name);
    }
  }
  const displayName = () => {
    if (target.displayName) {
      if (target.displayName.length > maxLength) {
        return target.displayName.substring(0, maxLength) + '..';
      }
      return target.displayName;
    }
    return 'Display name';
  };
  const avatarFileURL = fileId => {
    return `https://api.${apiRegion}.amity.co/api/v3/files/${fileId}/download?size=medium`;
  };
  return /*#__PURE__*/React.createElement(TouchableOpacity, {
    style: styles.listItem,
    onPress: handleToggle
  }, /*#__PURE__*/React.createElement(View, {
    style: styles.leftContainer
  }, target.avatarFileId ? /*#__PURE__*/React.createElement(Image, {
    style: styles.avatar,
    source: {
      uri: target.avatarFileId && avatarFileURL(target.avatarFileId)
    }
  }) : /*#__PURE__*/React.createElement(SvgXml, {
    style: styles.avatar,
    width: 40,
    height: 40,
    xml: target.targetType === 'user' ? userIcon() : communityIcon
  }), /*#__PURE__*/React.createElement(View, null, /*#__PURE__*/React.createElement(Text, {
    style: styles.itemText
  }, displayName()), target.targetType === 'community' && /*#__PURE__*/React.createElement(Text, {
    style: styles.categoryText
  }, categoryName))));
}
//# sourceMappingURL=index.js.map