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