"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.globalFeedPageLimit = exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _Content = _interopRequireDefault(require("../../../post/components/Content/Content"));
var _enumUIKitID = require("../../../../enums/enumUIKitID");
var _useUiKitReference = require("../../../../hooks/useUiKitReference");
var _AmityPostContentComponentStyle = require("../../../../enums/AmityPostContentComponentStyle");
var _Tab = _interopRequireDefault(require("../../../story/components/Tab/Tab"));
var _types = require("../../../../types");
var _usePostImpression = require("../../../../hooks/usePostImpression");
var _styles = require("./styles");
var _useCustomRankingGlobalFeed = require("../../../../hooks/useCustomRankingGlobalFeed");
var _PostAdComponent = _interopRequireDefault(require("../../../../components/PostAdComponent/PostAdComponent"));
var _Divider = _interopRequireDefault(require("../../../../components/Divider"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const globalFeedPageLimit = exports.globalFeedPageLimit = 20;
const AmityGlobalFeedComponent = ({
  pageId,
  isShowStoryTab = true,
  GlobalFeedHeaderComponent,
  itemWithAds,
  refresh,
  loading,
  onNextPage
}) => {
  const componentId = _enumUIKitID.ComponentID.global_feed_component;
  const {
    isExcluded,
    accessibilityId
  } = (0, _useUiKitReference.useAmityComponent)({
    pageId,
    componentId
  });
  const [refreshing, setRefreshing] = (0, _react.useState)(false);
  const styles = (0, _styles.useStyle)();
  const flatListRef = (0, _react.useRef)(null);
  const handleLoadMore = () => {
    if (loading || !onNextPage) return;
    onNextPage === null || onNextPage === void 0 || onNextPage();
  };
  const onRefresh = (0, _react.useCallback)(async () => {
    setRefreshing(true);
    await refresh();
    setRefreshing(false);
  }, [refresh]);
  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 viewabilityConfig = (0, _react.useRef)({
    viewAreaCoveragePercentThreshold: 60
  }).current;
  const listHeaderComponent = (0, _react.useMemo)(() => {
    if (isShowStoryTab) {
      return /*#__PURE__*/_react.default.createElement(_reactNative.View, null, GlobalFeedHeaderComponent, /*#__PURE__*/_react.default.createElement(_Tab.default, {
        type: _types.AmityStoryTabComponentEnum.globalFeed
      }));
    }
    return GlobalFeedHeaderComponent || null;
  }, [isShowStoryTab, GlobalFeedHeaderComponent]);
  if (isExcluded) return null;
  return /*#__PURE__*/_react.default.createElement(_reactNative.FlatList, {
    initialNumToRender: 20,
    testID: accessibilityId,
    accessibilityLabel: accessibilityId,
    style: styles.feedWrap,
    data: itemWithAds,
    renderItem: ({
      item,
      index
    }) => {
      return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, index !== 0 && /*#__PURE__*/_react.default.createElement(_Divider.default, null), (0, _useCustomRankingGlobalFeed.isAmityAd)(item) ? /*#__PURE__*/_react.default.createElement(_PostAdComponent.default, {
        ad: item
      }) : /*#__PURE__*/_react.default.createElement(_Content.default, {
        post: item,
        AmityPostContentComponentStyle: _AmityPostContentComponentStyle.AmityPostContentComponentStyleEnum.feed
      }));
    },
    keyExtractor: (item, index) => (0, _useCustomRankingGlobalFeed.isAmityAd)(item) ? item.adId.toString() + '_' + index : item.postId.toString() + '_' + index,
    onEndReachedThreshold: 0.5,
    onEndReached: handleLoadMore,
    ref: flatListRef,
    refreshControl: /*#__PURE__*/_react.default.createElement(_reactNative.RefreshControl, {
      refreshing: refreshing,
      onRefresh: onRefresh,
      colors: ['lightblue'],
      tintColor: "lightblue"
    }),
    keyboardShouldPersistTaps: "handled",
    ListHeaderComponent: listHeaderComponent,
    viewabilityConfig: viewabilityConfig,
    onViewableItemsChanged: handleViewChange,
    extraData: itemWithAds
  });
};
var _default = exports.default = /*#__PURE__*/(0, _react.memo)(AmityGlobalFeedComponent);
//# sourceMappingURL=GlobalFeed.js.map