import { watchEffect, Ref, unref, isRef } from "vue";

export function useTimeout(
  fn: () => void,
  delay?: Ref<number | undefined> | number,
  options?: {
    immediate?: boolean;
  }
) {
  const immediate = options?.immediate;
  if (immediate) {
    fn();
  }

  watchEffect((onInvalidate) => {
    if (isRef(delay)) {
      if (typeof delay.value !== "number" || delay.value < 0) return;
    } else {
      if (typeof delay !== "number" || delay < 0) return;
    }
    const _deply = unref(delay);
    const timer = setTimeout(() => {
      fn();
    }, _deply);
    onInvalidate(() => {
      clearInterval(timer);
    });
  });
}
