/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import { isServer } from "lit";
function createObserver(type, callback, options) {
  if (isServer) {
    return void 0;
  }
  const Observer = getObserver(type);
  return new Observer(callback, options);
}
function getObserver(type) {
  class ExtendedMutationObserver extends window.MutationObserver {
    constructor(callback) {
      super(callback);
      this.observedEntry = [];
      this.callback = callback;
    }
    observe(target, options) {
      this.observedEntry.push({ target, options });
      return super.observe(target, options);
    }
    unobserve(target) {
      const newObservedEntries = this.observedEntry.filter((observed) => observed.target !== target);
      this.observedEntry = [];
      this.callback(super.takeRecords(), this);
      this.disconnect();
      newObservedEntries.forEach((observed) => this.observe(observed.target, observed.options));
    }
  }
  return (function() {
    return type === "intersection" ? window.IntersectionObserver : type === "mutation" ? ExtendedMutationObserver : window.ResizeObserver;
  })();
}
function updateRefObserver(observer, oldTarget, target, options) {
  if (!observer) {
    return;
  }
  if (oldTarget) {
    observer.unobserve(oldTarget);
  }
  if (!target) {
    return;
  }
  if (observer instanceof MutationObserver) {
    observer.observe(target, options);
    return;
  }
  observer.observe(target);
}
export {
  createObserver as c,
  updateRefObserver as u
};
