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

import { View, Text, TouchableOpacity, Image } from 'react-native';
import { SvgXml } from 'react-native-svg';
import { personXml } from '../../../../../core/assets/icons/xml';
import { useStyles } from './styles';
import { useNavigation } from '@react-navigation/native';
import useAuth from '../../../../../core/hooks/useAuth';
import MediaSection from '../../MediaSection';
import { PostRepository } from '@amityco/ts-sdk-react-native';
import { LinkPreview } from '../../../PreviewLink/LinkPreview';
import RenderTextWithMention from '../../../RenderTextWithMention/RenderTextWithMention';
export default function PendingPostList({
  postDetail,
  onAcceptDecline,
  isModerator = false
}) {
  const [postData, setPostData] = useState(postDetail);
  const {
    apiRegion
  } = useAuth();
  const styles = useStyles();
  const [textPost, setTextPost] = useState();
  const navigation = useNavigation();
  const [mentionPositionArr, setMentionsPositionArr] = useState([]);
  const [loading, setLoading] = useState(true);
  const {
    data,
    postId,
    createdAt,
    creator: user,
    children: childrenPosts = [],
    metadata
  } = postData ?? {};
  const mentionPosition = metadata === null || metadata === void 0 ? void 0 : metadata.mentioned;
  useEffect(() => {
    if (mentionPosition) {
      setMentionsPositionArr(mentionPosition);
    }
  }, [mentionPosition]);
  useEffect(() => {
    setTimeout(() => {
      setLoading(false);
    }, 200);
    setPostData(postDetail);
  }, [postDetail]);
  useEffect(() => {
    setTextPost(data === null || data === void 0 ? void 0 : data.text);
  }, [postDetail]);
  function getTimeDifference(timestamp) {
    // Convert the timestamp string to a Date object
    const timestampDate = Date.parse(timestamp);

    // Get the current date and time
    const currentDate = Date.now();

    // Calculate the difference in milliseconds
    const differenceMs = currentDate - timestampDate;
    const differenceYear = Math.floor(differenceMs / (1000 * 60 * 60 * 24 * 365));
    const differenceDay = Math.floor(differenceMs / (1000 * 60 * 60 * 24));
    const differenceHour = Math.floor(differenceMs / (1000 * 60 * 60));
    const differenceMinutes = Math.floor(differenceMs / (1000 * 60));
    const differenceSec = Math.floor(differenceMs / 1000);
    if (differenceSec < 60) {
      return 'Just now';
    } else if (differenceMinutes < 60) {
      return differenceMinutes + ` ${differenceMinutes === 1 ? 'min ago' : 'mins ago'}`;
    } else if (differenceHour < 24) {
      return differenceHour + ` ${differenceHour === 1 ? 'hour ago' : 'hours ago'}`;
    } else if (differenceDay < 365) {
      return (differenceDay !== 1 ? differenceDay : '') + ` ${differenceDay === 1 ? 'Yesterday' : 'days ago'}`;
    } else {
      return differenceYear + ` ${differenceYear === 1 ? 'year ago' : 'years ago'}`;
    }
  }
  const handleDisplayNamePress = () => {
    if (user !== null && user !== void 0 && user.userId) {
      navigation.navigate('UserProfile', {
        userId: user.userId
      });
    }
  };
  const memoizedMediaSection = useMemo(() => {
    return /*#__PURE__*/React.createElement(MediaSection, {
      childrenPosts: childrenPosts
    });
  }, [childrenPosts]);
  async function approvePost() {
    onAcceptDecline && onAcceptDecline(postId);
    await PostRepository.approvePost(postId);
  }
  async function declinePost() {
    onAcceptDecline && onAcceptDecline(postId);
    await PostRepository.declinePost(postId);
  }
  return /*#__PURE__*/React.createElement(View, {
    key: postId,
    style: styles.postWrap
  }, /*#__PURE__*/React.createElement(View, {
    style: styles.headerSection
  }, /*#__PURE__*/React.createElement(View, {
    style: styles.user
  }, 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, null, /*#__PURE__*/React.createElement(View, {
    style: styles.headerRow
  }, /*#__PURE__*/React.createElement(TouchableOpacity, {
    onPress: handleDisplayNamePress
  }, /*#__PURE__*/React.createElement(Text, {
    style: styles.headerText
  }, user === null || user === void 0 ? void 0 : user.displayName))), /*#__PURE__*/React.createElement(View, {
    style: styles.timeRow
  }, /*#__PURE__*/React.createElement(Text, {
    style: styles.headerTextTime
  }, getTimeDifference(createdAt)))))), /*#__PURE__*/React.createElement(View, null, /*#__PURE__*/React.createElement(View, {
    style: styles.bodySection
  }, textPost && (childrenPosts === null || childrenPosts === void 0 ? void 0 : childrenPosts.length) === 0 && /*#__PURE__*/React.createElement(LinkPreview, {
    text: textPost,
    mentionPositionArr: [...mentionPositionArr]
  }), textPost && (childrenPosts === null || childrenPosts === void 0 ? void 0 : childrenPosts.length) > 0 && /*#__PURE__*/React.createElement(RenderTextWithMention, {
    textPost: textPost,
    mentionPositionArr: [...mentionPositionArr]
  }), childrenPosts.length > 0 && /*#__PURE__*/React.createElement(View, {
    style: styles.mediaWrap
  }, !loading && memoizedMediaSection)), isModerator && /*#__PURE__*/React.createElement(View, {
    style: styles.actionSection
  }, /*#__PURE__*/React.createElement(TouchableOpacity, {
    onPress: () => approvePost(),
    style: styles.acceptBtn
  }, /*#__PURE__*/React.createElement(Text, {
    style: styles.acceptBtnText
  }, "Accept")), /*#__PURE__*/React.createElement(TouchableOpacity, {
    onPress: () => declinePost(),
    style: styles.declineBtn
  }, /*#__PURE__*/React.createElement(Text, {
    style: styles.declineBtnText
  }, "Decline")))));
}
//# sourceMappingURL=index.js.map