"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _styles = require("./styles");
var _react = require("react");
var _enums = require("../../../../enums");
var _hooks = require("../../../../hooks");
var _Content = _interopRequireDefault(require("../../../../features/post/components/Content/Content"));
var _usePosts = require("../../../../hooks/usePosts");
var _Divider = _interopRequireDefault(require("../../../../components/Divider"));
var _reactNative = require("react-native");
var _useCustomRankingGlobalFeed = require("../../../../hooks/useCustomRankingGlobalFeed");
var _PostAdComponent = _interopRequireDefault(require("../../../../components/PostAdComponent/PostAdComponent"));
var _usePaginator = require("../../../../hooks/usePaginator");
var _usePostImpression = require("../../../../hooks/usePostImpression");
var _AmityPostContentComponentStyle = require("../../../../enums/AmityPostContentComponentStyle");
var _components = require("../../../../components");
var _PostFeedSkeleton = _interopRequireDefault(require("../../../../components/PostFeedSkeleton"));
var _usePinnedPostCollection = require("../../../../hooks/collections/post/usePinnedPostCollection");
var _utils = require("../../../../utils");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const pageLimit = 10;
const AmityCommunityFeedComponent = /*#__PURE__*/(0, _react.forwardRef)(({
  pageId = _enums.PageID.WildCardPage,
  communityId
}, ref) => {
  var _pinnedPostCollection, _pinnedPostCollection2, _pinnedPostCollection3;
  const componentId = _enums.ComponentID.community_feed;
  const {
    community
  } = (0, _hooks.useCommunity)(communityId);
  const {
    accessibilityId
  } = (0, _hooks.useAmityComponent)({
    pageId,
    componentId
  });
  const {
    posts,
    loading,
    onNextPage
  } = (0, _usePosts.usePosts)({
    targetId: communityId,
    targetType: 'community',
    limit: pageLimit
  });
  const pinnedPostCollection = (0, _usePinnedPostCollection.usePinnedPostCollection)({
    enabled: !!communityId,
    params: {
      communityId,
      sortBy: 'lastPinned'
    }
  });
  const announcementPosts = (pinnedPostCollection === null || pinnedPostCollection === void 0 || (_pinnedPostCollection = pinnedPostCollection.data) === null || _pinnedPostCollection === void 0 ? void 0 : _pinnedPostCollection.filter(item => (item === null || item === void 0 ? void 0 : item.placement) === 'announcement' && (item === null || item === void 0 ? void 0 : item.post))) || [];
  const pinnedPosts = (pinnedPostCollection === null || pinnedPostCollection === void 0 || (_pinnedPostCollection2 = pinnedPostCollection.data) === null || _pinnedPostCollection2 === void 0 ? void 0 : _pinnedPostCollection2.filter(item => {
    var _item$post;
    return (item === null || item === void 0 ? void 0 : item.placement) === 'default' && (item === null || item === void 0 ? void 0 : item.post) && !announcementPosts.map(aItem => {
      var _aItem$post;
      return aItem === null || aItem === void 0 || (_aItem$post = aItem.post) === null || _aItem$post === void 0 ? void 0 : _aItem$post.postId;
    }).includes(item === null || item === void 0 || (_item$post = item.post) === null || _item$post === void 0 ? void 0 : _item$post.postId);
  })) || [];
  const {
    itemWithAds
  } = (0, _usePaginator.usePaginatorApi)({
    items: posts,
    isLoading: loading,
    placement: 'feed',
    communityId: communityId,
    pageSize: pageLimit,
    getItemId: item => (0, _useCustomRankingGlobalFeed.isAmityAd)(item) ? item === null || item === void 0 ? void 0 : item.adId.toString() : item === null || item === void 0 ? void 0 : item.postId.toString()
  });
  const styles = (0, _styles.useStyles)();
  const {
    handleViewChange
  } = (0, _usePostImpression.usePostImpression)(itemWithAds === null || itemWithAds === void 0 ? void 0 : itemWithAds.filter(item => !!((0, _useCustomRankingGlobalFeed.isAmityAd)(item) ? item === null || item === void 0 ? void 0 : item.adId : item === null || item === void 0 ? void 0 : item.postId)));
  const handleLoadMore = () => {
    if (onNextPage) {
      onNextPage();
    }
  };
  (0, _react.useImperativeHandle)(ref, () => ({
    handleLoadMore
  }));
  if (!(community !== null && community !== void 0 && community.isJoined) && !(community !== null && community !== void 0 && community.isPublic)) {
    return /*#__PURE__*/React.createElement(_components.Empty, {
      heightPercent: 0.3
    }, /*#__PURE__*/React.createElement(_components.Empty.Content, {
      title: "This community is private",
      description: "Join this community to see its content and members.",
      icon: "private"
    }));
  }
  if (loading && !itemWithAds) {
    return /*#__PURE__*/React.createElement(_PostFeedSkeleton.default, null);
  }
  if (!loading && (itemWithAds === null || itemWithAds === void 0 ? void 0 : itemWithAds.length) === 0) {
    return /*#__PURE__*/React.createElement(_components.Empty, {
      heightPercent: 0.3
    }, /*#__PURE__*/React.createElement(_components.Empty.Content, {
      title: "No posts yet",
      icon: "post"
    }));
  }
  const postsWithoutAnnouncement = (itemWithAds === null || itemWithAds === void 0 ? void 0 : itemWithAds.filter(post => post && !announcementPosts.some(announcementPost => {
    var _announcementPost$pos;
    return !(0, _useCustomRankingGlobalFeed.isAmityAd)(post) && (announcementPost === null || announcementPost === void 0 || (_announcementPost$pos = announcementPost.post) === null || _announcementPost$pos === void 0 ? void 0 : _announcementPost$pos.postId) === (post === null || post === void 0 ? void 0 : post.postId);
  }))) || [];
  const postsWithPinnedPosts = postsWithoutAnnouncement === null || postsWithoutAnnouncement === void 0 ? void 0 : postsWithoutAnnouncement.map(post => {
    const pinnedPost = pinnedPosts === null || pinnedPosts === void 0 ? void 0 : pinnedPosts.find(pinned => {
      var _pinned$post;
      return !(0, _useCustomRankingGlobalFeed.isAmityAd)(post) && (pinned === null || pinned === void 0 || (_pinned$post = pinned.post) === null || _pinned$post === void 0 ? void 0 : _pinned$post.postId) === (post === null || post === void 0 ? void 0 : post.postId);
    });
    return pinnedPost ? pinnedPost : post;
  });
  const isAnnouncementPostPinned = pinnedPostCollection === null || pinnedPostCollection === void 0 || (_pinnedPostCollection3 = pinnedPostCollection.data) === null || _pinnedPostCollection3 === void 0 ? void 0 : _pinnedPostCollection3.some(item => {
    var _item$post2;
    return (item === null || item === void 0 ? void 0 : item.placement) === 'default' && (item === null || item === void 0 ? void 0 : item.post) && announcementPosts.map(aItem => {
      var _aItem$post2;
      return aItem === null || aItem === void 0 || (_aItem$post2 = aItem.post) === null || _aItem$post2 === void 0 ? void 0 : _aItem$post2.postId;
    }).includes(item === null || item === void 0 || (_item$post2 = item.post) === null || _item$post2 === void 0 ? void 0 : _item$post2.postId);
  });
  return /*#__PURE__*/React.createElement(_reactNative.FlatList, {
    scrollEnabled: false,
    testID: accessibilityId,
    accessibilityLabel: accessibilityId,
    contentContainerStyle: styles.container,
    data: [...announcementPosts, ...(postsWithPinnedPosts ? postsWithPinnedPosts : [])],
    renderItem: ({
      item,
      index
    }) => {
      return /*#__PURE__*/React.createElement(_reactNative.View, null, index !== 0 && /*#__PURE__*/React.createElement(_Divider.default, null), (0, _utils.isPinnedPost)(item) ? /*#__PURE__*/React.createElement(_Content.default, {
        post: item.post,
        category: item.placement === 'announcement' ? isAnnouncementPostPinned ? _AmityPostContentComponentStyle.AmityPostCategory.PIN_AND_ANNOUNCEMENT : _AmityPostContentComponentStyle.AmityPostCategory.ANNOUNCEMENT : _AmityPostContentComponentStyle.AmityPostCategory.PIN,
        isCommunityNameShown: false,
        AmityPostContentComponentStyle: _AmityPostContentComponentStyle.AmityPostContentComponentStyleEnum.feed
      }) : (0, _useCustomRankingGlobalFeed.isAmityAd)(item) ? /*#__PURE__*/React.createElement(_PostAdComponent.default, {
        ad: item
      }) : /*#__PURE__*/React.createElement(_Content.default, {
        post: item,
        isCommunityNameShown: false,
        AmityPostContentComponentStyle: _AmityPostContentComponentStyle.AmityPostContentComponentStyleEnum.feed
      }));
    },
    ListFooterComponent: loading && itemWithAds && itemWithAds.length > 0 ? /*#__PURE__*/React.createElement(_PostFeedSkeleton.default, null) : null,
    viewabilityConfig: {
      viewAreaCoveragePercentThreshold: 60
    },
    onViewableItemsChanged: handleViewChange,
    keyExtractor: (item, index) => (0, _utils.isPinnedPost)(item) ? item.post.postId : (0, _useCustomRankingGlobalFeed.isAmityAd)(item) ? item.adId.toString() + index : item.postId.toString() + '_' + index,
    extraData: itemWithAds
  });
});
var _default = exports.default = AmityCommunityFeedComponent;
//# sourceMappingURL=Feed.js.map