"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const scheduler = require("./utils/scheduler.js");
const ResizeObservation = require("./ResizeObservation.js");
const ResizeObserverDetail = require("./ResizeObserverDetail.js");
const resizeObservers = require("./utils/resizeObservers.js");
var observerMap = /* @__PURE__ */ new WeakMap();
var getObservationIndex = function(observationTargets, target) {
  for (var i = 0; i < observationTargets.length; i += 1) {
    if (observationTargets[i].target === target) {
      return i;
    }
  }
  return -1;
};
var ResizeObserverController = function() {
  function ResizeObserverController2() {
  }
  ResizeObserverController2.connect = function(resizeObserver, callback) {
    var detail = new ResizeObserverDetail.ResizeObserverDetail(resizeObserver, callback);
    observerMap.set(resizeObserver, detail);
  };
  ResizeObserverController2.observe = function(resizeObserver, target, options) {
    var detail = observerMap.get(resizeObserver);
    var firstObservation = detail.observationTargets.length === 0;
    if (getObservationIndex(detail.observationTargets, target) < 0) {
      firstObservation && resizeObservers.resizeObservers.push(detail);
      detail.observationTargets.push(new ResizeObservation.ResizeObservation(target, options && options.box));
      scheduler.updateCount(1);
      scheduler.scheduler.schedule();
    }
  };
  ResizeObserverController2.unobserve = function(resizeObserver, target) {
    var detail = observerMap.get(resizeObserver);
    var index = getObservationIndex(detail.observationTargets, target);
    var lastObservation = detail.observationTargets.length === 1;
    if (index >= 0) {
      lastObservation && resizeObservers.resizeObservers.splice(resizeObservers.resizeObservers.indexOf(detail), 1);
      detail.observationTargets.splice(index, 1);
      scheduler.updateCount(-1);
    }
  };
  ResizeObserverController2.disconnect = function(resizeObserver) {
    var _this = this;
    var detail = observerMap.get(resizeObserver);
    detail.observationTargets.slice().forEach(function(ot) {
      return _this.unobserve(resizeObserver, ot.target);
    });
    detail.activeTargets.splice(0, detail.activeTargets.length);
  };
  return ResizeObserverController2;
}();
exports.ResizeObserverController = ResizeObserverController;
