'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 BatchableSprite = require('../../sprite/shared/BatchableSprite.js');

class CanvasTextPipe {
  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.currentKey !== newKey) {
      const view = renderable.view;
      const resolution = view.resolution ?? this._renderer.resolution;
      const { width, height } = this._renderer.canvasText.getTextureSize(
        view.text,
        resolution,
        view._style
      );
      if (
        // is only being used by this text:
        this._renderer.canvasText.getReferenceCount(gpuText.currentKey) === 1 && width === gpuText.texture._source.width && height === gpuText.texture._source.height
      ) {
        return false;
      }
      return true;
    }
    return false;
  }
  addRenderable(renderable, _instructionSet) {
    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.canvasText.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);
    }
    renderable.view._didUpdate = false;
    const padding = renderable.view._style.padding;
    updateQuadBounds.updateQuadBounds(batchableSprite.bounds, renderable.view.anchor, batchableSprite.texture, padding);
  }
  _updateGpuText(renderable) {
    const gpuText = this._getGpuText(renderable);
    const batchableSprite = gpuText.batchableSprite;
    const view = renderable.view;
    if (gpuText.texture) {
      this._renderer.canvasText.decreaseReferenceCount(gpuText.currentKey);
    }
    const resolution = view.resolution ?? this._renderer.resolution;
    gpuText.texture = batchableSprite.texture = this._renderer.canvasText.getTexture(
      view.text,
      resolution,
      view._style,
      view._getKey()
    );
    gpuText.currentKey = view._getKey();
    batchableSprite.texture = gpuText.texture;
  }
  _getGpuText(renderable) {
    return this._gpuText[renderable.uid] || this._initGpuText(renderable);
  }
  _initGpuText(renderable) {
    const view = renderable.view;
    view._style.update();
    const gpuTextData = {
      texture: null,
      currentKey: "--",
      batchableSprite: PoolGroup.BigPool.get(BatchableSprite.BatchableSprite)
    };
    gpuTextData.batchableSprite.sprite = renderable;
    gpuTextData.batchableSprite.bounds = [0, 1, 0, 0];
    this._gpuText[renderable.uid] = gpuTextData;
    this._updateText(renderable);
    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 */
CanvasTextPipe.extension = {
  type: [
    Extensions.ExtensionType.WebGLPipes,
    Extensions.ExtensionType.WebGPUPipes,
    Extensions.ExtensionType.CanvasPipes
  ],
  name: "text"
};

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