'use strict';

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

var EventEmitter = require('eventemitter3');
var Cache = require('../../../../assets/cache/Cache.js');
var uid = require('../../../../utils/data/uid.js');
var deprecation = require('../../../../utils/logging/deprecation.js');
var NOOP = require('../../../../utils/NOOP.js');
var BufferImageSource = require('./sources/BufferImageSource.js');
var TextureSource = require('./sources/TextureSource.js');
var TextureLayout = require('./TextureLayout.js');
var TextureMatrix = require('./TextureMatrix.js');

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

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

class Texture extends EventEmitter__default["default"] {
  constructor({ source, layout, label, frame } = {}) {
    super();
    this.id = uid.uid("texture");
    this.styleSourceKey = 0;
    this.label = label;
    this.source = source?.source ?? new TextureSource.TextureSource();
    layout = layout instanceof TextureLayout.TextureLayout ? layout : new TextureLayout.TextureLayout(layout);
    if (frame) {
      const { width, height } = this._source;
      layout.frame.x = frame.x / width;
      layout.frame.y = frame.y / height;
      layout.frame.width = frame.width / width;
      layout.frame.height = frame.height / height;
      layout.updateUvs();
    }
    this.layout = layout;
    this.destroyed = false;
  }
  static from(id) {
    if (typeof id === "string") {
      return Cache.Cache.get(id);
    } else if (id instanceof TextureSource.TextureSource) {
      return new Texture({ source: id });
    }
    return new Texture({
      source: new TextureSource.TextureSource(id)
    });
  }
  static fromBuffer(options) {
    return new Texture({
      source: BufferImageSource.BufferImageSource.from({
        ...options,
        style: {
          scaleMode: "nearest"
        }
      })
    });
  }
  set source(value) {
    if (this._source) {
      this._source.off("resize", this.onUpdate, this);
    }
    this._source = value;
    value.on("resize", this.onUpdate, this);
    this.emit("update", this);
  }
  get source() {
    return this._source;
  }
  get layout() {
    return this._layout;
  }
  set layout(value) {
    this._layout?.off("update", this.onUpdate, this);
    this._layout = value;
    value.on("update", this.onUpdate, this);
    this.emit("update", this);
  }
  get textureMatrix() {
    if (!this._textureMatrix) {
      this._textureMatrix = new TextureMatrix.TextureMatrix(this);
    }
    return this._textureMatrix;
  }
  set frameWidth(value) {
    this._layout.frame.width = value / this._source.width;
  }
  get frameWidth() {
    return this._source.pixelWidth / this._source._resolution * this._layout.frame.width;
  }
  set frameHeight(value) {
    this._layout.frame.height = value / this._source.height;
  }
  get frameHeight() {
    return this._source.pixelHeight / this._source._resolution * this._layout.frame.height;
  }
  set frameX(value) {
    if (value === 0) {
      this._layout.frame.x = 0;
      return;
    }
    this._layout.frame.x = this._source.width / value;
  }
  get frameX() {
    return this._source.width * this._layout.frame.x;
  }
  set frameY(value) {
    if (value === 0) {
      this._layout.frame.y = 0;
      return;
    }
    this._layout.frame.y = this._source.height / value;
  }
  get frameY() {
    return this._source.height * this._layout.frame.y;
  }
  /** The width of the Texture in pixels. */
  get width() {
    return this._source.width * this._layout.orig.width;
  }
  /** The height of the Texture in pixels. */
  get height() {
    return this._source.height * this._layout.orig.height;
  }
  /**
   * Destroys this texture
   * @param destroySource - Destroy the source when the texture is destroyed.
   */
  destroy(destroySource = false) {
    if (this._layout) {
      this._layout.destroy();
      this._layout = null;
    }
    if (this._source) {
      if (destroySource) {
        this._source.destroy();
        this._source = null;
      }
    }
    this._textureMatrix = null;
    this.destroyed = true;
    this.emit("destroy", this);
    this.removeAllListeners();
  }
  /**
   * @internal
   */
  onUpdate() {
    this.emit("update", this);
  }
  /** @deprecated since 8.0.0 */
  get baseTexture() {
    deprecation.deprecation(deprecation.v8_0_0, "Texture.baseTexture is now Texture.source");
    return this._source;
  }
}
Texture.EMPTY = new Texture({});
Texture.EMPTY.label = "EMPTY";
Texture.EMPTY.destroy = NOOP.NOOP;

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