"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _styles = require("./styles");
var _reactNative = require("react-native");
var _Typography = require("../../../../../../core/components/Typography/Typography");
var _Avatar = _interopRequireDefault(require("../../../../../components/Avatar"));
var _native = require("@react-navigation/native");
var _Timestamp = _interopRequireDefault(require("../../../../../elements/Timestamp"));
var _enums = require("../../../../../enums");
var _MenuButton = _interopRequireDefault(require("../../../../../elements/MenuButton"));
var _useAuth = _interopRequireDefault(require("../../../../../../core/hooks/useAuth"));
var _bottomSheetSlice = require("../../../../../../core/stores/slices/bottomSheetSlice");
var _icons = require("../../../../../../core/assets/icons");
var _MenuAction = _interopRequireDefault(require("../../../../../elements/MenuAction"));
var _PostContent = _interopRequireDefault(require("../../../../../components/PostContent"));
var _FormDescription = _interopRequireDefault(require("../../../../../elements/FormDescription"));
var _usePostCollection = require("../../../../../hooks/collections/post/usePostCollection");
var _ActionButton = _interopRequireDefault(require("../../../../../elements/ActionButton"));
var _usePendingPostQuery = require("../../../../../hooks/usePendingPostQuery");
var _BrandBadge = require("../../../../../elements/BrandBadge");
var _hooks = require("../../../../../hooks");
var _reactNativeSvg = require("react-native-svg");
var _elements = require("../../../../../elements");
var _PostFeedSkeleton = _interopRequireDefault(require("../../../../../components/PostFeedSkeleton"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const usePendingPostList = ({
  community,
  onPendingPostCountChange
}) => {
  const {
    styles,
    theme
  } = (0, _styles.useStyles)();
  const {
    client
  } = (0, _useAuth.default)();
  const {
    openBottomSheet,
    closeBottomSheet
  } = (0, _bottomSheetSlice.useBottomSheet)();
  const navigation = (0, _native.useNavigation)();
  const {
    deletePost,
    approvePost,
    declinePost
  } = (0, _usePendingPostQuery.usePendingPostQuery)();
  const collection = (0, _usePostCollection.usePostCollection)({
    params: {
      targetType: 'community',
      targetId: community.communityId,
      feedType: 'reviewing',
      limit: 20
    }
  });
  const currentUser = (0, _hooks.useUser)(client.userId || '');
  const isCommunityModerator = (0, _hooks.isModerator)(currentUser === null || currentUser === void 0 ? void 0 : currentUser.roles);
  (0, _react.useEffect)(() => {
    onPendingPostCountChange === null || onPendingPostCountChange === void 0 || onPendingPostCountChange(collection.data.length);
  }, [collection.data.length, onPendingPostCountChange]);
  const handleDeletePost = postId => {
    closeBottomSheet();
    _reactNative.Alert.alert('Delete this post?', "This post will be permanently deleted. You'll no longer to see and find this post.", [{
      text: 'Cancel',
      style: 'cancel'
    }, {
      text: 'Delete',
      style: 'destructive',
      onPress: () => {
        deletePost(postId);
      }
    }]);
  };
  const renderDivider = () => /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.separator
  });
  return {
    styles,
    theme,
    client,
    navigation,
    collection,
    declinePost,
    approvePost,
    renderDivider,
    openBottomSheet,
    handleDeletePost,
    isCommunityModerator
  };
};
const PendingPostList = ({
  community,
  onPendingPostCountChange
}) => {
  const {
    client,
    styles,
    theme,
    navigation,
    collection,
    declinePost,
    approvePost,
    renderDivider,
    openBottomSheet,
    handleDeletePost,
    isCommunityModerator
  } = usePendingPostList({
    community,
    onPendingPostCountChange
  });
  return /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.container
  }, /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.labelContainer
  }, /*#__PURE__*/React.createElement(_FormDescription.default, {
    pageId: _enums.PageID.pending_request_page,
    componentId: _enums.ComponentID.pending_post_list,
    elementId: _enums.ElementID.posts_tab_description
  })), collection.isLoading && /*#__PURE__*/React.createElement(_PostFeedSkeleton.default, null), /*#__PURE__*/React.createElement(_reactNative.FlatList, {
    data: collection.data,
    style: styles.scrollContainer,
    keyExtractor: item => item.postId,
    ItemSeparatorComponent: renderDivider,
    showsVerticalScrollIndicator: false,
    contentContainerStyle: styles.pendingPostListContainer,
    ListFooterComponent: collection.isFetchingNextPage ? /*#__PURE__*/React.createElement(_PostFeedSkeleton.default, null) : null,
    ListEmptyComponent: /*#__PURE__*/React.createElement(_reactNative.View, {
      style: styles.emptyContainer
    }, /*#__PURE__*/React.createElement(_reactNativeSvg.SvgXml, {
      width: 60,
      height: 60,
      xml: (0, _icons.firework)(),
      color: theme.colors.secondaryShade4
    }), /*#__PURE__*/React.createElement(_elements.Title, {
      style: styles.emptyTitle
    }, "No pending posts")),
    onEndReached: () => {
      var _collection$fetchNext;
      if (collection.hasNextPage && !collection.isFetchingNextPage) (_collection$fetchNext = collection.fetchNextPage) === null || _collection$fetchNext === void 0 || _collection$fetchNext.call(collection);
    },
    renderItem: ({
      item
    }) => {
      var _item$creator, _item$creator2, _item$creator3, _item$creator4, _item$creator6, _item$creator7, _item$creator8, _item$creator9, _item$data, _item$metadata;
      return /*#__PURE__*/React.createElement(_reactNative.View, null, /*#__PURE__*/React.createElement(_reactNative.View, {
        style: styles.pendingPostContainer
      }, /*#__PURE__*/React.createElement(_reactNative.View, {
        style: styles.pendingPostCreatorContainer
      }, /*#__PURE__*/React.createElement(_Avatar.default.User, {
        userName: (item === null || item === void 0 || (_item$creator = item.creator) === null || _item$creator === void 0 ? void 0 : _item$creator.displayName) || (item === null || item === void 0 || (_item$creator2 = item.creator) === null || _item$creator2 === void 0 ? void 0 : _item$creator2.userId),
        imageStyle: styles.pendingPostAvatar,
        uri: item === null || item === void 0 || (_item$creator3 = item.creator) === null || _item$creator3 === void 0 || (_item$creator3 = _item$creator3.avatar) === null || _item$creator3 === void 0 ? void 0 : _item$creator3.fileUrl,
        userId: item === null || item === void 0 || (_item$creator4 = item.creator) === null || _item$creator4 === void 0 ? void 0 : _item$creator4.userId,
        shouldRedirectToUserProfile: true
      }), /*#__PURE__*/React.createElement(_reactNative.View, {
        style: styles.userNameContainer
      }, /*#__PURE__*/React.createElement(_reactNative.View, {
        style: styles.displayNameRow
      }, /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
        style: styles.displayNameTouchable,
        onPress: () => {
          var _item$creator5;
          return navigation.navigate('UserProfile', {
            userId: item === null || item === void 0 || (_item$creator5 = item.creator) === null || _item$creator5 === void 0 ? void 0 : _item$creator5.userId
          });
        }
      }, /*#__PURE__*/React.createElement(_Typography.Typography.BodyBold, {
        style: styles.colorBase,
        numberOfLines: 1,
        ellipsizeMode: "tail"
      }, (item === null || item === void 0 || (_item$creator6 = item.creator) === null || _item$creator6 === void 0 ? void 0 : _item$creator6.displayName) || (item === null || item === void 0 || (_item$creator7 = item.creator) === null || _item$creator7 === void 0 ? void 0 : _item$creator7.userId))), (item === null || item === void 0 || (_item$creator8 = item.creator) === null || _item$creator8 === void 0 ? void 0 : _item$creator8.isBrand) && /*#__PURE__*/React.createElement(_BrandBadge.BrandBadge, {
        width: 16,
        height: 16
      })), /*#__PURE__*/React.createElement(_Timestamp.default, {
        timestamp: item === null || item === void 0 ? void 0 : item.createdAt,
        pageId: _enums.PageID.pending_request_page,
        componentId: _enums.ComponentID.pending_post_list
      }))), client.userId === (item === null || item === void 0 || (_item$creator9 = item.creator) === null || _item$creator9 === void 0 ? void 0 : _item$creator9.userId) && /*#__PURE__*/React.createElement(_MenuButton.default, {
        testID: "pending-post-menu-button",
        pageId: _enums.PageID.pending_request_page,
        componentId: _enums.ComponentID.pending_post_list,
        onPress: () => {
          openBottomSheet({
            height: 140,
            content: /*#__PURE__*/React.createElement(_MenuAction.default, {
              danger: true,
              label: "Delete post",
              onPress: () => handleDeletePost(item === null || item === void 0 ? void 0 : item.postId),
              iconProps: {
                xml: (0, _icons.trash)()
              },
              testID: "delete-pending-post-button",
              accessibilityLabel: "Delete pending post"
            })
          });
        }
      })), /*#__PURE__*/React.createElement(_reactNative.View, {
        style: styles.pendingPostContentContainer
      }, /*#__PURE__*/React.createElement(_PostContent.default, {
        post: item,
        disabledPoll: true,
        showedAllOptions: true,
        childrenPosts: item.children,
        textPost: (_item$data = item.data) === null || _item$data === void 0 ? void 0 : _item$data.text,
        mentionPositionArr: (item === null || item === void 0 || (_item$metadata = item.metadata) === null || _item$metadata === void 0 ? void 0 : _item$metadata.mentioned) ?? []
      })), isCommunityModerator && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(_reactNative.View, {
        style: styles.divider
      }), /*#__PURE__*/React.createElement(_reactNative.View, {
        style: styles.actionContainer
      }, /*#__PURE__*/React.createElement(_ActionButton.default, {
        fullWidth: true,
        pageId: _enums.PageID.pending_request_page,
        onPress: () => approvePost(item.postId),
        componentId: _enums.ComponentID.pending_post_list,
        elementId: _enums.ElementID.post_accept_button
      }), /*#__PURE__*/React.createElement(_ActionButton.default, {
        fullWidth: true,
        type: "secondary",
        pageId: _enums.PageID.pending_request_page,
        onPress: () => declinePost(item.postId),
        componentId: _enums.ComponentID.pending_post_list,
        elementId: _enums.ElementID.post_decline_button
      }))));
    }
  }));
};
var _default = exports.default = PendingPostList;
//# sourceMappingURL=PendingPostList.js.map