import { Text, TouchableOpacity, View } from 'react-native';
import { memo, useCallback, useEffect, useState } from 'react';
import { useStyles } from './styles';
import { ComponentID, ElementID, PageID, TabName } from '../../enums';
import { CategoryRepository } from '@amityco/ts-sdk-react-native';
import { useNavigation } from '@react-navigation/native';
import { useBehaviour } from '../../providers/BehaviourProvider';
import AvatarElement from '../../elements/CommonElements/AvatarElement';
import { useAmityComponent } from '../../hooks';
import TextElement from '../../elements/CommonElements/TextElement';
import ImageElement from '../../elements/CommonElements/ImageElement';
import { formatNumber } from '../../../core/utils/number';
import { BrandBadge } from '../../elements/BrandBadge';
const SearchResultItem = ({
  pageId = PageID.WildCardPage,
  componentId = ComponentID.WildCardComponent,
  item,
  searchType
}) => {
  const {
    themeStyles
  } = useAmityComponent({
    pageId: pageId,
    componentId: componentId
  });
  const styles = useStyles(themeStyles);
  const {
    AmityCommunitySearchResultComponent
  } = useBehaviour();
  const navigation = useNavigation();
  const [communityCategory, setCommunityCategory] = useState(null);
  const isCommunity = searchType === TabName.Communities || searchType === TabName.MyCommunities;
  const showPrivateIcon = isCommunity && !item.isPublic;
  const showOfficialBadgeIcon = isCommunity && item.isOfficial;
  const memberText = (item === null || item === void 0 ? void 0 : item.membersCount) > 1 ? 'members' : 'member';
  const onPressSearchResultItem = useCallback(() => {
    if (isCommunity) {
      if (AmityCommunitySearchResultComponent.goToCommunityProfilePage) {
        return AmityCommunitySearchResultComponent.goToCommunityProfilePage({
          targetId: item.communityId,
          targetType: TabName.Communities
        });
      }
      return navigation.navigate('CommunityProfilePage', {
        communityId: item.communityId
      });
    }
    if (AmityCommunitySearchResultComponent.goToUserProfilePage) return AmityCommunitySearchResultComponent.goToUserProfilePage({
      targetId: item.communityId ?? item.userId,
      targetType: TabName.Users
    });
    return navigation.navigate('UserProfile', {
      userId: item.userId
    });
  }, [AmityCommunitySearchResultComponent, isCommunity, item.communityId, item.userId, navigation]);
  useEffect(() => {
    (async () => {
      if (isCommunity) {
        const {
          data
        } = await CategoryRepository.getCategory(item.categoryIds[0]);
        setCommunityCategory(data);
      }
    })();
  }, [isCommunity, item.categoryIds]);
  return /*#__PURE__*/React.createElement(TouchableOpacity, {
    style: styles.container,
    onPress: onPressSearchResultItem
  }, /*#__PURE__*/React.createElement(AvatarElement, {
    style: styles.avatar,
    avatarId: item.avatarFileId,
    pageID: pageId,
    componentID: componentId,
    elementID: ElementID.community_avatar,
    avatarCustomUrl: searchType === TabName.Users ? item === null || item === void 0 ? void 0 : item.avatarCustomUrl : undefined,
    targetType: searchType === TabName.Communities ? 'community' : 'user',
    displayName: searchType === TabName.Users ? item === null || item === void 0 ? void 0 : item.displayName : undefined
  }), /*#__PURE__*/React.createElement(View, {
    style: styles.profileInfoContainer
  }, /*#__PURE__*/React.createElement(View, {
    style: styles.rowContainer
  }, showPrivateIcon && /*#__PURE__*/React.createElement(ImageElement, {
    pageID: pageId,
    componentID: componentId,
    elementID: ElementID.community_private_badge,
    style: styles.lockIcon,
    configKey: "icon"
  }), /*#__PURE__*/React.createElement(TextElement, {
    pageID: pageId,
    componentID: componentId,
    elementID: ElementID.community_display_name,
    text: item.displayName,
    style: styles.diaplayName,
    numberOfLines: 1,
    ellipsizeMode: "tail"
  }), !isCommunity && item.isBrand && /*#__PURE__*/React.createElement(BrandBadge, {
    width: 20,
    height: 20
  }), showOfficialBadgeIcon && /*#__PURE__*/React.createElement(ImageElement, {
    pageID: pageId,
    componentID: componentId,
    elementID: ElementID.community_official_badge,
    style: styles.badgeIcon,
    configKey: "icon"
  })), isCommunity && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Text, {
    style: (communityCategory === null || communityCategory === void 0 ? void 0 : communityCategory.name) && styles.category,
    testID: "community_search_result/community_category_name",
    accessibilityLabel: "community_search_result/community_category_name"
  }, (communityCategory === null || communityCategory === void 0 ? void 0 : communityCategory.name) ?? ''), /*#__PURE__*/React.createElement(Text, {
    style: styles.memberCounts,
    testID: "community_search_result/community_members_count",
    accessibilityLabel: "community_search_result/community_members_count"
  }, `${formatNumber(item === null || item === void 0 ? void 0 : item.membersCount)} ${memberText}`))));
};
export default /*#__PURE__*/memo(SearchResultItem);
//# sourceMappingURL=SearchResultItem.js.map