import { useStyles } from './styles';
import { ComponentID, PageID } from '../../../../enums';
import { useAmityComponent, useCommunity } from '../../../../hooks';
import AmityPostContentComponent from '../../../../features/post/components/Content/Content';
import Divider from '../../../../components/Divider';
import { View, FlatList } from 'react-native';
import { AmityPostCategory, AmityPostContentComponentStyleEnum } from '../../../../enums/AmityPostContentComponentStyle';
import { Empty } from '../../../../components';
import PostFeedSkeleton from '../../../../components/PostFeedSkeleton';
import { usePinnedPostCollection } from '../../../../hooks/collections/post/usePinnedPostCollection';
function AmityCommunityPinnedPostComponent({
  communityId
}) {
  const styles = useStyles();
  const {
    accessibilityId
  } = useAmityComponent({
    pageId: PageID.community_profile_page,
    componentId: ComponentID.community_pin
  });
  const {
    community
  } = useCommunity(communityId);
  const pinnedPostCollection = usePinnedPostCollection({
    enabled: !!communityId,
    params: {
      communityId,
      sortBy: 'lastPinned'
    }
  });
  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, {
      icon: "private",
      title: "This community is private",
      description: "Join this community to see its content and members."
    }));
  }
  if (pinnedPostCollection.isLoading) {
    return /*#__PURE__*/React.createElement(PostFeedSkeleton, null);
  }
  const announcementPosts = pinnedPostCollection.data.filter(announcementPost => announcementPost.placement === 'announcement' && pinnedPostCollection.data.some(pinnedPost => pinnedPost.placement === 'default' && pinnedPost.referenceId === announcementPost.referenceId));
  const pinnedPostsWithoutAnnouncement = pinnedPostCollection.data.filter(item => item.placement === 'default').filter(post => !announcementPosts.some(announcement => announcement.referenceId === post.referenceId));
  const pinnedPosts = pinnedPostCollection.data.filter(post => post.placement === 'default');
  return /*#__PURE__*/React.createElement(FlatList, {
    scrollEnabled: false,
    testID: accessibilityId,
    contentContainerStyle: styles.container,
    keyExtractor: item => item.post.postId,
    accessibilityLabel: "Community Pinned Post List",
    viewabilityConfig: {
      viewAreaCoveragePercentThreshold: 60
    },
    data: [...announcementPosts, ...(announcementPosts.length > 0 ? pinnedPostsWithoutAnnouncement : pinnedPosts)],
    ListEmptyComponent: /*#__PURE__*/React.createElement(Empty, {
      heightPercent: 0.3
    }, /*#__PURE__*/React.createElement(Empty.Content, {
      icon: "post",
      title: "No pinned post yet"
    })),
    onEndReached: () => {
      var _pinnedPostCollection;
      if (pinnedPostCollection.hasNextPage && !pinnedPostCollection.isFetchingNextPage) (_pinnedPostCollection = pinnedPostCollection.fetchNextPage) === null || _pinnedPostCollection === void 0 || _pinnedPostCollection.call(pinnedPostCollection);
    },
    ListFooterComponent: pinnedPostCollection.isFetchingNextPage ? /*#__PURE__*/React.createElement(PostFeedSkeleton, null) : null,
    renderItem: ({
      item,
      index
    }) => {
      return /*#__PURE__*/React.createElement(View, null, index !== 0 && /*#__PURE__*/React.createElement(Divider, null), /*#__PURE__*/React.createElement(AmityPostContentComponent, {
        post: item.post,
        category: item.placement === 'announcement' ? AmityPostCategory.PIN_AND_ANNOUNCEMENT : AmityPostCategory.PIN,
        isCommunityNameShown: false,
        AmityPostContentComponentStyle: AmityPostContentComponentStyleEnum.feed
      }));
    }
  });
}
export default AmityCommunityPinnedPostComponent;
//# sourceMappingURL=PinnedPost.js.map