"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _reactNative = require("react-native");
var _react = require("react");
var _styles = require("./styles");
var _enums = require("../../enums");
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _native = require("@react-navigation/native");
var _BehaviourProvider = require("../../providers/BehaviourProvider");
var _AvatarElement = _interopRequireDefault(require("../../elements/CommonElements/AvatarElement"));
var _hooks = require("../../hooks");
var _TextElement = _interopRequireDefault(require("../../elements/CommonElements/TextElement"));
var _ImageElement = _interopRequireDefault(require("../../elements/CommonElements/ImageElement"));
var _number = require("../../../core/utils/number");
var _BrandBadge = require("../../elements/BrandBadge");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const SearchResultItem = ({
  pageId = _enums.PageID.WildCardPage,
  componentId = _enums.ComponentID.WildCardComponent,
  item,
  searchType
}) => {
  const {
    themeStyles
  } = (0, _hooks.useAmityComponent)({
    pageId: pageId,
    componentId: componentId
  });
  const styles = (0, _styles.useStyles)(themeStyles);
  const {
    AmityCommunitySearchResultComponent
  } = (0, _BehaviourProvider.useBehaviour)();
  const navigation = (0, _native.useNavigation)();
  const [communityCategory, setCommunityCategory] = (0, _react.useState)(null);
  const isCommunity = searchType === _enums.TabName.Communities || searchType === _enums.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 = (0, _react.useCallback)(() => {
    if (isCommunity) {
      if (AmityCommunitySearchResultComponent.goToCommunityProfilePage) {
        return AmityCommunitySearchResultComponent.goToCommunityProfilePage({
          targetId: item.communityId,
          targetType: _enums.TabName.Communities
        });
      }
      return navigation.navigate('CommunityProfilePage', {
        communityId: item.communityId
      });
    }
    if (AmityCommunitySearchResultComponent.goToUserProfilePage) return AmityCommunitySearchResultComponent.goToUserProfilePage({
      targetId: item.communityId ?? item.userId,
      targetType: _enums.TabName.Users
    });
    return navigation.navigate('UserProfile', {
      userId: item.userId
    });
  }, [AmityCommunitySearchResultComponent, isCommunity, item.communityId, item.userId, navigation]);
  (0, _react.useEffect)(() => {
    (async () => {
      if (isCommunity) {
        const {
          data
        } = await _tsSdkReactNative.CategoryRepository.getCategory(item.categoryIds[0]);
        setCommunityCategory(data);
      }
    })();
  }, [isCommunity, item.categoryIds]);
  return /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
    style: styles.container,
    onPress: onPressSearchResultItem
  }, /*#__PURE__*/React.createElement(_AvatarElement.default, {
    style: styles.avatar,
    avatarId: item.avatarFileId,
    pageID: pageId,
    componentID: componentId,
    elementID: _enums.ElementID.community_avatar,
    avatarCustomUrl: searchType === _enums.TabName.Users ? item === null || item === void 0 ? void 0 : item.avatarCustomUrl : undefined,
    targetType: searchType === _enums.TabName.Communities ? 'community' : 'user',
    displayName: searchType === _enums.TabName.Users ? item === null || item === void 0 ? void 0 : item.displayName : undefined
  }), /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.profileInfoContainer
  }, /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.rowContainer
  }, showPrivateIcon && /*#__PURE__*/React.createElement(_ImageElement.default, {
    pageID: pageId,
    componentID: componentId,
    elementID: _enums.ElementID.community_private_badge,
    style: styles.lockIcon,
    configKey: "icon"
  }), /*#__PURE__*/React.createElement(_TextElement.default, {
    pageID: pageId,
    componentID: componentId,
    elementID: _enums.ElementID.community_display_name,
    text: item.displayName,
    style: styles.diaplayName,
    numberOfLines: 1,
    ellipsizeMode: "tail"
  }), !isCommunity && item.isBrand && /*#__PURE__*/React.createElement(_BrandBadge.BrandBadge, {
    width: 20,
    height: 20
  }), showOfficialBadgeIcon && /*#__PURE__*/React.createElement(_ImageElement.default, {
    pageID: pageId,
    componentID: componentId,
    elementID: _enums.ElementID.community_official_badge,
    style: styles.badgeIcon,
    configKey: "icon"
  })), isCommunity && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(_reactNative.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(_reactNative.Text, {
    style: styles.memberCounts,
    testID: "community_search_result/community_members_count",
    accessibilityLabel: "community_search_result/community_members_count"
  }, `${(0, _number.formatNumber)(item === null || item === void 0 ? void 0 : item.membersCount)} ${memberText}`))));
};
var _default = exports.default = /*#__PURE__*/(0, _react.memo)(SearchResultItem);
//# sourceMappingURL=SearchResultItem.js.map