import { StoryRepository } from '@amityco/ts-sdk-react-native';
import { useCallback, useRef, useState } from 'react';
export const useGlobalStory = () => {
  const globalStoriesOnNextPageRef = useRef(null);
  const [fetching, setFetching] = useState(false);
  const [globalStoryTargetsOnNextPage, setGlobalStoryTargetsOnNextPage] = useState(null);
  const [globalStories, setGlobalStories] = useState([]);
  const [globalStoryTargets, setGlobalStoryTargets] = useState([]);
  const getGlobalStoryTargets = useCallback(() => {
    const unsubscribe = StoryRepository.getGlobalStoryTargets({
      limit: 20,
      seenState: 'smart'
    }, ({
      data,
      error,
      loading,
      hasNextPage,
      onNextPage
    }) => {
      if (error) return;
      setFetching(loading);
      if (!loading) {
        setGlobalStoryTargets([...data]);
        setGlobalStoryTargetsOnNextPage(() => hasNextPage ? onNextPage : null);
      }
    });
    return () => unsubscribe();
  }, []);
  const getGlobalStories = useCallback(param => {
    const unsubscribe = StoryRepository.getStoriesByTargetIds(param, ({
      data,
      error,
      loading,
      hasNextPage,
      onNextPage
    }) => {
      if (error) return;
      if (!loading) {
        setGlobalStories([...data]);
        globalStoriesOnNextPageRef.current = hasNextPage ? onNextPage : null;
      }
    });
    return () => unsubscribe();
  }, []);
  return {
    loading: fetching,
    getGlobalStoryTargets,
    globalStoryTargets,
    globalStoryTargetsOnNextPage,
    getGlobalStories,
    globalStories,
    globalStoriesOnNextPage: globalStoriesOnNextPageRef.current
  };
};
//# sourceMappingURL=useGlobalStory.js.map