import { memo, useEffect, useState } from 'react';
import { View, Text, TouchableOpacity, Pressable } from 'react-native';
import { SvgXml } from 'react-native-svg';
import { arrowForward } from '../../../../../core/assets/icons/xml';
import { useStyles } from './styles';
import { getCommunityById } from '../../../../../core/legacy/community';
import { useNavigation } from '@react-navigation/native';
import { useTheme } from 'react-native-paper';
import { ComponentID, ElementID, PageID } from '../../../../enums';
import AvatarElement from '../../../../elements/CommonElements/AvatarElement';
import ModeratorBadgeElement from '../../../../elements/ModeratorBadgeElement/ModeratorBadgeElement';
import { BrandBadge } from '../../../../elements/BrandBadge';
import AmityPostEngagementActionsComponent from '../EngagementActions';
import { useAmityComponent, useUser, isModerator } from '../../../../hooks';
import { AmityPostCategory, AmityPostContentComponentStyleEnum } from '../../../../enums/AmityPostContentComponentStyle';
import TimestampElement from '../../../../elements/TimestampElement/TimestampElement';
import { useBehaviour } from '../../../../providers/BehaviourProvider';
import PostContent from '../../../../components/PostContent';
import { PostMenu } from '../../../../components/PostMenu';
import PinBadge from '../../../../elements/PinBadge';
import AnnouncementBadge from '../../../../elements/AnnouncementBadge';
import { Typography } from '../../../../../core/components/Typography/Typography';
const AmityPostContentComponent = ({
  pageId = PageID.WildCardPage,
  post,
  showedAllOptions,
  AmityPostContentComponentStyle = AmityPostContentComponentStyleEnum.detail,
  isCommunityNameShown = true,
  category = AmityPostCategory.GENERAL
}) => {
  const theme = useTheme();
  const {
    AmityPostContentComponentBehavior,
    AmityGlobalFeedComponentBehavior,
    AmityCommunityProfilePageBehavior,
    AmityUserFeedComponentBehavior
  } = useBehaviour();
  const componentId = ComponentID.post_content;
  const {
    accessibilityId,
    themeStyles
  } = useAmityComponent({
    pageId: pageId,
    componentId: componentId
  });
  const styles = useStyles(themeStyles);
  const [textPost, setTextPost] = useState('');
  const [communityData, setCommunityData] = useState(null);
  const navigation = useNavigation();
  const [mentionPositionArr, setMentionsPositionArr] = 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 = useUser((creator === null || creator === void 0 ? void 0 : creator.userId) || '');
  const isCommunityModerator = isModerator(creatorUser === null || creatorUser === void 0 ? void 0 : creatorUser.roles);
  useEffect(() => {
    if (mentionPosition) {
      setMentionsPositionArr(mentionPosition);
    }
  }, [mentionPosition]);
  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 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(View, null, (category === AmityPostCategory.ANNOUNCEMENT || category === AmityPostCategory.PIN_AND_ANNOUNCEMENT) && /*#__PURE__*/React.createElement(View, {
    style: styles.featuredBadgeContainer
  }, /*#__PURE__*/React.createElement(AnnouncementBadge, {
    componentId: ComponentID.post_content
  })), /*#__PURE__*/React.createElement(View, {
    key: postId,
    style: styles.postWrap,
    testID: accessibilityId,
    accessibilityLabel: accessibilityId
  }, /*#__PURE__*/React.createElement(Pressable, {
    style: styles.headerSection,
    onPress: onPressPost
  }, /*#__PURE__*/React.createElement(View, {
    style: styles.user
  }, creator !== null && creator !== void 0 && creator.userId ? /*#__PURE__*/React.createElement(TouchableOpacity, {
    onPress: e => {
      e.stopPropagation();
      handleDisplayNamePress();
    }
  }, /*#__PURE__*/React.createElement(AvatarElement, {
    style: styles.avatar,
    avatarId: creator === null || creator === void 0 ? void 0 : creator.avatarFileId,
    pageID: pageId,
    elementID: 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, {
    style: styles.avatar,
    avatarId: creator === null || creator === void 0 ? void 0 : creator.avatarFileId,
    pageID: pageId,
    elementID: 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(View, {
    style: styles.fillSpace
  }, /*#__PURE__*/React.createElement(View, {
    style: styles.headerRow
  }, /*#__PURE__*/React.createElement(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.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, {
    width: 20,
    height: 20,
    accessible: true,
    accessibilityLabel: "Brand verified"
  }), (communityData === null || communityData === void 0 ? void 0 : communityData.displayName) && isCommunityNameShown && /*#__PURE__*/React.createElement(View, {
    style: styles.communityNameContainer
  }, /*#__PURE__*/React.createElement(SvgXml, {
    style: styles.arrow,
    xml: arrowForward(theme.colors.baseShade1),
    width: "8",
    height: "8"
  }), /*#__PURE__*/React.createElement(TouchableOpacity, {
    onPress: handleCommunityNamePress,
    style: styles.fillSpace
  }, /*#__PURE__*/React.createElement(Typography.BodyBold, {
    ellipsizeMode: "tail",
    numberOfLines: 1,
    style: styles.headerText
  }, communityData === null || communityData === void 0 ? void 0 : communityData.displayName)))), /*#__PURE__*/React.createElement(View, {
    style: styles.timeRow
  }, isCommunityModerator && /*#__PURE__*/React.createElement(View, {
    style: styles.row
  }, /*#__PURE__*/React.createElement(ModeratorBadgeElement, {
    pageID: pageId,
    componentID: componentId,
    communityId: targetType === 'community' && targetId,
    userId: creator === null || creator === void 0 ? void 0 : creator.userId
  }), /*#__PURE__*/React.createElement(Text, {
    style: styles.dot
  }, "\xB7")), /*#__PURE__*/React.createElement(TimestampElement, {
    createdAt: createdAt,
    style: styles.headerTextTime,
    componentID: componentId
  }), editedAt !== createdAt && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Text, {
    style: styles.dot
  }, "\xB7"), /*#__PURE__*/React.createElement(Text, {
    style: styles.headerTextTime
  }, "Edited"))))), (category === AmityPostCategory.PIN || category === AmityPostCategory.PIN_AND_ANNOUNCEMENT) && /*#__PURE__*/React.createElement(PinBadge, {
    componentId: ComponentID.post_content
  }), AmityPostContentComponentStyle === AmityPostContentComponentStyleEnum.feed && /*#__PURE__*/React.createElement(PostMenu, {
    post: post,
    pageId: pageId,
    componentId: componentId
  })), /*#__PURE__*/React.createElement(View, null, /*#__PURE__*/React.createElement(View, {
    style: styles.bodySection
  }, /*#__PURE__*/React.createElement(PostContent, {
    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(AmityPostEngagementActionsComponent, {
    pageId: pageId,
    componentId: componentId,
    AmityPostContentComponentStyle: AmityPostContentComponentStyle,
    targetType: targetType,
    targetId: targetId,
    postId: postId
  }))));
};
export default /*#__PURE__*/memo(AmityPostContentComponent);
//# sourceMappingURL=Content.js.map