import { ExtensionType } from '../../extensions/Extensions.mjs';
import { buildInstructions } from './utils/buildInstructions.mjs';
import { collectLayerGroups } from './utils/collectLayerGroups.mjs';
import { executeInstructions } from './utils/executeInstructions.mjs';
import { updateLayerGroupTransforms } from './utils/updateLayerGroupTransforms.mjs';
import { validateRenderables } from './utils/validateRenderables.mjs';

class LayerSystem {
  constructor(renderer) {
    this._renderer = renderer;
  }
  render({ container, transform }) {
    container.layer = true;
    const renderer = this._renderer;
    const layerGroups = collectLayerGroups(container.layerGroup, []);
    const renderPipes = renderer.renderPipes;
    for (let i = 0; i < layerGroups.length; i++) {
      const layerGroup = layerGroups[i];
      layerGroup.runOnRender();
      layerGroup.instructionSet.renderPipes = renderPipes;
      if (!layerGroup.structureDidChange) {
        validateRenderables(layerGroup, renderPipes);
      }
      updateLayerGroupTransforms(layerGroup);
      if (layerGroup.structureDidChange) {
        layerGroup.structureDidChange = false;
        buildInstructions(layerGroup, renderPipes);
      } else {
        updateRenderables(layerGroup);
      }
      renderer.renderPipes.batch.upload(layerGroup.instructionSet);
    }
    if (transform) {
      container.layerGroup.worldTransform.copyFrom(transform);
    }
    renderer.globalUniforms.start(
      {
        projectionMatrix: renderer.renderTarget.rootProjectionMatrix,
        worldTransformMatrix: container.layerGroup.worldTransform
      }
    );
    executeInstructions(container.layerGroup, renderPipes);
    if (renderPipes.uniformBatch) {
      renderPipes.uniformBatch.renderEnd();
      renderPipes.uniformBuffer.renderEnd();
    }
  }
  destroy() {
    const writeable = this;
    writeable._renderer = null;
  }
}
/** @ignore */
LayerSystem.extension = {
  type: [
    ExtensionType.WebGLSystem,
    ExtensionType.WebGPUSystem,
    ExtensionType.CanvasSystem
  ],
  name: "layer"
};
function updateRenderables(layerGroup) {
  const { list, index } = layerGroup.childrenRenderablesToUpdate;
  for (let i = 0; i < index; i++) {
    const container = list[i];
    if (container.didViewUpdate) {
      layerGroup.updateRenderable(container);
    }
  }
  layerGroup.childrenRenderablesToUpdate.index = 0;
}

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