"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = PendingPostList;
var _react = require("react");
var _reactNative = require("react-native");
var _reactNativeSvg = require("react-native-svg");
var _xml = require("../../../../../core/assets/icons/xml");
var _styles = require("./styles");
var _native = require("@react-navigation/native");
var _useAuth = _interopRequireDefault(require("../../../../../core/hooks/useAuth"));
var _MediaSection = _interopRequireDefault(require("../../MediaSection"));
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _LinkPreview = require("../../../PreviewLink/LinkPreview");
var _RenderTextWithMention = _interopRequireDefault(require("../../../RenderTextWithMention/RenderTextWithMention"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
// import { useTranslation } from 'react-i18next';

function PendingPostList({
  postDetail,
  onAcceptDecline,
  isModerator = false
}) {
  const [postData, setPostData] = (0, _react.useState)(postDetail);
  const {
    apiRegion
  } = (0, _useAuth.default)();
  const styles = (0, _styles.useStyles)();
  const [textPost, setTextPost] = (0, _react.useState)();
  const navigation = (0, _native.useNavigation)();
  const [mentionPositionArr, setMentionsPositionArr] = (0, _react.useState)([]);
  const [loading, setLoading] = (0, _react.useState)(true);
  const {
    data,
    postId,
    createdAt,
    creator: user,
    children: childrenPosts = [],
    metadata
  } = postData ?? {};
  const mentionPosition = metadata === null || metadata === void 0 ? void 0 : metadata.mentioned;
  (0, _react.useEffect)(() => {
    if (mentionPosition) {
      setMentionsPositionArr(mentionPosition);
    }
  }, [mentionPosition]);
  (0, _react.useEffect)(() => {
    setTimeout(() => {
      setLoading(false);
    }, 200);
    setPostData(postDetail);
  }, [postDetail]);
  (0, _react.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 = (0, _react.useMemo)(() => {
    return /*#__PURE__*/React.createElement(_MediaSection.default, {
      childrenPosts: childrenPosts
    });
  }, [childrenPosts]);
  async function approvePost() {
    onAcceptDecline && onAcceptDecline(postId);
    await _tsSdkReactNative.PostRepository.approvePost(postId);
  }
  async function declinePost() {
    onAcceptDecline && onAcceptDecline(postId);
    await _tsSdkReactNative.PostRepository.declinePost(postId);
  }
  return /*#__PURE__*/React.createElement(_reactNative.View, {
    key: postId,
    style: styles.postWrap
  }, /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.headerSection
  }, /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.user
  }, user !== null && user !== void 0 && user.avatarFileId ? /*#__PURE__*/React.createElement(_reactNative.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(_reactNative.View, {
    style: styles.avatar
  }, /*#__PURE__*/React.createElement(_reactNativeSvg.SvgXml, {
    xml: _xml.personXml,
    width: "20",
    height: "16"
  })), /*#__PURE__*/React.createElement(_reactNative.View, null, /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.headerRow
  }, /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
    onPress: handleDisplayNamePress
  }, /*#__PURE__*/React.createElement(_reactNative.Text, {
    style: styles.headerText
  }, user === null || user === void 0 ? void 0 : user.displayName))), /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.timeRow
  }, /*#__PURE__*/React.createElement(_reactNative.Text, {
    style: styles.headerTextTime
  }, getTimeDifference(createdAt)))))), /*#__PURE__*/React.createElement(_reactNative.View, null, /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.bodySection
  }, textPost && (childrenPosts === null || childrenPosts === void 0 ? void 0 : childrenPosts.length) === 0 && /*#__PURE__*/React.createElement(_LinkPreview.LinkPreview, {
    text: textPost,
    mentionPositionArr: [...mentionPositionArr]
  }), textPost && (childrenPosts === null || childrenPosts === void 0 ? void 0 : childrenPosts.length) > 0 && /*#__PURE__*/React.createElement(_RenderTextWithMention.default, {
    textPost: textPost,
    mentionPositionArr: [...mentionPositionArr]
  }), childrenPosts.length > 0 && /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.mediaWrap
  }, !loading && memoizedMediaSection)), isModerator && /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.actionSection
  }, /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
    onPress: () => approvePost(),
    style: styles.acceptBtn
  }, /*#__PURE__*/React.createElement(_reactNative.Text, {
    style: styles.acceptBtnText
  }, "Accept")), /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
    onPress: () => declinePost(),
    style: styles.declineBtn
  }, /*#__PURE__*/React.createElement(_reactNative.Text, {
    style: styles.declineBtnText
  }, "Decline")))));
}
//# sourceMappingURL=index.js.map