"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useCustomRankingGlobalFeed = exports.isAmityAd = void 0;
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _react = require("react");
var _globalfeedSlice = _interopRequireDefault(require("../../core/stores/slices/globalfeedSlice"));
var _GlobalFeed = require("../features/feed/components/GlobalFeed/GlobalFeed");
var _reactNative = require("react-native");
var _store = require("../../core/stores/store");
var _usePaginator = require("./usePaginator");
var _useAuth = _interopRequireDefault(require("../../core/hooks/useAuth"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const isAmityAd = item => {
  return (item === null || item === void 0 ? void 0 : item.adId) !== undefined;
};
exports.isAmityAd = isAmityAd;
const useCustomRankingGlobalFeed = ({
  enabled = true
} = {}) => {
  const {
    isConnected
  } = (0, _useAuth.default)();
  const dispatch = (0, _store.useUIKitDispatch)();
  const unsubscribeRef = (0, _react.useRef)(null);
  const onNextPageRef = (0, _react.useRef)(null);
  const interactionHandleRef = (0, _react.useRef)(null);
  const {
    setNewGlobalFeed
  } = _globalfeedSlice.default.actions;
  const [fetching, setFetching] = (0, _react.useState)(false);
  const [error, setError] = (0, _react.useState)(false);
  const postList = (0, _store.useUIKitSelector)(state => state.globalFeed.postList);
  const {
    itemWithAds
  } = (0, _usePaginator.usePaginatorApi)({
    items: postList,
    isLoading: fetching,
    placement: 'feed',
    pageSize: _GlobalFeed.globalFeedPageLimit,
    getItemId: item => item.postId.toString()
  });
  const hasInitialDataRef = (0, _react.useRef)(false);
  const fetchCustomRanking = (0, _react.useCallback)(() => {
    if (!isConnected) return null;
    hasInitialDataRef.current = false;
    return _tsSdkReactNative.FeedRepository.getGlobalFeed({
      limit: _GlobalFeed.globalFeedPageLimit
    }, ({
      data,
      loading: isLoading,
      error: $error,
      onNextPage
    }) => {
      if (isLoading) {
        // Only show loading state on initial fetch, not on pagination
        if (!hasInitialDataRef.current) {
          setFetching(true);
        }
        return;
      }
      if (onNextPage) onNextPageRef.current = onNextPage;
      if ($error) setError($error);
      if (data) {
        var _interactionHandleRef;
        const filtered = data.filter(post => post.structureType !== _tsSdkReactNative.PostStructureType.AUDIO && post.structureType !== _tsSdkReactNative.PostStructureType.FILE && post.structureType !== _tsSdkReactNative.PostStructureType.MIXED);
        (_interactionHandleRef = interactionHandleRef.current) === null || _interactionHandleRef === void 0 || _interactionHandleRef.cancel();
        interactionHandleRef.current = _reactNative.InteractionManager.runAfterInteractions(() => {
          dispatch(setNewGlobalFeed(filtered));
          setFetching(false);
          hasInitialDataRef.current = true;
          interactionHandleRef.current = null;
        });
      } else {
        setFetching(false);
      }
    });
  }, [dispatch, setNewGlobalFeed, isConnected]);
  (0, _react.useEffect)(() => {
    if (!enabled) return undefined;
    unsubscribeRef.current = fetchCustomRanking();
    return () => {
      var _unsubscribeRef$curre, _interactionHandleRef2;
      (_unsubscribeRef$curre = unsubscribeRef.current) === null || _unsubscribeRef$curre === void 0 || _unsubscribeRef$curre.call(unsubscribeRef);
      (_interactionHandleRef2 = interactionHandleRef.current) === null || _interactionHandleRef2 === void 0 || _interactionHandleRef2.cancel();
      interactionHandleRef.current = null;
    };
  }, [fetchCustomRanking, enabled]);
  const refresh = (0, _react.useCallback)(() => {
    var _unsubscribeRef$curre2, _interactionHandleRef3;
    if (unsubscribeRef.current) (_unsubscribeRef$curre2 = unsubscribeRef.current) === null || _unsubscribeRef$curre2 === void 0 || _unsubscribeRef$curre2.call(unsubscribeRef);
    (_interactionHandleRef3 = interactionHandleRef.current) === null || _interactionHandleRef3 === void 0 || _interactionHandleRef3.cancel();
    interactionHandleRef.current = null;
    onNextPageRef.current = null;
    hasInitialDataRef.current = false;
    unsubscribeRef.current = fetchCustomRanking();
  }, [fetchCustomRanking]);
  return {
    loading: fetching,
    refresh,
    itemWithAds,
    onNextPage: onNextPageRef.current,
    error,
    globalFeedPosts: postList
  };
};
exports.useCustomRankingGlobalFeed = useCustomRankingGlobalFeed;
//# sourceMappingURL=useCustomRankingGlobalFeed.js.map