"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.PostMenu = PostMenu;
var _react = require("react");
var _reactNative = require("react-native");
var _community = require("../../../core/legacy/community");
var _native = require("@react-navigation/native");
var _hooks = require("../../hooks");
var _feed = require("../../../core/legacy/feed");
var _useAuth = _interopRequireDefault(require("../../../core/hooks/useAuth"));
var _globalfeedSlice = _interopRequireDefault(require("../../../core/stores/slices/globalfeedSlice"));
var _icons = require("../../../core/assets/icons");
var _toastSlice = require("../../../core/stores/slices/toastSlice");
var _enums = require("../../enums");
var _useClosePoll = require("../../hooks/queries/useClosePoll");
var _store = require("../../../core/stores/store");
var _MenuButton = _interopRequireDefault(require("../../elements/MenuButton/MenuButton"));
var _MenuAction = _interopRequireDefault(require("../../elements/MenuAction/MenuAction"));
var _bottomSheetSlice = require("../../../core/stores/slices/bottomSheetSlice");
var _useUser = require("../../hooks/useUser");
var _permissions = require("../../utils/permissions");
var _usePostShareAction = require("../../features/post/components/EngagementActions/Components/usePostShareAction");
var _CopyLinkAction = require("../../elements/CopyLinkAction");
var _ShareAction = require("../../elements/ShareAction");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function PostMenu({
  pageId,
  componentId,
  post
}) {
  var _post$childrenPosts, _post$creator, _post$creator2, _post$creator3, _post$creator4, _getPollInfo, _post$creator5;
  const {
    showToast
  } = (0, _toastSlice.useToast)();
  const {
    closePoll
  } = (0, _useClosePoll.useClosePoll)();
  const {
    client
  } = (0, _useAuth.default)();
  const [communityData, setCommunityData] = (0, _react.useState)(null);
  const {
    deleteByPostId
  } = _globalfeedSlice.default.actions;
  const dispatch = (0, _store.useUIKitDispatch)();
  const navigation = (0, _native.useNavigation)();
  const {
    openBottomSheet,
    closeBottomSheet,
    bottomSheetHeight
  } = (0, _bottomSheetSlice.useBottomSheet)();
  const {
    isFlaggedByMe,
    reportPost,
    unreportPost
  } = (0, _hooks.useFlagPost)({
    postId: post === null || post === void 0 ? void 0 : post.postId
  });
  const {
    postId,
    targetType,
    targetId
  } = post ?? {};
  const myId = client.userId;
  const currentUser = (0, _useUser.useUser)(myId);
  const isGlobalAdmin = (0, _permissions.isAdmin)(currentUser === null || currentUser === void 0 ? void 0 : currentUser.roles);
  const {
    shareLink
  } = (0, _usePostShareAction.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];
  (0, _react.useEffect)(() => {
    if (targetType === 'community' && targetId) {
      getCommunityInfo(targetId);
    }
  }, [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 goToEditPost = () => {
    closeBottomSheet();
    navigation.navigate('EditPost', {
      post,
      community: communityData
    });
  };
  const deletePost = async () => {
    try {
      const deleted = await (0, _feed.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();
    _reactNative.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 === _enums.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.default, {
      key: "report",
      gap: "small",
      label: 'Report post',
      iconProps: {
        xml: (0, _icons.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.default, {
      key: "unreport",
      gap: "small",
      iconProps: {
        xml: (0, _icons.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.default, {
      key: "edit",
      gap: "small",
      iconProps: {
        xml: (0, _icons.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.default, {
      key: "close-poll",
      gap: "small",
      iconProps: {
        xml: (0, _icons.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.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.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 || (0, _hooks.isModerator)(currentUser === null || currentUser === void 0 ? void 0 : currentUser.roles) || isGlobalAdmin,
    action: /*#__PURE__*/React.createElement(_MenuAction.default, {
      key: "delete",
      gap: "small",
      danger: true,
      iconProps: {
        xml: (0, _icons.trash)()
      },
      label: "Delete post",
      onPress: onDeletePost
    })
  }].filter(({
    show
  }) => show);
  return /*#__PURE__*/React.createElement(_MenuButton.default, {
    pageId: pageId,
    componentId: componentId,
    hitSlop: 12,
    onPress: () => {
      openBottomSheet({
        height: bottomSheetHeight[actions.length],
        content: /*#__PURE__*/React.createElement(_reactNative.View, null, actions.map(({
          action
        }) => action))
      });
    }
  });
}
//# sourceMappingURL=index.js.map