"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _reactNative = require("react-native");
var _react = require("react");
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _styles = require("./styles");
var _hooks = require("../../../../../hooks");
var _enums = require("../../../../../enums");
var _reactNativeSvg = require("react-native-svg");
var _xml = require("../../../../../../core/assets/icons/xml");
var _feed = require("../../../../../../core/legacy/feed");
var _native = require("@react-navigation/native");
var _LikeButtonIconElement = _interopRequireDefault(require("../../../../../elements/LikeButtonIconElement/LikeButtonIconElement"));
var _CommentButtonIconElement = _interopRequireDefault(require("../../../../../elements/CommentButtonIconElement/CommentButtonIconElement"));
var _BehaviourProvider = require("../../../../../providers/BehaviourProvider");
var _number = require("../../../../../../core/utils/number");
var _usePostShareAction = require("./usePostShareAction");
var _ShareButton = require("../../../../../elements/ShareButton");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const FeedStyle = ({
  community,
  postId,
  pageId,
  componentId
}) => {
  const {
    themeStyles
  } = (0, _hooks.useAmityComponent)({
    pageId: _enums.PageID.post_detail_page,
    componentId: _enums.ComponentID.post_content
  });
  const styles = (0, _styles.useStyles)(themeStyles);
  const {
    AmityGlobalFeedComponentBehavior
  } = (0, _BehaviourProvider.useBehaviour)();
  const navigation = (0, _native.useNavigation)();
  const [postData, setPostData] = (0, _react.useState)(null);
  const [isLike, setIsLike] = (0, _react.useState)(false);
  const [totalReactions, setTotalReactions] = (0, _react.useState)(0);
  const unsubscribeRef = (0, _react.useRef)(null);
  const {
    shareLink,
    handleSharePress
  } = (0, _usePostShareAction.usePostShareAction)({
    postId,
    postData,
    pageId
  });
  (0, _react.useEffect)(() => {
    _tsSdkReactNative.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 = (0, _react.useCallback)(async () => {
    unsubscribeRef.current = (0, _tsSdkReactNative.subscribeTopic)((0, _tsSdkReactNative.getPostTopic)(postData, _tsSdkReactNative.SubscriptionLevels.POST));
    try {
      if (isLike) {
        setIsLike(false);
        setTotalReactions(prev => prev - 1);
        await (0, _feed.removePostReaction)(postId, 'like');
      } else {
        setIsLike(true);
        setTotalReactions(prev => prev + 1);
        await (0, _feed.addPostReaction)(postId, 'like');
      }
    } catch (error) {
      console.log(error);
    }
  }, [isLike, postData, postId]);
  const onPressComment = (0, _react.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(_reactNative.View, {
      style: styles.actionSection
    }, /*#__PURE__*/React.createElement(_reactNative.Text, {
      style: styles.btnText
    }, "Join community to interact with all posts"));
  }
  return /*#__PURE__*/React.createElement(_reactNative.Pressable, {
    onPress: onPressComment,
    style: styles.actionSection
  }, /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.row
  }, /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
    onPress: addReactionToPost,
    style: styles.likeBtn
  }, isLike ? /*#__PURE__*/React.createElement(_reactNativeSvg.SvgXml, {
    xml: (0, _xml.likeReaction)(themeStyles.colors.background),
    width: "20",
    height: "20"
  }) : /*#__PURE__*/React.createElement(_LikeButtonIconElement.default, {
    pageID: pageId,
    componentID: componentId,
    width: 20,
    height: 20,
    resizeMode: "contain"
  }), /*#__PURE__*/React.createElement(_reactNative.Text, {
    style: isLike ? styles.likedText : styles.btnText
  }, (0, _number.formatNumber)(totalReactions))), /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
    style: styles.commentBtn,
    onPress: onPressComment
  }, /*#__PURE__*/React.createElement(_CommentButtonIconElement.default, {
    pageID: pageId,
    componentID: componentId,
    width: 20,
    height: 20,
    resizeMode: "contain"
  }), /*#__PURE__*/React.createElement(_reactNative.Text, {
    style: styles.btnText
  }, postData === null || postData === void 0 ? void 0 : postData.localCommentCount))), shareLink && /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.commentBtn
  }, /*#__PURE__*/React.createElement(_ShareButton.ShareButton, {
    pageId: pageId,
    componentId: componentId,
    onPress: handleSharePress
  })));
};
var _default = exports.default = /*#__PURE__*/(0, _react.memo)(FeedStyle);
//# sourceMappingURL=FeedStyle.js.map