"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
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 _community = require("../../../../../core/legacy/community");
var _native = require("@react-navigation/native");
var _reactNativePaper = require("react-native-paper");
var _enums = require("../../../../enums");
var _AvatarElement = _interopRequireDefault(require("../../../../elements/CommonElements/AvatarElement"));
var _ModeratorBadgeElement = _interopRequireDefault(require("../../../../elements/ModeratorBadgeElement/ModeratorBadgeElement"));
var _BrandBadge = require("../../../../elements/BrandBadge");
var _EngagementActions = _interopRequireDefault(require("../EngagementActions"));
var _hooks = require("../../../../hooks");
var _AmityPostContentComponentStyle = require("../../../../enums/AmityPostContentComponentStyle");
var _TimestampElement = _interopRequireDefault(require("../../../../elements/TimestampElement/TimestampElement"));
var _BehaviourProvider = require("../../../../providers/BehaviourProvider");
var _PostContent = _interopRequireDefault(require("../../../../components/PostContent"));
var _PostMenu = require("../../../../components/PostMenu");
var _PinBadge = _interopRequireDefault(require("../../../../elements/PinBadge"));
var _AnnouncementBadge = _interopRequireDefault(require("../../../../elements/AnnouncementBadge"));
var _Typography = require("../../../../../core/components/Typography/Typography");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const AmityPostContentComponent = ({
  pageId = _enums.PageID.WildCardPage,
  post,
  showedAllOptions,
  AmityPostContentComponentStyle = _AmityPostContentComponentStyle.AmityPostContentComponentStyleEnum.detail,
  isCommunityNameShown = true,
  category = _AmityPostContentComponentStyle.AmityPostCategory.GENERAL
}) => {
  const theme = (0, _reactNativePaper.useTheme)();
  const {
    AmityPostContentComponentBehavior,
    AmityGlobalFeedComponentBehavior,
    AmityCommunityProfilePageBehavior,
    AmityUserFeedComponentBehavior
  } = (0, _BehaviourProvider.useBehaviour)();
  const componentId = _enums.ComponentID.post_content;
  const {
    accessibilityId,
    themeStyles
  } = (0, _hooks.useAmityComponent)({
    pageId: pageId,
    componentId: componentId
  });
  const styles = (0, _styles.useStyles)(themeStyles);
  const [textPost, setTextPost] = (0, _react.useState)('');
  const [communityData, setCommunityData] = (0, _react.useState)(null);
  const navigation = (0, _native.useNavigation)();
  const [mentionPositionArr, setMentionsPositionArr] = (0, _react.useState)([]);
  const {
    postId,
    data,
    myReactions = [],
    reactions: reactionCount,
    createdAt,
    creator,
    targetType,
    targetId,
    children = [],
    editedAt,
    metadata
  } = post ?? {};
  const mentionPosition = metadata === null || metadata === void 0 ? void 0 : metadata.mentioned;
  const creatorUser = (0, _hooks.useUser)((creator === null || creator === void 0 ? void 0 : creator.userId) || '');
  const isCommunityModerator = (0, _hooks.isModerator)(creatorUser === null || creatorUser === void 0 ? void 0 : creatorUser.roles);
  (0, _react.useEffect)(() => {
    if (mentionPosition) {
      setMentionsPositionArr(mentionPosition);
    }
  }, [mentionPosition]);
  (0, _react.useEffect)(() => {
    setTextPost(data === null || data === void 0 ? void 0 : data.text);
    if (targetType === 'community' && targetId) {
      getCommunityInfo(targetId);
    }
  }, [data, myReactions, reactionCount === null || reactionCount === void 0 ? void 0 : reactionCount.like, targetId, targetType]);
  async function getCommunityInfo(id) {
    const {
      data: community
    } = await (0, _community.getCommunityById)(id);
    if (community.error) return;
    if (!community.loading) {
      setCommunityData(community === null || community === void 0 ? void 0 : community.data);
    }
  }
  const handleDisplayNamePress = () => {
    if (!(creator !== null && creator !== void 0 && creator.userId)) return null;
    if (AmityPostContentComponentBehavior !== null && AmityPostContentComponentBehavior !== void 0 && AmityPostContentComponentBehavior.goToUserProfilePage) {
      return AmityPostContentComponentBehavior.goToUserProfilePage({
        userId: creator.userId
      });
    }
    return navigation.navigate('UserProfile', {
      userId: creator.userId
    });
  };
  const handleCommunityNamePress = () => {
    if (targetType !== 'community' || !targetId) return null;
    if (AmityPostContentComponentBehavior !== null && AmityPostContentComponentBehavior !== void 0 && AmityPostContentComponentBehavior.goToCommunityProfilePage) {
      return AmityPostContentComponentBehavior.goToCommunityProfilePage({
        communityId: targetId,
        communityName: communityData === null || communityData === void 0 ? void 0 : communityData.displayName
      });
    }
    return navigation.navigate('CommunityProfilePage', {
      communityId: targetId
    });
  };
  const onPressPost = () => {
    if (AmityGlobalFeedComponentBehavior.goToPostDetailPage) {
      return AmityGlobalFeedComponentBehavior.goToPostDetailPage(postId);
    }
    if (AmityCommunityProfilePageBehavior.goToPostDetailPage) {
      return AmityCommunityProfilePageBehavior.goToPostDetailPage(postId);
    }
    if (AmityUserFeedComponentBehavior !== null && AmityUserFeedComponentBehavior !== void 0 && AmityUserFeedComponentBehavior.goToPostDetailPage) {
      return AmityUserFeedComponentBehavior.goToPostDetailPage(postId);
    }
    return navigation.navigate('PostDetail', {
      postId: postId,
      category: category
    });
  };
  return /*#__PURE__*/React.createElement(_reactNative.View, null, (category === _AmityPostContentComponentStyle.AmityPostCategory.ANNOUNCEMENT || category === _AmityPostContentComponentStyle.AmityPostCategory.PIN_AND_ANNOUNCEMENT) && /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.featuredBadgeContainer
  }, /*#__PURE__*/React.createElement(_AnnouncementBadge.default, {
    componentId: _enums.ComponentID.post_content
  })), /*#__PURE__*/React.createElement(_reactNative.View, {
    key: postId,
    style: styles.postWrap,
    testID: accessibilityId,
    accessibilityLabel: accessibilityId
  }, /*#__PURE__*/React.createElement(_reactNative.Pressable, {
    style: styles.headerSection,
    onPress: onPressPost
  }, /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.user
  }, creator !== null && creator !== void 0 && creator.userId ? /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
    onPress: e => {
      e.stopPropagation();
      handleDisplayNamePress();
    }
  }, /*#__PURE__*/React.createElement(_AvatarElement.default, {
    style: styles.avatar,
    avatarId: creator === null || creator === void 0 ? void 0 : creator.avatarFileId,
    pageID: pageId,
    elementID: _enums.ElementID.WildCardElement,
    componentID: componentId,
    avatarCustomUrl: creator === null || creator === void 0 ? void 0 : creator.avatarCustomUrl,
    targetType: "user",
    displayName: creator === null || creator === void 0 ? void 0 : creator.displayName
  })) : /*#__PURE__*/React.createElement(_AvatarElement.default, {
    style: styles.avatar,
    avatarId: creator === null || creator === void 0 ? void 0 : creator.avatarFileId,
    pageID: pageId,
    elementID: _enums.ElementID.WildCardElement,
    componentID: componentId,
    avatarCustomUrl: creator === null || creator === void 0 ? void 0 : creator.avatarCustomUrl,
    targetType: "user",
    displayName: creator === null || creator === void 0 ? void 0 : creator.displayName
  }), /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.fillSpace
  }, /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.headerRow
  }, /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
    style: targetType === 'community' && isCommunityNameShown && !!(communityData !== null && communityData !== void 0 && communityData.displayName) ? styles.headerTextContainer // global feed: cap at 40% to leave room for community name
    : styles.headerTextContainerUser // community feed: fill available width
    ,
    onPress: handleDisplayNamePress
  }, /*#__PURE__*/React.createElement(_Typography.Typography.BodyBold, {
    ellipsizeMode: "tail",
    numberOfLines: 1,
    style: styles.headerText
  }, creator === null || creator === void 0 ? void 0 : creator.displayName)), (creator === null || creator === void 0 ? void 0 : creator.isBrand) && /*#__PURE__*/React.createElement(_BrandBadge.BrandBadge, {
    width: 20,
    height: 20,
    accessible: true,
    accessibilityLabel: "Brand verified"
  }), (communityData === null || communityData === void 0 ? void 0 : communityData.displayName) && isCommunityNameShown && /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.communityNameContainer
  }, /*#__PURE__*/React.createElement(_reactNativeSvg.SvgXml, {
    style: styles.arrow,
    xml: (0, _xml.arrowForward)(theme.colors.baseShade1),
    width: "8",
    height: "8"
  }), /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
    onPress: handleCommunityNamePress,
    style: styles.fillSpace
  }, /*#__PURE__*/React.createElement(_Typography.Typography.BodyBold, {
    ellipsizeMode: "tail",
    numberOfLines: 1,
    style: styles.headerText
  }, communityData === null || communityData === void 0 ? void 0 : communityData.displayName)))), /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.timeRow
  }, isCommunityModerator && /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.row
  }, /*#__PURE__*/React.createElement(_ModeratorBadgeElement.default, {
    pageID: pageId,
    componentID: componentId,
    communityId: targetType === 'community' && targetId,
    userId: creator === null || creator === void 0 ? void 0 : creator.userId
  }), /*#__PURE__*/React.createElement(_reactNative.Text, {
    style: styles.dot
  }, "\xB7")), /*#__PURE__*/React.createElement(_TimestampElement.default, {
    createdAt: createdAt,
    style: styles.headerTextTime,
    componentID: componentId
  }), editedAt !== createdAt && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(_reactNative.Text, {
    style: styles.dot
  }, "\xB7"), /*#__PURE__*/React.createElement(_reactNative.Text, {
    style: styles.headerTextTime
  }, "Edited"))))), (category === _AmityPostContentComponentStyle.AmityPostCategory.PIN || category === _AmityPostContentComponentStyle.AmityPostCategory.PIN_AND_ANNOUNCEMENT) && /*#__PURE__*/React.createElement(_PinBadge.default, {
    componentId: _enums.ComponentID.post_content
  }), AmityPostContentComponentStyle === _AmityPostContentComponentStyle.AmityPostContentComponentStyleEnum.feed && /*#__PURE__*/React.createElement(_PostMenu.PostMenu, {
    post: post,
    pageId: pageId,
    componentId: componentId
  })), /*#__PURE__*/React.createElement(_reactNative.View, null, /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.bodySection
  }, /*#__PURE__*/React.createElement(_PostContent.default, {
    post: post,
    textPost: textPost,
    childrenPosts: children,
    onPressPost: onPressPost,
    showedAllOptions: showedAllOptions,
    mentionPositionArr: mentionPositionArr,
    disabledPoll: (communityData === null || communityData === void 0 ? void 0 : communityData.isPublic) && !(communityData !== null && communityData !== void 0 && communityData.isJoined)
  })), /*#__PURE__*/React.createElement(_EngagementActions.default, {
    pageId: pageId,
    componentId: componentId,
    AmityPostContentComponentStyle: AmityPostContentComponentStyle,
    targetType: targetType,
    targetId: targetId,
    postId: postId
  }))));
};
var _default = exports.default = /*#__PURE__*/(0, _react.memo)(AmityPostContentComponent);
//# sourceMappingURL=Content.js.map