import { useStyles } from './styles';
import { forwardRef, useImperativeHandle } from 'react';
import { ComponentID, PageID } from '../../../../enums';
import { useAmityComponent, useCommunity } from '../../../../hooks';
import AmityPostContentComponent from '../../../../features/post/components/Content/Content';
import { usePosts } from '../../../../hooks/usePosts';
import Divider from '../../../../components/Divider';
import { View, FlatList } from 'react-native';
import { isAmityAd } from '../../../../hooks/useCustomRankingGlobalFeed';
import PostAdComponent from '../../../../components/PostAdComponent/PostAdComponent';
import { usePaginatorApi } from '../../../../hooks/usePaginator';
import { usePostImpression } from '../../../../hooks/usePostImpression';
import { AmityPostCategory, AmityPostContentComponentStyleEnum } from '../../../../enums/AmityPostContentComponentStyle';
import { Empty } from '../../../../components';
import PostFeedSkeleton from '../../../../components/PostFeedSkeleton';
import { usePinnedPostCollection } from '../../../../hooks/collections/post/usePinnedPostCollection';
import { isPinnedPost } from '../../../../utils';
const pageLimit = 10;
const AmityCommunityFeedComponent = /*#__PURE__*/forwardRef(({
  pageId = PageID.WildCardPage,
  communityId
}, ref) => {
  var _pinnedPostCollection, _pinnedPostCollection2, _pinnedPostCollection3;
  const componentId = ComponentID.community_feed;
  const {
    community
  } = useCommunity(communityId);
  const {
    accessibilityId
  } = useAmityComponent({
    pageId,
    componentId
  });
  const {
    posts,
    loading,
    onNextPage
  } = usePosts({
    targetId: communityId,
    targetType: 'community',
    limit: pageLimit
  });
  const pinnedPostCollection = 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
  } = usePaginatorApi({
    items: posts,
    isLoading: loading,
    placement: 'feed',
    communityId: communityId,
    pageSize: pageLimit,
    getItemId: item => isAmityAd(item) ? item === null || item === void 0 ? void 0 : item.adId.toString() : item === null || item === void 0 ? void 0 : item.postId.toString()
  });
  const styles = useStyles();
  const {
    handleViewChange
  } = usePostImpression(itemWithAds === null || itemWithAds === void 0 ? void 0 : itemWithAds.filter(item => !!(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();
    }
  };
  useImperativeHandle(ref, () => ({
    handleLoadMore
  }));
  if (!(community !== null && community !== void 0 && community.isJoined) && !(community !== null && community !== void 0 && community.isPublic)) {
    return /*#__PURE__*/React.createElement(Empty, {
      heightPercent: 0.3
    }, /*#__PURE__*/React.createElement(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, null);
  }
  if (!loading && (itemWithAds === null || itemWithAds === void 0 ? void 0 : itemWithAds.length) === 0) {
    return /*#__PURE__*/React.createElement(Empty, {
      heightPercent: 0.3
    }, /*#__PURE__*/React.createElement(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 !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 !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(FlatList, {
    scrollEnabled: false,
    testID: accessibilityId,
    accessibilityLabel: accessibilityId,
    contentContainerStyle: styles.container,
    data: [...announcementPosts, ...(postsWithPinnedPosts ? postsWithPinnedPosts : [])],
    renderItem: ({
      item,
      index
    }) => {
      return /*#__PURE__*/React.createElement(View, null, index !== 0 && /*#__PURE__*/React.createElement(Divider, null), isPinnedPost(item) ? /*#__PURE__*/React.createElement(AmityPostContentComponent, {
        post: item.post,
        category: item.placement === 'announcement' ? isAnnouncementPostPinned ? AmityPostCategory.PIN_AND_ANNOUNCEMENT : AmityPostCategory.ANNOUNCEMENT : AmityPostCategory.PIN,
        isCommunityNameShown: false,
        AmityPostContentComponentStyle: AmityPostContentComponentStyleEnum.feed
      }) : isAmityAd(item) ? /*#__PURE__*/React.createElement(PostAdComponent, {
        ad: item
      }) : /*#__PURE__*/React.createElement(AmityPostContentComponent, {
        post: item,
        isCommunityNameShown: false,
        AmityPostContentComponentStyle: AmityPostContentComponentStyleEnum.feed
      }));
    },
    ListFooterComponent: loading && itemWithAds && itemWithAds.length > 0 ? /*#__PURE__*/React.createElement(PostFeedSkeleton, null) : null,
    viewabilityConfig: {
      viewAreaCoveragePercentThreshold: 60
    },
    onViewableItemsChanged: handleViewChange,
    keyExtractor: (item, index) => isPinnedPost(item) ? item.post.postId : isAmityAd(item) ? item.adId.toString() + index : item.postId.toString() + '_' + index,
    extraData: itemWithAds
  });
});
export default AmityCommunityFeedComponent;
//# sourceMappingURL=Feed.js.map