import { useEffect, useRef, useState } from 'react';
// import { useTranslation } from 'react-i18next';

import { View, Text, TouchableOpacity, Image, Modal, Animated, Alert } from 'react-native';
import { useStyles } from './styles';
import { SvgXml } from 'react-native-svg';
import { expandIcon, likeCircle, personXml, 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 { isReportTarget, reportTargetById, unReportTargetById } from '../../../../../core/legacy/feed';
import EditCommentModal from '../../EditCommentModal';
import { useTheme } from 'react-native-paper';
import { LinkPreview } from '../../../PreviewLink/LinkPreview';
import { Typography } from '../../../../../core/components/Typography/Typography';
import { pen, trash, unreport, report } from '../../../../../core/assets/icons';
import { useNavigation } from '@react-navigation/native';
import { useTimeDifference } from '../../../../hooks';
import { useToast } from '../../../../../core/stores/slices/toastSlice';
import AmityReactionListComponent from '../../../../features/reaction/components/List';
export default function ReplyCommentList({
  commentDetail,
  onDelete,
  commentId,
  onNavigate
}) {
  const {
    // commentId,
    data,
    user,
    createdAt,
    reactions,
    myReactions,
    childrenComment,
    editedAt,
    mentionPosition,
    childrenNumber
  } = commentDetail;
  const theme = useTheme();
  const styles = useStyles();
  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 [isReportByMe, setIsReportByMe] = useState(false);
  const [editCommentModal, setEditCommentModal] = useState(false);
  const [isEditComment, setIsEditComment] = useState(false);
  const slideAnimation = useRef(new Animated.Value(0)).current;
  const [commentMentionPosition, setCommentMentionPosition] = useState([]);
  const [isReactionListVisible, setIsReactionListVisible] = useState(false);
  const navigation = useNavigation();
  const timeDifference = useTimeDifference(createdAt);
  const {
    showToast
  } = useToast();
  useEffect(() => {
    if (mentionPosition) {
      setCommentMentionPosition(mentionPosition);
    }
  }, [mentionPosition]);
  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) {
      setIsLike(false);
      setLikeReaction(likeReaction - 1);
      await removeCommentReaction(commentId, 'like');
    } else {
      setIsLike(true);
      setLikeReaction(likeReaction + 1);
      await addCommentReaction(commentId, 'like');
    }
  };
  const deletePostObject = () => {
    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);
      if (unReportPost) {
        showToast({
          message: 'Reply unreported.',
          type: 'success'
        });
      }
      setIsVisible(false);
      setIsReportByMe(false);
    } else {
      const reportPost = await reportTargetById('comment', commentId);
      if (reportPost) {
        showToast({
          message: 'Reply unreported.',
          type: 'success'
        });
      }
      setIsVisible(false);
      setIsReportByMe(true);
    }
  };
  const modalStyle = {
    transform: [{
      translateY: slideAnimation.interpolate({
        inputRange: [0, 1],
        outputRange: [600, 0] // Adjust this value to control the sliding distance
      })
    }]
  };
  const openEditCommentModal = () => {
    setIsVisible(false);
    setEditCommentModal(true);
  };
  const onEditComment = editText => {
    setIsEditComment(true);
    setEditCommentModal(false);
    setTextComment(editText);
  };
  const onCloseEditCommentModal = () => {
    setEditCommentModal(false);
  };
  const onPressReplyReaction = () => {
    setIsReactionListVisible(true);
  };
  return /*#__PURE__*/React.createElement(View, {
    key: commentId,
    style: styles.replyCommentWrap
  }, /*#__PURE__*/React.createElement(View, {
    style: styles.replyHeaderSection
  }, user !== null && user !== void 0 && user.avatarFileId ? /*#__PURE__*/React.createElement(Image, {
    style: styles.avatar,
    source: {
      uri: `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.headerRow
  }, /*#__PURE__*/React.createElement(TouchableOpacity, {
    disabled: !(user !== null && user !== void 0 && user.userId),
    onPress: () => {
      if (!(user !== null && user !== void 0 && user.userId)) return;
      if (onNavigate) {
        onNavigate(user.userId);
      } else {
        navigation.navigate('UserProfile', {
          userId: user.userId
        });
      }
    }
  }, /*#__PURE__*/React.createElement(Text, {
    style: styles.headerText
  }, user === null || user === void 0 ? void 0 : user.displayName))), /*#__PURE__*/React.createElement(View, {
    style: styles.commentBubble
  }, textComment && /*#__PURE__*/React.createElement(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(Typography.Caption, {
    style: styles.headerTextTime
  }, timeDifference), (editedAt !== createdAt || isEditComment) && /*#__PURE__*/React.createElement(Typography.Caption, {
    style: styles.headerTextTime
  }, ' ', "(edited)")), /*#__PURE__*/React.createElement(TouchableOpacity, {
    onPress: () => addReactionToComment(),
    style: styles.likeBtn
  }, /*#__PURE__*/React.createElement(Typography.CaptionBold, {
    style: isLike ? styles.likedText : styles.btnText
  }, "Like")), /*#__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: onPressReplyReaction,
    style: styles.likeBtn
  }, /*#__PURE__*/React.createElement(Typography.Caption, {
    style: styles.btnText
  }, likeReaction), /*#__PURE__*/React.createElement(SvgXml, {
    xml: likeCircle,
    width: "20",
    height: "20"
  }))), /*#__PURE__*/React.createElement(View, null, childrenComment.length > 0 && /*#__PURE__*/React.createElement(Pressable, {
    style: styles.viewMoreReplyBtn
  }, /*#__PURE__*/React.createElement(SvgXml, {
    xml: expandIcon
  }), /*#__PURE__*/React.createElement(Text, {
    style: styles.viewMoreText
  }, "View ", childrenNumber, " replies"))))), /*#__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]
  }, /*#__PURE__*/React.createElement(View, {
    style: styles.handleBar
  }), (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, {
    width: "24",
    height: "24",
    xml: pen(),
    color: theme.colors.base
  }), /*#__PURE__*/React.createElement(Typography.BodyBold, {
    style: styles.normalActionText
  }, "Edit reply")), /*#__PURE__*/React.createElement(TouchableOpacity, {
    onPress: deletePostObject,
    style: styles.modalRow
  }, /*#__PURE__*/React.createElement(SvgXml, {
    width: "24",
    height: "24",
    xml: trash(),
    color: theme.colors.alert
  }), /*#__PURE__*/React.createElement(Typography.BodyBold, {
    style: styles.dangerActionText
  }, "Delete reply"))) : /*#__PURE__*/React.createElement(TouchableOpacity, {
    onPress: reportCommentObject,
    style: styles.modalRow
  }, /*#__PURE__*/React.createElement(SvgXml, {
    width: "24",
    height: "24",
    color: theme.colors.base,
    xml: isReportByMe ? unreport() : report()
  }), /*#__PURE__*/React.createElement(Typography.BodyBold, {
    style: styles.normalActionText
  }, isReportByMe ? 'Unreport reply' : 'Report reply'))))), /*#__PURE__*/React.createElement(EditCommentModal, {
    visible: editCommentModal,
    commentDetail: commentDetail,
    onFinishEdit: onEditComment,
    onClose: onCloseEditCommentModal
  }), /*#__PURE__*/React.createElement(AmityReactionListComponent, {
    isModalVisible: isReactionListVisible,
    onCloseModal: () => setIsReactionListVisible(false),
    referenceId: commentId,
    referenceType: "comment",
    onPressUser: userId => {
      setIsReactionListVisible(false);
      if (onNavigate) {
        onNavigate(userId);
      } else {
        navigation.navigate('UserProfile', {
          userId
        });
      }
    }
  }));
}
//# sourceMappingURL=index.js.map