'use strict';
import { resourceNames } from '../../../../fd-toolbox/resources/resource-names.js';
import { fetchResourceMeta } from '../../../meta/meta-providers.js';
import { fetchResources } from '../../../meta/resource-providers.js';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
  value,
  configurable: true
});
const intersectionObserverThreshold = 0.9;
async function initFeed(setFeedEntries, setComments, pageSize, setIsFetching) {
  const feedMeta = await fetchResourceMeta(resourceNames.feedEntry);
  const odataConfig = {
    top: pageSize,
    expands: [resourceNames.user]
  };
  if (feedMeta) {
    setIsFetching(true);
    const feedEntries = await fetchResources(feedMeta.resourceName, odataConfig);
    setIsFetching(false);
    if (feedEntries) setFeedEntries(feedEntries);
    setComments(["", ""]);
  }
}
__name(initFeed, "initFeed");
async function fetchNextFeedEntries(fetchSize, currentLength) {
  const odataConfig = {
    top: fetchSize,
    skip: currentLength,
    expands: [resourceNames.user]
  };
  const feedMeta = await fetchResourceMeta(resourceNames.feedEntry);
  if (feedMeta) {
    const entries = await fetchResources(feedMeta.resourceName, odataConfig);
    if (entries) return entries;
  }
  return [];
}
__name(fetchNextFeedEntries, "fetchNextFeedEntries");
async function fetchFeedMoreEntries(hasMore, fdFeedEntries, postsPerFetch, setFeedEntries, setHasMore) {
  if (!hasMore) return;
  if (fdFeedEntries.length === 0) return;
  const newEntries = await fetchNextFeedEntries(postsPerFetch, fdFeedEntries.length);
  if (newEntries.length > 0) {
    setFeedEntries([...fdFeedEntries, ...newEntries]);
    setHasMore(true);
  } else {
    setHasMore(false);
  }
}
__name(fetchFeedMoreEntries, "fetchFeedMoreEntries");
function createFeedIntersectionObserver(isFetching, setIsFetching, hasMore, fdFeedEntries, postsPerFetch, setFeedEntries, setHasMore) {
  const observer = new IntersectionObserver(([entry]) => {
    if (entry.isIntersecting && !isFetching) {
      setIsFetching(true);
      fetchFeedMoreEntries(hasMore, fdFeedEntries, postsPerFetch, setFeedEntries, setHasMore).finally(() => {
        setIsFetching(false);
      });
    }
  }, {
    threshold: intersectionObserverThreshold
  });
  return observer;
}
__name(createFeedIntersectionObserver, "createFeedIntersectionObserver");
function observerFeed(hasMore, isFetching, setIsFetching, fdFeedEntries, postsPerFetch, setFeedEntries, setHasMore, observerRef) {
  let result;
  if (!observerRef || !hasMore) return result;
  const observer = createFeedIntersectionObserver(isFetching, setIsFetching, hasMore, fdFeedEntries, postsPerFetch, setFeedEntries, setHasMore);
  observer.observe(observerRef);
  result = /* @__PURE__ */__name(() => observer.disconnect(), "result");
  return result;
}
__name(observerFeed, "observerFeed");

export { createFeedIntersectionObserver, fetchFeedMoreEntries, fetchNextFeedEntries, initFeed, observerFeed };
