'use strict';

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

var Extensions = require('../../../extensions/Extensions.js');
var PoolGroup = require('../../../utils/pool/PoolGroup.js');
var updateQuadBounds = require('../../../utils/updateQuadBounds.js');
var Texture = require('../../renderers/shared/texture/Texture.js');
var BatchableSprite = require('../../sprite/shared/BatchableSprite.js');

class HTMLTextPipe {
  constructor(renderer) {
    this._gpuText = /* @__PURE__ */ Object.create(null);
    this._renderer = renderer;
  }
  validateRenderable(renderable) {
    const gpuText = this._getGpuText(renderable);
    const newKey = renderable.view._getKey();
    if (gpuText.textureNeedsUploading) {
      gpuText.textureNeedsUploading = false;
      return true;
    }
    if (gpuText.currentKey !== newKey) {
      return true;
    }
    return false;
  }
  addRenderable(renderable) {
    const gpuText = this._getGpuText(renderable);
    const batchableSprite = gpuText.batchableSprite;
    if (renderable.view._didUpdate) {
      this._updateText(renderable);
    }
    this._renderer.renderPipes.batch.addToBatch(batchableSprite);
  }
  updateRenderable(renderable) {
    const gpuText = this._getGpuText(renderable);
    const batchableSprite = gpuText.batchableSprite;
    if (renderable.view._didUpdate) {
      this._updateText(renderable);
    }
    batchableSprite.batcher.updateElement(batchableSprite);
  }
  destroyRenderable(renderable) {
    this._destroyRenderableById(renderable.uid);
  }
  _destroyRenderableById(renderableUid) {
    const gpuText = this._gpuText[renderableUid];
    this._renderer.htmlText.decreaseReferenceCount(gpuText.currentKey);
    PoolGroup.BigPool.return(gpuText.batchableSprite);
    this._gpuText[renderableUid] = null;
  }
  _updateText(renderable) {
    const newKey = renderable.view._getKey();
    const gpuText = this._getGpuText(renderable);
    const batchableSprite = gpuText.batchableSprite;
    if (gpuText.currentKey !== newKey) {
      this._updateGpuText(renderable).catch((e) => {
        console.error(e);
      });
    }
    renderable.view._didUpdate = false;
    const padding = renderable.view._style.padding;
    updateQuadBounds.updateQuadBounds(batchableSprite.bounds, renderable.view.anchor, batchableSprite.texture, padding);
  }
  async _updateGpuText(renderable) {
    renderable.view._didUpdate = false;
    const gpuText = this._getGpuText(renderable);
    if (gpuText.generatingTexture)
      return;
    const newKey = renderable.view._getKey();
    this._renderer.htmlText.decreaseReferenceCount(gpuText.currentKey);
    gpuText.generatingTexture = true;
    gpuText.currentKey = newKey;
    const view = renderable.view;
    const resolution = view.resolution ?? this._renderer.resolution;
    const texture = await this._renderer.htmlText.getManagedTexture(
      view.text,
      resolution,
      view._style,
      view._getKey()
    );
    const batchableSprite = gpuText.batchableSprite;
    batchableSprite.texture = gpuText.texture = texture;
    gpuText.generatingTexture = false;
    gpuText.textureNeedsUploading = true;
    renderable.view.onUpdate();
    const padding = renderable.view._style.padding;
    updateQuadBounds.updateQuadBounds(batchableSprite.bounds, renderable.view.anchor, batchableSprite.texture, padding);
  }
  _getGpuText(renderable) {
    return this._gpuText[renderable.uid] || this._initGpuText(renderable);
  }
  _initGpuText(renderable) {
    const view = renderable.view;
    view._style.update();
    const gpuTextData = {
      texture: Texture.Texture.EMPTY,
      currentKey: "--",
      batchableSprite: PoolGroup.BigPool.get(BatchableSprite.BatchableSprite),
      textureNeedsUploading: false,
      generatingTexture: false
    };
    gpuTextData.batchableSprite.sprite = renderable;
    gpuTextData.batchableSprite.texture = Texture.Texture.EMPTY;
    gpuTextData.batchableSprite.bounds = [0, 1, 0, 0];
    this._gpuText[renderable.uid] = gpuTextData;
    renderable.on("destroyed", () => {
      this.destroyRenderable(renderable);
    });
    return gpuTextData;
  }
  destroy() {
    for (const i in this._gpuText) {
      this._destroyRenderableById(i);
    }
    this._gpuText = null;
    this._renderer = null;
  }
}
/** @ignore */
HTMLTextPipe.extension = {
  type: [
    Extensions.ExtensionType.WebGLPipes,
    Extensions.ExtensionType.WebGPUPipes,
    Extensions.ExtensionType.CanvasPipes
  ],
  name: "htmlText"
};

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