import { useEffect, useState } from 'react';
import { Alert, View } from 'react-native';
import { getCommunityById } from '../../../core/legacy/community';
import { useNavigation } from '@react-navigation/native';
import { useFlagPost, isModerator } from '../../hooks';
import { deletePostById } from '../../../core/legacy/feed';
import useAuth from '../../../core/hooks/useAuth';
import globalFeedSlice from '../../../core/stores/slices/globalfeedSlice';
import { pen, poll, report, trash, unreport } from '../../../core/assets/icons';
import { useToast } from '../../../core/stores/slices/toastSlice';
import { PageID } from '../../enums';
import { useClosePoll } from '../../hooks/queries/useClosePoll';
import { useUIKitDispatch } from '../../../core/stores/store';
import MenuButton from '../../elements/MenuButton/MenuButton';
import MenuAction from '../../elements/MenuAction/MenuAction';
import { useBottomSheet } from '../../../core/stores/slices/bottomSheetSlice';
import { useUser } from '../../hooks/useUser';
import { isAdmin } from '../../utils/permissions';
import { usePostShareAction } from '../../features/post/components/EngagementActions/Components/usePostShareAction';
import { CopyLinkAction } from '../../elements/CopyLinkAction';
import { ShareAction } from '../../elements/ShareAction';
export function PostMenu({
  pageId,
  componentId,
  post
}) {
  var _post$childrenPosts, _post$creator, _post$creator2, _post$creator3, _post$creator4, _getPollInfo, _post$creator5;
  const {
    showToast
  } = useToast();
  const {
    closePoll
  } = useClosePoll();
  const {
    client
  } = useAuth();
  const [communityData, setCommunityData] = useState(null);
  const {
    deleteByPostId
  } = globalFeedSlice.actions;
  const dispatch = useUIKitDispatch();
  const navigation = useNavigation();
  const {
    openBottomSheet,
    closeBottomSheet,
    bottomSheetHeight
  } = useBottomSheet();
  const {
    isFlaggedByMe,
    reportPost,
    unreportPost
  } = useFlagPost({
    postId: post === null || post === void 0 ? void 0 : post.postId
  });
  const {
    postId,
    targetType,
    targetId
  } = post ?? {};
  const myId = client.userId;
  const currentUser = useUser(myId);
  const isGlobalAdmin = isAdmin(currentUser === null || currentUser === void 0 ? void 0 : currentUser.roles);
  const {
    shareLink
  } = usePostShareAction({
    postId,
    postData: post,
    pageId
  });
  const childrenPost = post === null || post === void 0 || (_post$childrenPosts = post.childrenPosts) === null || _post$childrenPosts === void 0 ? void 0 : _post$childrenPosts[0];
  useEffect(() => {
    if (targetType === 'community' && targetId) {
      getCommunityInfo(targetId);
    }
  }, [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 goToEditPost = () => {
    closeBottomSheet();
    navigation.navigate('EditPost', {
      post,
      community: communityData
    });
  };
  const deletePost = async () => {
    try {
      const deleted = await deletePostById(postId);
      if (deleted) {
        dispatch(deleteByPostId({
          postId
        }));
        showToast({
          type: 'success',
          message: 'Post deleted.'
        });
      }
    } catch (error) {
      showToast({
        type: 'failed',
        message: 'Failed to delete post. Please try again.'
      });
    }
  };
  const onDeletePost = () => {
    closeBottomSheet();
    Alert.alert('Delete this post', `This post will be permanently deleted. You'll no longer see and find this post`, [{
      text: 'Cancel',
      style: 'cancel'
    }, {
      text: 'Delete',
      style: 'destructive',
      onPress: () => {
        deletePost();
        pageId === PageID.post_detail_page && navigation.pop();
      }
    }]);
  };
  const actions = [{
    show: (post === null || post === void 0 || (_post$creator = post.creator) === null || _post$creator === void 0 ? void 0 : _post$creator.userId) !== myId && !isFlaggedByMe,
    action: /*#__PURE__*/React.createElement(MenuAction, {
      key: "report",
      gap: "small",
      label: 'Report post',
      iconProps: {
        xml: report()
      },
      onPress: () => {
        closeBottomSheet();
        reportPost(postId);
      }
    })
  }, {
    show: (post === null || post === void 0 || (_post$creator2 = post.creator) === null || _post$creator2 === void 0 ? void 0 : _post$creator2.userId) !== myId && isFlaggedByMe,
    action: /*#__PURE__*/React.createElement(MenuAction, {
      key: "unreport",
      gap: "small",
      iconProps: {
        xml: unreport()
      },
      label: 'Unreport post',
      onPress: () => {
        closeBottomSheet();
        unreportPost(postId);
      }
    })
  }, {
    show: (post === null || post === void 0 || (_post$creator3 = post.creator) === null || _post$creator3 === void 0 ? void 0 : _post$creator3.userId) === myId && (childrenPost === null || childrenPost === void 0 ? void 0 : childrenPost.dataType) !== 'room' && (childrenPost === null || childrenPost === void 0 ? void 0 : childrenPost.dataType) !== 'poll',
    action: /*#__PURE__*/React.createElement(MenuAction, {
      key: "edit",
      gap: "small",
      iconProps: {
        xml: pen()
      },
      label: "Edit post",
      onPress: goToEditPost
    })
  }, {
    show: (post === null || post === void 0 || (_post$creator4 = post.creator) === null || _post$creator4 === void 0 ? void 0 : _post$creator4.userId) === myId && (childrenPost === null || childrenPost === void 0 ? void 0 : childrenPost.dataType) === 'poll' && (childrenPost === null || childrenPost === void 0 || (_getPollInfo = childrenPost.getPollInfo()) === null || _getPollInfo === void 0 ? void 0 : _getPollInfo.status) === 'open',
    action: /*#__PURE__*/React.createElement(MenuAction, {
      key: "close-poll",
      gap: "small",
      iconProps: {
        xml: poll()
      },
      label: "Close poll",
      onPress: () => {
        var _data;
        closeBottomSheet();
        closePoll(childrenPost === null || childrenPost === void 0 || (_data = childrenPost.data) === null || _data === void 0 ? void 0 : _data.pollId);
      }
    })
  }, {
    show: !!shareLink && targetType === 'community' && !(communityData !== null && communityData !== void 0 && communityData.isJoined),
    action: /*#__PURE__*/React.createElement(CopyLinkAction, {
      key: "copy-link",
      link: shareLink,
      pageId: pageId,
      componentId: componentId,
      onPress: closeBottomSheet
    })
  }, {
    show: !!shareLink && targetType === 'community' && !(communityData !== null && communityData !== void 0 && communityData.isJoined),
    action: /*#__PURE__*/React.createElement(ShareAction, {
      key: "share",
      link: shareLink,
      pageId: pageId,
      componentId: componentId,
      onPress: closeBottomSheet
    })
  }, {
    show: (post === null || post === void 0 || (_post$creator5 = post.creator) === null || _post$creator5 === void 0 ? void 0 : _post$creator5.userId) === myId || isModerator(currentUser === null || currentUser === void 0 ? void 0 : currentUser.roles) || isGlobalAdmin,
    action: /*#__PURE__*/React.createElement(MenuAction, {
      key: "delete",
      gap: "small",
      danger: true,
      iconProps: {
        xml: trash()
      },
      label: "Delete post",
      onPress: onDeletePost
    })
  }].filter(({
    show
  }) => show);
  return /*#__PURE__*/React.createElement(MenuButton, {
    pageId: pageId,
    componentId: componentId,
    hitSlop: 12,
    onPress: () => {
      openBottomSheet({
        height: bottomSheetHeight[actions.length],
        content: /*#__PURE__*/React.createElement(View, null, actions.map(({
          action
        }) => action))
      });
    }
  });
}
//# sourceMappingURL=index.js.map