import { Text, TouchableOpacity, View } from 'react-native';
import { memo, useState, useCallback, useEffect } 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 LikeButtonIconElement from '../../../../../elements/LikeButtonIconElement/LikeButtonIconElement';
import CommentButtonIconElement from '../../../../../elements/CommentButtonIconElement/CommentButtonIconElement';
import AmityReactionListComponent from '../../../../reaction/components/List';
import { formatNumber } from '../../../../../../core/utils/number';
import { usePostShareAction } from './usePostShareAction';
import { ShareButton } from '../../../../../elements/ShareButton';
const DetailStyle = ({
  community,
  postId,
  componentId,
  pageId
}) => {
  const {
    themeStyles
  } = useAmityComponent({
    pageId: PageID.post_detail_page,
    componentId: ComponentID.post_content
  });
  const styles = useStyles(themeStyles);
  const [postData, setPostData] = useState(null);
  const {
    shareLink,
    handleSharePress
  } = usePostShareAction({
    postId,
    postData,
    pageId
  });
  const [isLike, setIsLike] = useState(false);
  const [totalReactions, setTotalReactions] = useState(0);
  const [isReactionListVisible, setIsReactionListVisible] = useState(false);
  useEffect(() => {
    if (!postId) return null;
    let unsubscribe;
    const unsub = PostRepository.getPost(postId, ({
      error,
      loading,
      data
    }) => {
      if (!error && !loading) {
        var _data$myReactions;
        unsubscribe = subscribeTopic(getPostTopic(data, SubscriptionLevels.POST));
        setPostData(data);
        setTotalReactions(data.reactionsCount);
        setIsLike((data === null || data === void 0 || (_data$myReactions = data.myReactions) === null || _data$myReactions === void 0 ? void 0 : _data$myReactions.length) > 0);
      }
    });
    return () => {
      unsub();
      unsubscribe && unsubscribe();
    };
  }, [postId]);
  const renderLikeText = useCallback(likeNumber => {
    if (likeNumber === 1) return 'like';
    return 'likes';
  }, []);
  const renderCommentText = useCallback(commentNumber => {
    if (commentNumber === 1) return 'comment';
    return 'comments';
  }, []);
  const addReactionToPost = useCallback(async () => {
    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, postId]);
  const onClickReactions = useCallback(() => {
    setIsReactionListVisible(true);
  }, []);
  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(React.Fragment, null, /*#__PURE__*/React.createElement(View, {
    style: styles.countSection
  }, totalReactions ? /*#__PURE__*/React.createElement(View, {
    style: styles.row
  }, /*#__PURE__*/React.createElement(SvgXml, {
    width: "20",
    style: {
      marginRight: 4
    },
    height: "16",
    xml: likeReaction(themeStyles.colors.background)
  }), /*#__PURE__*/React.createElement(Text, {
    style: styles.likeCountText,
    onPress: onClickReactions
  }, formatNumber(totalReactions), " ", renderLikeText(totalReactions))) : /*#__PURE__*/React.createElement(Text, {
    style: styles.likeCountText,
    onPress: onClickReactions
  }, formatNumber(totalReactions), " ", renderLikeText(totalReactions)), /*#__PURE__*/React.createElement(Text, {
    style: styles.commentCountText
  }, formatNumber(postData === null || postData === void 0 ? void 0 : postData.localCommentCount), ' ', renderCommentText(postData === null || postData === void 0 ? void 0 : postData.localCommentCount))), /*#__PURE__*/React.createElement(View, {
    style: [styles.actionSection, styles.detailActionSection]
  }, /*#__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: "16"
  }) : /*#__PURE__*/React.createElement(LikeButtonIconElement, {
    pageID: pageId,
    componentID: componentId,
    width: 20,
    height: 20,
    resizeMode: "contain"
  }), /*#__PURE__*/React.createElement(Text, {
    style: isLike ? styles.likedText : styles.btnText
  }, "Like")), /*#__PURE__*/React.createElement(View, {
    style: styles.commentBtn
  }, /*#__PURE__*/React.createElement(CommentButtonIconElement, {
    pageID: pageId,
    componentID: componentId,
    width: 20,
    height: 20,
    resizeMode: "contain"
  }), /*#__PURE__*/React.createElement(Text, {
    style: styles.btnText
  }, "Comment"))), shareLink && /*#__PURE__*/React.createElement(View, {
    style: styles.commentBtn
  }, /*#__PURE__*/React.createElement(ShareButton, {
    pageId: pageId,
    componentId: componentId,
    onPress: handleSharePress
  })), isReactionListVisible && /*#__PURE__*/React.createElement(AmityReactionListComponent, {
    referenceId: postId,
    referenceType: "post",
    isModalVisible: isReactionListVisible,
    onCloseModal: () => setIsReactionListVisible(false)
  })));
};
export default /*#__PURE__*/memo(DetailStyle);
//# sourceMappingURL=DetailStyle.js.map