/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Ident[ifier: Apache-2.0
 */
import { EventEmitter, GeneratedClass, ObservableProxy } from "#general";
const EventCache = /* @__PURE__ */ new WeakMap();
function BackingEvents(backing) {
  const target = backing.endpoint.eventsOf(backing.type);
  const constructor = target.constructor;
  let Implementation = EventCache.get(constructor);
  if (Implementation === void 0) {
    Implementation = EventEmitterProxy(target);
    EventCache.set(constructor, Implementation);
  }
  return new Implementation(target);
}
const TARGET = /* @__PURE__ */ Symbol("target");
const PROXIES = /* @__PURE__ */ Symbol("proxies");
class EventProxy extends ObservableProxy {
  constructor(target) {
    super(target);
  }
  get isQuieter() {
    return this.target.isQuieter;
  }
  get quiet() {
    return this.target.quiet;
  }
  get online() {
    return this.target.online;
  }
  toString() {
    return this.target.toString();
  }
}
function EventEmitterProxy(instance) {
  const descriptors = {
    [Symbol.dispose]: {
      value() {
        const proxies = this[PROXIES];
        if (!proxies) {
          return;
        }
        for (const proxy of Object.values(proxies)) {
          proxy[Symbol.dispose]();
        }
        this[PROXIES] = void 0;
      }
    }
  };
  for (const key in instance) {
    descriptors[key] = {
      get() {
        let proxies = this[PROXIES];
        if (proxies === void 0) {
          proxies = this[PROXIES] = {};
        } else if (key in proxies) {
          return proxies[key];
        }
        return proxies[key] = new EventProxy(this[TARGET][key]);
      }
    };
  }
  return GeneratedClass({
    name: `${instance.constructor.name}$Proxy`,
    base: EventEmitter,
    instanceDescriptors: descriptors,
    initialize(target) {
      this[TARGET] = target;
    }
  });
}
export {
  BackingEvents
};
//# sourceMappingURL=BackingEvents.js.map
