"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 _reactNativeSvg = require("react-native-svg");
var _xml = require("../../../../core/assets/icons/xml");
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _native = require("@react-navigation/native");
var _useAuth = _interopRequireDefault(require("../../../../core/hooks/useAuth"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function SearchItem({
  target,
  onPress,
  userProfileNavigateEnabled = true
}) {
  const styles = (0, _styles.useStyles)();
  const {
    apiRegion
  } = (0, _useAuth.default)();
  const [isChecked, setIsChecked] = (0, _react.useState)(false);
  const [categoryName, setCategoryName] = (0, _react.useState)('');
  const navigation = (0, _native.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
        });
      }
    }
  };
  (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);
    }
  }
  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(_reactNative.TouchableOpacity, {
    style: styles.listItem,
    onPress: handleToggle
  }, /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.leftContainer
  }, target.avatarFileId ? /*#__PURE__*/React.createElement(_reactNative.Image, {
    style: styles.avatar,
    source: {
      uri: target.avatarFileId && avatarFileURL(target.avatarFileId)
    }
  }) : /*#__PURE__*/React.createElement(_reactNativeSvg.SvgXml, {
    style: styles.avatar,
    width: 40,
    height: 40,
    xml: target.targetType === 'user' ? (0, _xml.userIcon)() : _xml.communityIcon
  }), /*#__PURE__*/React.createElement(_reactNative.View, null, /*#__PURE__*/React.createElement(_reactNative.Text, {
    style: styles.itemText
  }, displayName()), target.targetType === 'community' && /*#__PURE__*/React.createElement(_reactNative.Text, {
    style: styles.categoryText
  }, categoryName))));
}
//# sourceMappingURL=index.js.map