"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = SearchItem;
var _react = require("react");
var _reactNative = require("react-native");
var _styles = require("./styles");
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _native = require("@react-navigation/native");
var _hooks = require("../../hooks");
var _enums = require("../../enums");
var _assets = require("../../../core/assets");
function SearchItem({
  target,
  onPress,
  userProfileNavigateEnabled = true
}) {
  const styles = (0, _styles.useStyles)();
  const {
    getImage
  } = (0, _hooks.useFile)();
  const [isChecked, setIsChecked] = (0, _react.useState)(false);
  const [avatar, setAvatar] = (0, _react.useState)(_assets.defaultAvatarUri);
  const [categoryName, setCategoryName] = (0, _react.useState)('');
  const navigation = (0, _native.useNavigation)();
  const handleToggle = () => {
    setIsChecked(!isChecked);
    if (onPress) {
      onPress(target);
    }
    if (userProfileNavigateEnabled) {
      if (target.targetType === 'community') {
        navigation.navigate('CommunityProfilePage', {
          communityId: target.targetId
        });
      } else {
        navigation.navigate('UserProfile', {
          userId: target.targetId
        });
      }
    }
  };
  (0, _react.useEffect)(() => {
    if (!(target !== null && target !== void 0 && target.avatarFileId)) return;
    (async () => {
      const fileUrl = await getImage({
        fileId: target === null || target === void 0 ? void 0 : target.avatarFileId,
        imageSize: _enums.ImageSizeState.small
      });
      const avatarUrl = fileUrl ? fileUrl : (target === null || target === void 0 ? void 0 : target.targetType) === 'community' ? _assets.defaultCommunityAvatarUri : _assets.defaultAvatarUri;
      setAvatar(avatarUrl);
    })();
  }, [getImage, target === null || target === void 0 ? void 0 : target.avatarFileId, target === null || target === void 0 ? void 0 : target.targetType]);
  (0, _react.useEffect)(() => {
    getCategory();
  }, [target.categoryIds]);
  async function getCategory() {
    if (target.categoryIds) {
      const {
        data: category
      } = await _tsSdkReactNative.CategoryRepository.getCategory(target.categoryIds[0]);
      category && setCategoryName(category.name);
    }
  }
  return /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
    style: styles.listItem,
    onPress: handleToggle
  }, /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.leftContainer
  }, /*#__PURE__*/React.createElement(_reactNative.Image, {
    style: styles.avatar,
    source: {
      uri: avatar
    }
  }), /*#__PURE__*/React.createElement(_reactNative.View, null, /*#__PURE__*/React.createElement(_reactNative.Text, {
    numberOfLines: 1,
    style: styles.itemText
  }, target === null || target === void 0 ? void 0 : target.displayName), (target === null || target === void 0 ? void 0 : target.targetType) === 'community' && /*#__PURE__*/React.createElement(_reactNative.Text, {
    style: styles.categoryText
  }, categoryName))));
}
//# sourceMappingURL=index.js.map