"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useLiveCollection = void 0;
var _reactQuery = require("@tanstack/react-query");
var _react = require("react");
const useLiveCollection = ({
  key,
  enabled,
  params,
  fetcher
}) => {
  var _query$data;
  const unsubscribe = (0, _react.useRef)(null);
  const resolveRef = (0, _react.useRef)(null);
  const rejectRef = (0, _react.useRef)(null);
  const queryClient = (0, _reactQuery.useQueryClient)();
  const fetch = async ({
    pageParam
  }) => {
    return new Promise((resolve, reject) => {
      resolveRef.current = resolve;
      rejectRef.current = reject;
      if (pageParam) {
        pageParam();
      } else {
        unsubscribe.current = fetcher(params, ({
          error,
          loading,
          data: $data,
          hasNextPage,
          onNextPage
        }) => {
          var _rejectRef$current;
          if (error) return (_rejectRef$current = rejectRef.current) === null || _rejectRef$current === void 0 ? void 0 : _rejectRef$current.call(rejectRef, error);
          if (!loading) {
            var _resolveRef$current;
            (_resolveRef$current = resolveRef.current) === null || _resolveRef$current === void 0 || _resolveRef$current.call(resolveRef, {
              data: $data,
              hasNextPage,
              onNextPage
            });
            queryClient.setQueryData([key], oldData => {
              const newPage = {
                data: $data,
                hasNextPage,
                onNextPage
              };
              if (!oldData) {
                return {
                  pages: [newPage],
                  pageParams: [undefined]
                };
              }
              return {
                pages: [...oldData.pages, newPage],
                pageParams: [...oldData.pageParams, onNextPage]
              };
            });
          }
        });
      }
    });
  };
  const query = (0, _reactQuery.useInfiniteQuery)({
    initialPageParam: undefined,
    queryKey: [key],
    queryFn: fetch,
    getNextPageParam: lastPage => {
      return lastPage.hasNextPage ? lastPage.onNextPage : undefined;
    },
    select: data => {
      return {
        pages: data.pages.slice(-1),
        pageParams: data.pageParams.slice(-1)
      };
    },
    enabled
  });
  const data = ((_query$data = query.data) === null || _query$data === void 0 || (_query$data = _query$data.pages) === null || _query$data === void 0 ? void 0 : _query$data.flatMap(page => page.data)) ?? [];
  (0, _react.useEffect)(() => {
    queryClient.setQueryData(key, cache => {
      return {
        pages: cache ? cache.pages.slice(-1) : [],
        pageParams: cache ? cache.pageParams.slice(-1) : []
      };
    });
  }, [data.length, queryClient, key]);
  (0, _react.useEffect)(() => {
    return () => {
      var _unsubscribe$current;
      return (_unsubscribe$current = unsubscribe.current) === null || _unsubscribe$current === void 0 ? void 0 : _unsubscribe$current.call(unsubscribe);
    };
  }, []);
  return {
    ...query,
    data
  };
};
exports.useLiveCollection = useLiveCollection;
//# sourceMappingURL=useReactQueryLiveCollection.js.map