import { memo, useEffect, useRef, useState } from 'react';
import { View, Text, TouchableOpacity, Image, Modal, Animated, Alert } from 'react-native';
import { useStyles } from './styles';
import { SvgXml } from 'react-native-svg';
import { editIcon, likeCircle, personXml, reportOutLine, storyDraftDeletHyperLink, threeDots } from '../../../../../../core/assets/icons/xml';
import { addCommentReaction, removeCommentReaction } from '../../../../../../core/legacy/comment';
import { Pressable } from 'react-native';
import useAuth from '../../../../../../core/hooks/useAuth';
import { useTimeDifference } from '../../../../../hooks/useTimeDifference';
import { isReportTarget, reportTargetById, unReportTargetById } from '../../../../../../core/legacy/feed';
import EditCommentModal from '../../../../../components/legacy/EditCommentModal';
import { useTheme } from 'react-native-paper';
import ModeratorBadgeElement from '../../../../../elements/ModeratorBadgeElement/ModeratorBadgeElement';
import { BrandBadge } from '../../../../../elements/BrandBadge';
import { ComponentID, PageID } from '../../../../../enums';
import AmityReactionListComponent from '../../../../reaction/components/List';
import RenderTextWithMention from '../../../../../components/RenderTextWithMention/RenderTextWithMention';
import uiSlice from '../../../../../../core/stores/slices/uiSlice';
import { useUIKitDispatch } from '../../../../../../core/stores/store';
import { useNavigation } from '@react-navigation/native';
const ReplyCommentList = ({
  commentDetail,
  onDelete,
  commentId
}) => {
  const {
    data,
    user,
    createdAt,
    reactions,
    myReactions,
    childrenComment,
    editedAt,
    mentionPosition,
    targetType,
    targetId
  } = commentDetail;
  const theme = useTheme();
  const styles = useStyles();
  const dispatch = useUIKitDispatch();
  const {
    showToastMessage
  } = uiSlice.actions;
  const timeDifference = useTimeDifference(createdAt);
  const [isLike, setIsLike] = useState(myReactions ? myReactions.includes('like') : false);
  const [likeReaction, setLikeReaction] = useState(reactions.like ? reactions.like : 0);
  const {
    client,
    apiRegion
  } = useAuth();
  const [textComment, setTextComment] = useState(data.text);
  const [isVisible, setIsVisible] = useState(false);
  const [isReactionListVisible, setIsReactionListVisible] = useState(false);
  const [isReportByMe, setIsReportByMe] = useState(false);
  const [editCommentModal, setEditCommentModal] = useState(false);
  const [isEditComment, setIsEditComment] = useState(false);
  const slideAnimation = useRef(new Animated.Value(0)).current;
  const navigation = useNavigation();
  const openModal = () => {
    setIsVisible(true);
  };
  const closeModal = () => {
    Animated.timing(slideAnimation, {
      toValue: 0,
      duration: 100,
      useNativeDriver: true
    }).start(() => setIsVisible(false));
  };
  const checkIsReport = async () => {
    const isReport = await isReportTarget('comment', commentId);
    if (isReport) {
      setIsReportByMe(true);
    }
  };
  useEffect(() => {
    checkIsReport();
  }, [childrenComment]);
  const addReactionToComment = async () => {
    if (isLike && likeReaction) {
      setLikeReaction(likeReaction - 1);
      setIsLike(false);
      await removeCommentReaction(commentId, 'like');
    } else {
      setIsLike(true);
      setLikeReaction(likeReaction + 1);
      await addCommentReaction(commentId, 'like');
    }
  };
  const deleteReplyComment = () => {
    Alert.alert('Delete reply', 'This reply will be permanently deleted.', [{
      text: 'Cancel',
      style: 'cancel'
    }, {
      text: 'Delete',
      style: 'destructive',
      onPress: () => onDelete && onDelete(commentId)
    }]);
    setIsVisible(false);
  };
  const reportCommentObject = async () => {
    if (isReportByMe) {
      const unReportPost = await unReportTargetById('comment', commentId);
      setIsVisible(false);
      setIsReportByMe(false);
      if (unReportPost) {
        dispatch(showToastMessage({
          toastMessage: 'Comment unreported',
          isSuccessToast: true
        }));
      }
    } else {
      const reportPost = await reportTargetById('comment', commentId);
      setIsVisible(false);
      setIsReportByMe(true);
      if (reportPost) {
        dispatch(showToastMessage({
          toastMessage: 'Comment reported',
          isSuccessToast: true
        }));
      }
    }
  };
  const modalStyle = {
    transform: [{
      translateY: slideAnimation.interpolate({
        inputRange: [0, 1],
        outputRange: [600, 0]
      })
    }]
  };
  const openEditCommentModal = () => {
    setIsVisible(false);
    setEditCommentModal(true);
  };
  const onEditComment = editText => {
    setIsEditComment(true);
    setEditCommentModal(false);
    setTextComment(editText);
  };
  const onCloseEditCommentModal = () => {
    setEditCommentModal(false);
  };
  const onPressCommentReaction = () => {
    setIsReactionListVisible(true);
  };
  return /*#__PURE__*/React.createElement(View, {
    key: commentId,
    style: styles.replyCommentWrap
  }, /*#__PURE__*/React.createElement(View, {
    style: styles.replyHeaderSection
  }, /*#__PURE__*/React.createElement(TouchableOpacity, {
    onPress: () => (user === null || user === void 0 ? void 0 : user.userId) && navigation.navigate('UserProfile', {
      userId: (user === null || user === void 0 ? void 0 : user.userId) || ''
    })
  }, user !== null && user !== void 0 && user.avatarFileId ? /*#__PURE__*/React.createElement(Image, {
    style: styles.avatar,
    source: {
      uri: user !== null && user !== void 0 && user.avatarCustomUrl ? user === null || user === void 0 ? void 0 : user.avatarCustomUrl : `https://api.${apiRegion}.amity.co/api/v3/files/${user === null || user === void 0 ? void 0 : user.avatarFileId}/download`
    }
  }) : /*#__PURE__*/React.createElement(View, {
    style: styles.avatar
  }, /*#__PURE__*/React.createElement(SvgXml, {
    xml: personXml,
    width: "20",
    height: "16"
  }))), /*#__PURE__*/React.createElement(View, {
    style: styles.rightSection
  }, /*#__PURE__*/React.createElement(View, {
    style: styles.commentBubble
  }, /*#__PURE__*/React.createElement(TouchableOpacity, {
    onPress: () => (user === null || user === void 0 ? void 0 : user.userId) && navigation.navigate('UserProfile', {
      userId: (user === null || user === void 0 ? void 0 : user.userId) || ''
    })
  }, /*#__PURE__*/React.createElement(View, {
    style: styles.displayNameRow
  }, /*#__PURE__*/React.createElement(Text, {
    style: styles.headerText,
    numberOfLines: 1,
    ellipsizeMode: "tail"
  }, user === null || user === void 0 ? void 0 : user.displayName), (user === null || user === void 0 ? void 0 : user.isBrand) && /*#__PURE__*/React.createElement(BrandBadge, {
    width: 16,
    height: 16
  }))), targetType === 'community' && targetId &&
  /*#__PURE__*/
  // eslint-disable-next-line react-native/no-inline-styles
  React.createElement(View, {
    style: {
      marginVertical: 6
    }
  }, /*#__PURE__*/React.createElement(ModeratorBadgeElement, {
    communityId: targetId,
    userId: user === null || user === void 0 ? void 0 : user.userId,
    pageID: PageID.WildCardPage,
    componentID: ComponentID.post_content
  })), textComment && /*#__PURE__*/React.createElement(RenderTextWithMention, {
    textPost: textComment,
    mentionPositionArr: mentionPosition ?? []
  })
  // <LinkPreview
  //   text={textComment}
  //   mentionPositionArr={commentMentionPosition}
  // />
  ), /*#__PURE__*/React.createElement(View, {
    style: styles.actionSection
  }, /*#__PURE__*/React.createElement(View, {
    style: styles.rowContainer
  }, /*#__PURE__*/React.createElement(View, {
    style: styles.timeRow
  }, /*#__PURE__*/React.createElement(Text, {
    style: styles.headerTextTime
  }, timeDifference), (editedAt !== createdAt || isEditComment) && /*#__PURE__*/React.createElement(Text, {
    style: styles.headerTextTime
  }, " (edited)")), /*#__PURE__*/React.createElement(TouchableOpacity, {
    onPress: () => addReactionToComment(),
    style: styles.likeBtn
  }, /*#__PURE__*/React.createElement(Text, {
    style: isLike ? styles.likedText : styles.btnText
  }, !isLike ? 'Like' : 'Liked')), /*#__PURE__*/React.createElement(TouchableOpacity, {
    onPress: openModal,
    style: styles.threeDots
  }, /*#__PURE__*/React.createElement(SvgXml, {
    xml: threeDots(theme.colors.base),
    width: "20",
    height: "16"
  }))), likeReaction > 0 && /*#__PURE__*/React.createElement(TouchableOpacity, {
    onPress: onPressCommentReaction,
    style: styles.likeBtn
  }, /*#__PURE__*/React.createElement(Text, {
    style: styles.btnText
  }, likeReaction), /*#__PURE__*/React.createElement(SvgXml
  // eslint-disable-next-line react-native/no-inline-styles
  , {
    style: {
      marginLeft: 4
    },
    xml: likeCircle,
    width: "20",
    height: "16"
  }))))), /*#__PURE__*/React.createElement(Modal, {
    animationType: "fade",
    transparent: true,
    visible: isVisible,
    onRequestClose: closeModal
  }, /*#__PURE__*/React.createElement(Pressable, {
    onPress: closeModal,
    style: styles.modalContainer
  }, /*#__PURE__*/React.createElement(Animated.View, {
    style: [styles.modalContent, modalStyle, (user === null || user === void 0 ? void 0 : user.userId) === client.userId && styles.twoOptions]
  }, (user === null || user === void 0 ? void 0 : user.userId) === client.userId ? /*#__PURE__*/React.createElement(View, null, /*#__PURE__*/React.createElement(TouchableOpacity, {
    onPress: openEditCommentModal,
    style: styles.modalRow
  }, /*#__PURE__*/React.createElement(SvgXml, {
    xml: editIcon(theme.colors.base),
    width: "20",
    height: "20"
  }), /*#__PURE__*/React.createElement(Text, {
    style: styles.deleteText
  }, " Edit reply")), /*#__PURE__*/React.createElement(TouchableOpacity, {
    onPress: deleteReplyComment,
    style: styles.modalRow
  }, /*#__PURE__*/React.createElement(SvgXml, {
    xml: storyDraftDeletHyperLink(),
    width: "20",
    height: "20"
  }), /*#__PURE__*/React.createElement(Text, {
    style: styles.deleteText
  }, " Delete reply"))) : /*#__PURE__*/React.createElement(TouchableOpacity, {
    onPress: reportCommentObject,
    style: styles.modalRow
  }, /*#__PURE__*/React.createElement(SvgXml, {
    xml: reportOutLine(theme.colors.base),
    width: "20",
    height: "20"
  }), /*#__PURE__*/React.createElement(Text, {
    style: styles.deleteText
  }, isReportByMe ? 'Unreport reply' : 'Report reply'))))), /*#__PURE__*/React.createElement(EditCommentModal, {
    visible: editCommentModal,
    commentDetail: commentDetail,
    onFinishEdit: onEditComment,
    onClose: onCloseEditCommentModal
  }), isReactionListVisible && /*#__PURE__*/React.createElement(AmityReactionListComponent, {
    referenceId: commentId,
    referenceType: "comment",
    isModalVisible: isReactionListVisible,
    onCloseModal: () => setIsReactionListVisible(false)
  }));
};
export default /*#__PURE__*/memo(ReplyCommentList);
//# sourceMappingURL=index.js.map