"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useStory = void 0;
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _react = require("react");
var _reactNative = require("react-native");
const useStory = () => {
  const onNextPageRef = (0, _react.useRef)(null);
  const [fetching, setFetching] = (0, _react.useState)(true);
  const [stories, setStories] = (0, _react.useState)([]);
  const getStories = (0, _react.useCallback)(({
    targetId,
    targetType
  }) => {
    const unsubscribe = _tsSdkReactNative.StoryRepository.getActiveStoriesByTarget({
      targetId,
      targetType
    }, ({
      data,
      error,
      loading,
      hasNextPage,
      onNextPage
    }) => {
      if (error) return;
      setFetching(loading);
      if (!loading) {
        setStories(data);
        onNextPageRef.current = hasNextPage ? onNextPage : null;
      }
    });
    return unsubscribe();
  }, []);
  const handleReaction = (0, _react.useCallback)(async ({
    targetId,
    reactionName,
    isLiked
  }) => {
    try {
      if (isLiked) {
        await _tsSdkReactNative.ReactionRepository.removeReaction('story', targetId, reactionName);
      } else {
        await _tsSdkReactNative.ReactionRepository.addReaction('story', targetId, reactionName);
      }
    } catch (error) {
      _reactNative.Alert.alert('Error on reaction', error);
    }
  }, []);
  return {
    handleReaction,
    getStories,
    stories,
    loading: fetching,
    onNextPage: onNextPageRef.current
  };
};
exports.useStory = useStory;
//# sourceMappingURL=useStory.js.map