"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useStoryTarget = void 0;
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _react = require("react");
const useStoryTarget = param => {
  const [storyTarget, setStoryTarget] = (0, _react.useState)(null);
  const [fetching, setFetching] = (0, _react.useState)(false);
  (0, _react.useEffect)(() => {
    const unsubscribe = _tsSdkReactNative.StoryRepository.getTargetById(param, ({
      data,
      error,
      loading
    }) => {
      if (error) return;
      setFetching(loading);
      if (!loading) {
        setStoryTarget(data);
        const unsubTopic = (0, _tsSdkReactNative.getCommunityStoriesTopic)({
          targetId: param.targetId,
          targetType: param.targetType
        });
        (0, _tsSdkReactNative.subscribeTopic)(unsubTopic);
      }
    });
    return () => unsubscribe();
  }, [param]);
  return {
    storyTarget,
    loading: fetching
  };
};
exports.useStoryTarget = useStoryTarget;
//# sourceMappingURL=useStoryTarget.js.map