"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.usePosts = void 0;
var _react = require("react");
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _native = require("@react-navigation/native");
const usePosts = ({
  targetType,
  targetId,
  feedType,
  dataTypes,
  limit = 20
}) => {
  const [items, setItems] = (0, _react.useState)();
  const [loading, setLoading] = (0, _react.useState)(true);
  const [onNextPage, setOnNextPage] = (0, _react.useState)(null);
  (0, _native.useFocusEffect)((0, _react.useCallback)(() => {
    const unsubscribe = _tsSdkReactNative.PostRepository.getPosts({
      targetType,
      limit,
      targetId,
      feedType,
      dataTypes
    }, ({
      error,
      loading: isLoading,
      data,
      hasNextPage,
      onNextPage: loadNextPage
    }) => {
      if (error) return;
      if (!isLoading) {
        setItems(data);
        setOnNextPage(() => {
          if (hasNextPage) return loadNextPage;
          return null;
        });
      }
      setLoading(isLoading);
    });
    return unsubscribe;
  }, [targetType, targetId, limit, feedType, dataTypes]));
  return {
    posts: items,
    onNextPage,
    loading
  };
};
exports.usePosts = usePosts;
//# sourceMappingURL=usePosts.js.map