import { ExtensionType } from '../../../extensions/Extensions.mjs';
import { Rectangle } from '../../../maths/shapes/Rectangle.mjs';
import { settings } from '../../../settings/settings.mjs';
import { getCanvasTexture } from './texture/utils/getCanvasTexture.mjs';

const _ViewSystem = class {
  get resolution() {
    return this.texture.source._resolution;
  }
  set resolution(value) {
    this.texture.source.resize(
      this.texture.source.width,
      this.texture.source.height,
      value
    );
  }
  /**
   * initiates the view system
   * @param options - the options for the view
   */
  init(options) {
    options = {
      ..._ViewSystem.defaultOptions,
      ...options
    };
    this.screen = new Rectangle(0, 0, options.width, options.height);
    this.element = options.element || settings.ADAPTER.createCanvas();
    this.antialias = !!options.antialias;
    this.texture = getCanvasTexture(this.element, options);
    this.multiView = !!options.multiView;
    if (this.autoDensity) {
      this.element.style.width = `${this.texture.width}px`;
      this.element.style.height = `${this.texture.height}px`;
    }
  }
  /**
   * Resizes the screen and canvas to the specified dimensions.
   * @param desiredScreenWidth - The new width of the screen.
   * @param desiredScreenHeight - The new height of the screen.
   * @param resolution
   */
  resize(desiredScreenWidth, desiredScreenHeight, resolution) {
    this.texture.source.resize(desiredScreenWidth, desiredScreenHeight, resolution);
    this.screen.width = this.texture.frameWidth;
    this.screen.height = this.texture.frameHeight;
    if (this.autoDensity) {
      this.element.style.width = `${desiredScreenWidth}px`;
      this.element.style.height = `${desiredScreenHeight}px`;
    }
  }
  /**
   * Destroys this System and optionally removes the canvas from the dom.
   * @param {options | false} options - The options for destroying the view, or "false".
   * @param options.removeView - Whether to remove the view element from the DOM. Defaults to `false`.
   */
  destroy(options = false) {
    const removeView = typeof options === "boolean" ? options : !!options?.removeView;
    if (removeView && this.element.parentNode) {
      this.element.parentNode.removeChild(this.element);
    }
  }
};
let ViewSystem = _ViewSystem;
/** @ignore */
ViewSystem.extension = {
  type: [
    ExtensionType.WebGLSystem,
    ExtensionType.WebGPUSystem,
    ExtensionType.CanvasSystem
  ],
  name: "view",
  priority: 0
};
/** @ignore */
ViewSystem.defaultOptions = {
  /**
   * {@link WebGLOptions.width}
   * @default 800
   */
  width: 800,
  /**
   * {@link WebGLOptions.height}
   * @default 600
   */
  height: 600,
  /**
   * {@link WebGLOptions.resolution}
   * @type {number}
   * @default settings.RESOLUTION
   */
  resolution: settings.RESOLUTION,
  /**
   * {@link WebGLOptions.autoDensity}
   * @default false
   */
  autoDensity: false,
  /**
   * {@link WebGLOptions.antialias}
   * @default false
   */
  antialias: false
};

export { ViewSystem };
//# sourceMappingURL=ViewSystem.mjs.map
