import { EventEmitter } from "./EventEmitter";
export class Sizes extends EventEmitter {
  constructor({ canvas, config = {} }) {
    super();
    this.canvas = canvas;
    this.config = config || {};
    this.maxPixelRatio = Number(this.config.maxPixelRatio) || 2;
    this.handleResize = () => {
      this.init();
      this.emit("resize");
    };
    this.init();
    window.addEventListener("resize", this.handleResize);
  }
  init() {
    const target = this.canvas?.parentNode || this.canvas || document.documentElement;
    this.width = target.offsetWidth || window.innerWidth || 1;
    this.height = target.offsetHeight || window.innerHeight || 1;
    const maxPixelRatio = Number(this.config.maxPixelRatio) || this.maxPixelRatio || 2;
    this.maxPixelRatio = maxPixelRatio;
    this.pixelRatio = Math.min(window.devicePixelRatio || 1, maxPixelRatio);
  }
  destroy() {
    window.removeEventListener("resize", this.handleResize);
    this.off("resize");
  }
}
