"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useReaction = void 0;
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _native = require("@react-navigation/native");
var _react = require("react");
const useReaction = ({
  referenceId,
  referenceType
}) => {
  const [allReactionList, setAllReactionList] = (0, _react.useState)(null);
  const [likeReactionList, setLikeReactionList] = (0, _react.useState)(null);
  const [loveReactionList, setLoveReactionList] = (0, _react.useState)(null);
  const [isLoading, setIsLoading] = (0, _react.useState)(false);
  const [hasError, setHasError] = (0, _react.useState)(null);
  (0, _native.useFocusEffect)((0, _react.useCallback)(() => {
    if (!referenceId) return () => {};
    const unsubscribe = _tsSdkReactNative.ReactionRepository.getReactions({
      referenceId,
      referenceType
    }, async ({
      loading,
      error,
      data: reactorList
    }) => {
      setIsLoading(loading);
      if (error) {
        return setHasError(error);
      }
      if (!loading) {
        setAllReactionList(reactorList);
        const likeReactions = reactorList.filter(reactor => reactor.reactionName === 'like');
        const loveReactions = reactorList.filter(reactor => reactor.reactionName === 'love');
        setLoveReactionList(loveReactions);
        setLikeReactionList(likeReactions);
      }
    });
    return () => {
      unsubscribe();
      setAllReactionList(null);
      setLikeReactionList(null);
    };
  }, [referenceId, referenceType]));
  return {
    loading: isLoading,
    allReactionList,
    likeReactionList,
    loveReactionList,
    hasError
  };
};
exports.useReaction = useReaction;
//# sourceMappingURL=useReaction.js.map