import {
  useEventListener,
  useIntersectionObserver
} from "@vueuse/core";
function useElementVisibilityPromise(element) {
  let observer;
  return new Promise((resolve) => {
    observer = useIntersectionObserver(
      element,
      (intersectionObserverEntries) => {
        for (const entry of intersectionObserverEntries) {
          if (entry.isIntersecting)
            resolve();
        }
      },
      {
        rootMargin: "30px 0px 0px 0px",
        threshold: 0
      }
    );
  }).finally(() => {
    observer.stop();
  });
}
export function useElementScriptTrigger(options) {
  const { el, trigger } = options;
  if (import.meta.server || !el)
    return new Promise(() => {
    });
  if (el && options.trigger === "visible")
    return useElementVisibilityPromise(el);
  if (!trigger)
    return Promise.resolve();
  if (trigger !== "immediate") {
    return new Promise((resolve) => {
      const _ = useEventListener(
        typeof el !== "undefined" ? el : document.body,
        // @ts-expect-error untyped
        trigger,
        () => {
          resolve();
          _();
        },
        { once: true, passive: true }
      );
    });
  }
  return Promise.resolve();
}
