"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useReaction = void 0;
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _react = require("react");
const useReaction = ({
  referenceId,
  referenceType
}) => {
  const [reactions, setReactions] = (0, _react.useState)(null);
  const [reactors, setReactors] = (0, _react.useState)([]);
  const [isLoading, setIsLoading] = (0, _react.useState)(false);
  (0, _react.useEffect)(() => {
    const unsubscribe = _tsSdkReactNative.ReactionRepository.getReactions({
      referenceId,
      referenceType
    }, async ({
      data: reactorList,
      error
    }) => {
      setIsLoading(true);
      if (error) return;
      const reactionCount = reactorList.reduce((acc, curr) => {
        acc.all++;
        if (curr.reactionName === 'like') {
          acc.like++;
        }
        return acc;
      }, {
        all: 0,
        like: 0
      });
      setReactions(reactionCount);
      const userList = reactorList.map(item => item.userId);
      const {
        data: users
      } = await _tsSdkReactNative.UserRepository.getUserByIds(userList);
      setReactors(users);
      setIsLoading(false);
    });
    return () => unsubscribe();
  }, [referenceId, referenceType]);
  return {
    loading: isLoading,
    reactions,
    reactors
  };
};
exports.useReaction = useReaction;
//# sourceMappingURL=useReaction.js.map