"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useCommunities = void 0;
var _react = require("react");
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
const useCommunities = ({
  categoryId,
  membership = 'member',
  limit = 20
} = {}) => {
  const [communities, setCommunities] = (0, _react.useState)();
  const [loading, setLoading] = (0, _react.useState)(true);
  const [onNextCommunityPage, setOnNextCommunityPage] = (0, _react.useState)(null);
  (0, _react.useEffect)(() => {
    const unsubscribe = _tsSdkReactNative.CommunityRepository.getCommunities({
      membership,
      limit,
      categoryId
    }, ({
      error,
      loading,
      data,
      hasNextPage,
      onNextPage
    }) => {
      if (error) return;
      if (!loading) {
        setCommunities(data);
        setOnNextCommunityPage(() => {
          if (hasNextPage) return onNextPage;
          return null;
        });
      }
      setLoading(loading);
    });
    return unsubscribe;
  }, [categoryId, membership, limit]);
  return {
    communities,
    onNextCommunityPage,
    loading
  };
};
exports.useCommunities = useCommunities;
//# sourceMappingURL=useCommunities.js.map