"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useGlobalStory = void 0;
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _react = require("react");
const useGlobalStory = () => {
  const globalStoriesOnNextPageRef = (0, _react.useRef)(null);
  const [fetching, setFetching] = (0, _react.useState)(false);
  const [globalStoryTargetsOnNextPage, setGlobalStoryTargetsOnNextPage] = (0, _react.useState)(null);
  const [globalStories, setGlobalStories] = (0, _react.useState)([]);
  const [globalStoryTargets, setGlobalStoryTargets] = (0, _react.useState)([]);
  const getGlobalStoryTargets = (0, _react.useCallback)(() => {
    const unsubscribe = _tsSdkReactNative.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 = (0, _react.useCallback)(param => {
    const unsubscribe = _tsSdkReactNative.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
  };
};
exports.useGlobalStory = useGlobalStory;
//# sourceMappingURL=useGlobalStory.js.map