import { FeedRepository, PostStructureType } from '@amityco/ts-sdk-react-native';
import { useCallback, useEffect, useRef, useState } from 'react';
import globalFeedSlice from '../../core/stores/slices/globalfeedSlice';
import { globalFeedPageLimit } from '../features/feed/components/GlobalFeed/GlobalFeed';
import { InteractionManager } from 'react-native';
import { useUIKitDispatch, useUIKitSelector } from '../../core/stores/store';
import { usePaginatorApi } from './usePaginator';
import useAuth from '../../core/hooks/useAuth';
export const isAmityAd = item => {
  return (item === null || item === void 0 ? void 0 : item.adId) !== undefined;
};
export const useCustomRankingGlobalFeed = ({
  enabled = true
} = {}) => {
  const {
    isConnected
  } = useAuth();
  const dispatch = useUIKitDispatch();
  const unsubscribeRef = useRef(null);
  const onNextPageRef = useRef(null);
  const interactionHandleRef = useRef(null);
  const {
    setNewGlobalFeed
  } = globalFeedSlice.actions;
  const [fetching, setFetching] = useState(false);
  const [error, setError] = useState(false);
  const postList = useUIKitSelector(state => state.globalFeed.postList);
  const {
    itemWithAds
  } = usePaginatorApi({
    items: postList,
    isLoading: fetching,
    placement: 'feed',
    pageSize: globalFeedPageLimit,
    getItemId: item => item.postId.toString()
  });
  const hasInitialDataRef = useRef(false);
  const fetchCustomRanking = useCallback(() => {
    if (!isConnected) return null;
    hasInitialDataRef.current = false;
    return FeedRepository.getGlobalFeed({
      limit: 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 !== PostStructureType.AUDIO && post.structureType !== PostStructureType.FILE && post.structureType !== PostStructureType.MIXED);
        (_interactionHandleRef = interactionHandleRef.current) === null || _interactionHandleRef === void 0 || _interactionHandleRef.cancel();
        interactionHandleRef.current = InteractionManager.runAfterInteractions(() => {
          dispatch(setNewGlobalFeed(filtered));
          setFetching(false);
          hasInitialDataRef.current = true;
          interactionHandleRef.current = null;
        });
      } else {
        setFetching(false);
      }
    });
  }, [dispatch, setNewGlobalFeed, isConnected]);
  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 = 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
  };
};
//# sourceMappingURL=useCustomRankingGlobalFeed.js.map