const inBrowser = typeof window !== "undefined";
const DEFAULT_URL = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
const DEFAULT_EVENTS = [
  "scroll",
  "wheel",
  "resize",
  "mousewheel",
  "animationend",
  "transitionend",
  "touchmove"
];
const hasIntersectionObserver = inBrowser && "IntersectionObserver" in window && "IntersectionObserverEntry" in window && "intersectionRatio" in window.IntersectionObserverEntry.prototype;
const modeType = {
  event: "event",
  observer: "observer"
};
const call = (fn) => fn && fn();
function setImage(element, imagePath) {
  if (element instanceof HTMLIFrameElement || element instanceof HTMLImageElement || element instanceof HTMLMediaElement) {
    element.src = imagePath;
  } else {
    element.style.backgroundImage = `url('${imagePath}')`;
  }
}
function loadImage(imagePath) {
  return new Promise((resolve, reject) => {
    const img = new Image();
    img.src = imagePath;
    img.onload = () => resolve(imagePath);
    img.onerror = (err) => reject(err);
  });
}
function addCssClassName(element, cssClassName) {
  if (!element.className.includes(cssClassName)) {
    element.className += ` ${cssClassName}`;
  }
}
function throttle(fn, threshold = 250, scope) {
  let last;
  let deferTimer;
  return () => {
    const context = scope || this;
    const now = +new Date();
    const args = arguments;
    if (last && now < last + threshold) {
      clearTimeout(deferTimer);
      deferTimer = setTimeout(() => {
        last = now;
        fn.apply(context, args);
      }, threshold);
    } else {
      last = now;
      fn.apply(context, args);
    }
  };
}
function debounce(func, wait, immediate) {
  let timeout;
  let args;
  let context;
  let timestamp;
  let result;
  const later = function later2() {
    const last = +new Date() - timestamp;
    if (last < wait && last >= 0) {
      timeout = setTimeout(later2, wait - last);
    } else {
      timeout = null;
      if (!immediate) {
        result = func.apply(context, args);
        if (!timeout) {
          context = null;
          args = null;
        }
      }
    }
  };
  return function debounced() {
    context = this;
    args = arguments;
    timestamp = +new Date();
    const callNow = immediate && !timeout;
    if (!timeout) {
      timeout = setTimeout(later, wait);
    }
    if (callNow) {
      result = func.apply(context, args);
      context = null;
      args = null;
    }
    return result;
  };
}
export {
  DEFAULT_EVENTS,
  DEFAULT_URL,
  addCssClassName,
  call,
  debounce,
  hasIntersectionObserver,
  loadImage,
  modeType,
  setImage,
  throttle
};
