"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _styles = require("./styles");
var _enums = require("../../../../enums");
var _hooks = require("../../../../hooks");
var _Content = _interopRequireDefault(require("../../../../features/post/components/Content/Content"));
var _Divider = _interopRequireDefault(require("../../../../components/Divider"));
var _reactNative = require("react-native");
var _AmityPostContentComponentStyle = require("../../../../enums/AmityPostContentComponentStyle");
var _components = require("../../../../components");
var _PostFeedSkeleton = _interopRequireDefault(require("../../../../components/PostFeedSkeleton"));
var _usePinnedPostCollection = require("../../../../hooks/collections/post/usePinnedPostCollection");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function AmityCommunityPinnedPostComponent({
  communityId
}) {
  const styles = (0, _styles.useStyles)();
  const {
    accessibilityId
  } = (0, _hooks.useAmityComponent)({
    pageId: _enums.PageID.community_profile_page,
    componentId: _enums.ComponentID.community_pin
  });
  const {
    community
  } = (0, _hooks.useCommunity)(communityId);
  const pinnedPostCollection = (0, _usePinnedPostCollection.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(_components.Empty, {
      heightPercent: 0.3
    }, /*#__PURE__*/React.createElement(_components.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.default, 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(_reactNative.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(_components.Empty, {
      heightPercent: 0.3
    }, /*#__PURE__*/React.createElement(_components.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.default, null) : null,
    renderItem: ({
      item,
      index
    }) => {
      return /*#__PURE__*/React.createElement(_reactNative.View, null, index !== 0 && /*#__PURE__*/React.createElement(_Divider.default, null), /*#__PURE__*/React.createElement(_Content.default, {
        post: item.post,
        category: item.placement === 'announcement' ? _AmityPostContentComponentStyle.AmityPostCategory.PIN_AND_ANNOUNCEMENT : _AmityPostContentComponentStyle.AmityPostCategory.PIN,
        isCommunityNameShown: false,
        AmityPostContentComponentStyle: _AmityPostContentComponentStyle.AmityPostContentComponentStyleEnum.feed
      }));
    }
  });
}
var _default = exports.default = AmityCommunityPinnedPostComponent;
//# sourceMappingURL=PinnedPost.js.map