'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var EventEmitter = require('eventemitter3');
var uid = require('../../../../../utils/data/uid.js');
var deprecation = require('../../../../../utils/logging/deprecation.js');
var TextureStyle = require('../TextureStyle.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var EventEmitter__default = /*#__PURE__*/_interopDefaultLegacy(EventEmitter);

const _TextureSource = class extends EventEmitter__default["default"] {
  // eslint-disable-next-line @typescript-eslint/no-parameter-properties
  constructor(options = {}) {
    super();
    this.options = options;
    this.uid = uid.uid("textureSource");
    this.resourceType = "textureSource";
    this.resourceId = uid.uid("textureResource");
    this.uploadMethodId = "unknown";
    // dimensions
    /** @internal */
    this._resolution = 1;
    this.pixelWidth = 1;
    this.pixelHeight = 1;
    this.width = 1;
    this.height = 1;
    // sample count for multisample textures
    // generally this is used only used internally by pixi!
    this.sampleCount = 1;
    // mip stuff..
    this.mipLevelCount = 1;
    // overridden if autoGenerateMipmaps is true
    this.autoGenerateMipmaps = false;
    this.format = "rgba8unorm-srgb";
    this.dimension = "2d";
    // properties used when rendering to this texture..
    this.antialias = false;
    this.depthStencil = true;
    /**
     * Used by automatic texture Garbage Collection, stores last GC tick when it was bound
     * @protected
     */
    this.touched = 0;
    /**
     * Used by the batcher to build texture batches. faster to have the variable here!
     * @protected
     */
    this._batchTick = -1;
    /**
     * A temporary batch location for the texture batching. Here for performance reasons only!
     * @protected
     */
    this._textureBindLocation = -1;
    options = { ..._TextureSource.defaultOptions, ...options };
    this.resource = options.resource;
    this._resolution = options.resolution;
    if (options.width) {
      this.pixelWidth = options.width * this._resolution;
    } else {
      this.pixelWidth = options.resource?.width ?? 1;
    }
    if (options.height) {
      this.pixelHeight = options.height * this._resolution;
    } else {
      this.pixelHeight = options.resource?.height ?? 1;
    }
    this.width = this.pixelWidth / this._resolution;
    this.height = this.pixelHeight / this._resolution;
    this.format = options.format;
    this.dimension = options.dimensions;
    this.mipLevelCount = options.mipLevelCount;
    this.autoGenerateMipmaps = options.autoGenerateMipmaps;
    this.sampleCount = options.sampleCount;
    this.antialias = options.antialias;
    this.alphaMode = options.alphaMode;
    const style = options.style ?? {};
    this.style = style instanceof TextureStyle.TextureStyle ? style : new TextureStyle.TextureStyle(style);
    this.destroyed = false;
  }
  get source() {
    return this;
  }
  get style() {
    return this._style;
  }
  set style(value) {
    if (this.style === value)
      return;
    this._style?.off("change", this._onStyleChange, this);
    this._style = value;
    this._style?.on("change", this._onStyleChange, this);
    this._onStyleChange();
  }
  _onStyleChange() {
    this.emit("styleChange", this);
  }
  update() {
    this.emit("update", this);
  }
  /** Destroys this texture source */
  destroy() {
    this.destroyed = true;
    this.emit("destroy", this);
    if (this._style) {
      this._style.destroy();
      this._style = null;
    }
    this.uploadMethodId = null;
    this.resource = null;
    this.removeAllListeners();
  }
  unload() {
    this.resourceId++;
    this.emit("change", this);
    this.emit("unload", this);
  }
  get resourceWidth() {
    const { resource } = this;
    return resource.naturalWidth || resource.videoWidth || resource.displayWidth || resource.width;
  }
  get resourceHeight() {
    const { resource } = this;
    return resource.naturalHeight || resource.videoHeight || resource.displayHeight || resource.height;
  }
  get resolution() {
    return this._resolution;
  }
  set resolution(resolution) {
    if (this._resolution === resolution)
      return;
    this._resolution = resolution;
    this.width = this.pixelWidth / resolution;
    this.height = this.pixelHeight / resolution;
  }
  resize(width, height, resolution) {
    resolution = resolution || this._resolution;
    width = width || this.width;
    height = height || this.height;
    const newPixelWidth = Math.round(width * resolution);
    const newPixelHeight = Math.round(height * resolution);
    this.width = newPixelWidth / resolution;
    this.height = newPixelHeight / resolution;
    this._resolution = resolution;
    if (this.pixelWidth === newPixelWidth && this.pixelHeight === newPixelHeight) {
      return;
    }
    this.pixelWidth = newPixelWidth;
    this.pixelHeight = newPixelHeight;
    this.emit("resize", this);
    this.resourceId++;
    this.emit("change", this);
  }
  /** @deprecated since 8.0.0 */
  set wrapMode(value) {
    deprecation.deprecation(deprecation.v8_0_0, "TextureSource.wrapMode property has been deprecated. Use TextureSource.style.addressMode instead.");
    this._style.wrapMode = value;
  }
  /** @deprecated since 8.0.0 */
  get wrapMode() {
    deprecation.deprecation(deprecation.v8_0_0, "TextureSource.wrapMode property has been deprecated. Use TextureSource.style.addressMode instead.");
    return this._style.wrapMode;
  }
  /** @deprecated since 8.0.0 */
  set scaleMode(value) {
    deprecation.deprecation(deprecation.v8_0_0, "TextureSource.scaleMode property has been deprecated. Use TextureSource.style.scaleMode instead.");
    this._style.scaleMode = value;
  }
  /** @deprecated since 8.0.0 */
  get scaleMode() {
    deprecation.deprecation(deprecation.v8_0_0, "TextureSource.scaleMode property has been deprecated. Use TextureSource.style.scaleMode instead.");
    return this._style.scaleMode;
  }
};
let TextureSource = _TextureSource;
TextureSource.defaultOptions = {
  resolution: 1,
  format: "bgra8unorm",
  alphaMode: "no-premultiply-alpha",
  dimensions: "2d",
  mipLevelCount: 1,
  autoGenerateMipmaps: false,
  sampleCount: 1,
  antialias: false,
  style: {}
};

exports.TextureSource = TextureSource;
//# sourceMappingURL=TextureSource.js.map
