import { Pressable, Text, TouchableOpacity, View } from 'react-native';
import { memo, useCallback, useEffect, useState, useRef } from 'react';
import { PostRepository, SubscriptionLevels, getPostTopic, subscribeTopic } from '@amityco/ts-sdk-react-native';
import { useStyles } from './styles';
import { useAmityComponent } from '../../../../../hooks';
import { PageID, ComponentID } from '../../../../../enums';
import { SvgXml } from 'react-native-svg';
import { likeReaction } from '../../../../../../core/assets/icons/xml';
import { addPostReaction, removePostReaction } from '../../../../../../core/legacy/feed';
import { useNavigation } from '@react-navigation/native';
import LikeButtonIconElement from '../../../../../elements/LikeButtonIconElement/LikeButtonIconElement';
import CommentButtonIconElement from '../../../../../elements/CommentButtonIconElement/CommentButtonIconElement';
import { useBehaviour } from '../../../../../providers/BehaviourProvider';
import { formatNumber } from '../../../../../../core/utils/number';
import { usePostShareAction } from './usePostShareAction';
import { ShareButton } from '../../../../../elements/ShareButton';
const FeedStyle = ({
  community,
  postId,
  pageId,
  componentId
}) => {
  const {
    themeStyles
  } = useAmityComponent({
    pageId: PageID.post_detail_page,
    componentId: ComponentID.post_content
  });
  const styles = useStyles(themeStyles);
  const {
    AmityGlobalFeedComponentBehavior
  } = useBehaviour();
  const navigation = useNavigation();
  const [postData, setPostData] = useState(null);
  const [isLike, setIsLike] = useState(false);
  const [totalReactions, setTotalReactions] = useState(0);
  const unsubscribeRef = useRef(null);
  const {
    shareLink,
    handleSharePress
  } = usePostShareAction({
    postId,
    postData,
    pageId
  });
  useEffect(() => {
    PostRepository.getPost(postId, ({
      error,
      loading,
      data
    }) => {
      if (!error && !loading) {
        var _data$myReactions;
        setPostData(data);
        setTotalReactions(data.reactionsCount);
        setIsLike(((_data$myReactions = data.myReactions) === null || _data$myReactions === void 0 ? void 0 : _data$myReactions.length) > 0);
      }
    });
    return () => (unsubscribeRef === null || unsubscribeRef === void 0 ? void 0 : unsubscribeRef.current) && (unsubscribeRef === null || unsubscribeRef === void 0 ? void 0 : unsubscribeRef.current());
  }, [postId]);
  const addReactionToPost = useCallback(async () => {
    unsubscribeRef.current = subscribeTopic(getPostTopic(postData, SubscriptionLevels.POST));
    try {
      if (isLike) {
        setIsLike(false);
        setTotalReactions(prev => prev - 1);
        await removePostReaction(postId, 'like');
      } else {
        setIsLike(true);
        setTotalReactions(prev => prev + 1);
        await addPostReaction(postId, 'like');
      }
    } catch (error) {
      console.log(error);
    }
  }, [isLike, postData, postId]);
  const onPressComment = useCallback(() => {
    if (AmityGlobalFeedComponentBehavior.goToPostDetailPage) {
      return AmityGlobalFeedComponentBehavior.goToPostDetailPage();
    }
    return navigation.navigate('PostDetail', {
      postId: postId
    });
  }, [AmityGlobalFeedComponentBehavior, navigation, postId]);
  if (community && community.isJoined === false) {
    return /*#__PURE__*/React.createElement(View, {
      style: styles.actionSection
    }, /*#__PURE__*/React.createElement(Text, {
      style: styles.btnText
    }, "Join community to interact with all posts"));
  }
  return /*#__PURE__*/React.createElement(Pressable, {
    onPress: onPressComment,
    style: styles.actionSection
  }, /*#__PURE__*/React.createElement(View, {
    style: styles.row
  }, /*#__PURE__*/React.createElement(TouchableOpacity, {
    onPress: addReactionToPost,
    style: styles.likeBtn
  }, isLike ? /*#__PURE__*/React.createElement(SvgXml, {
    xml: likeReaction(themeStyles.colors.background),
    width: "20",
    height: "20"
  }) : /*#__PURE__*/React.createElement(LikeButtonIconElement, {
    pageID: pageId,
    componentID: componentId,
    width: 20,
    height: 20,
    resizeMode: "contain"
  }), /*#__PURE__*/React.createElement(Text, {
    style: isLike ? styles.likedText : styles.btnText
  }, formatNumber(totalReactions))), /*#__PURE__*/React.createElement(TouchableOpacity, {
    style: styles.commentBtn,
    onPress: onPressComment
  }, /*#__PURE__*/React.createElement(CommentButtonIconElement, {
    pageID: pageId,
    componentID: componentId,
    width: 20,
    height: 20,
    resizeMode: "contain"
  }), /*#__PURE__*/React.createElement(Text, {
    style: styles.btnText
  }, postData === null || postData === void 0 ? void 0 : postData.localCommentCount))), shareLink && /*#__PURE__*/React.createElement(View, {
    style: styles.commentBtn
  }, /*#__PURE__*/React.createElement(ShareButton, {
    pageId: pageId,
    componentId: componentId,
    onPress: handleSharePress
  })));
};
export default /*#__PURE__*/memo(FeedStyle);
//# sourceMappingURL=FeedStyle.js.map