import { LayerRenderable } from '../../renderers/shared/LayerRenderable.mjs';

function buildInstructions(layerGroup, renderPipes) {
  const root = layerGroup.root;
  const instructionSet = layerGroup.instructionSet;
  instructionSet.reset();
  renderPipes.batch.buildStart(instructionSet);
  renderPipes.blendMode.buildStart();
  renderPipes.colorMask.buildStart();
  if (root.sortableChildren) {
    root.sortChildren();
  }
  collectAllRenderablesAdvanced(root, instructionSet, renderPipes, true);
  renderPipes.batch.buildEnd(instructionSet);
  renderPipes.blendMode.buildEnd(instructionSet);
}
function collectAllRenderables(container, instructionSet, rendererPipes) {
  if (container.layerVisibleRenderable < 3 || !container.includeInBuild)
    return;
  if (container.sortableChildren) {
    container.sortChildren();
  }
  if (container.isSimple) {
    collectAllRenderablesSimple(container, instructionSet, rendererPipes);
  } else {
    collectAllRenderablesAdvanced(container, instructionSet, rendererPipes, false);
  }
}
function collectAllRenderablesSimple(container, instructionSet, renderPipes) {
  const view = container.view;
  if (view) {
    renderPipes.blendMode.setBlendMode(container, container.layerBlendMode, instructionSet);
    container.didViewUpdate = false;
    const rp = renderPipes;
    rp[view.renderPipeId].addRenderable(container, instructionSet);
  }
  if (!container.isLayerRoot) {
    const children = container.children;
    const length = children.length;
    for (let i = 0; i < length; i++) {
      collectAllRenderables(children[i], instructionSet, renderPipes);
    }
  }
}
function collectAllRenderablesAdvanced(container, instructionSet, renderPipes, isRoot) {
  if (isRoot) {
    const layerGroup = container.layerGroup;
    if (layerGroup.root.view) {
      const proxyRenderable = layerGroup.proxyRenderable ?? initProxyRenderable(layerGroup);
      if (proxyRenderable) {
        renderPipes.blendMode.setBlendMode(proxyRenderable, proxyRenderable.layerBlendMode, instructionSet);
        renderPipes[proxyRenderable.view.renderPipeId].addRenderable(proxyRenderable, instructionSet);
      }
    }
  } else {
    for (let i = 0; i < container.effects.length; i++) {
      const effect = container.effects[i];
      const pipe = renderPipes[effect.pipe];
      pipe.push(effect, container, instructionSet);
    }
  }
  if (!isRoot && container.isLayerRoot) {
    renderPipes.layer.addLayerGroup(container.layerGroup, instructionSet);
  } else {
    const view = container.view;
    if (view) {
      renderPipes.blendMode.setBlendMode(container, container.layerBlendMode, instructionSet);
      container.didViewUpdate = false;
      const pipe = renderPipes[view.renderPipeId];
      pipe.addRenderable(container, instructionSet);
    }
    const children = container.children;
    if (children.length) {
      for (let i = 0; i < children.length; i++) {
        collectAllRenderables(children[i], instructionSet, renderPipes);
      }
    }
  }
  if (!isRoot) {
    for (let i = container.effects.length - 1; i >= 0; i--) {
      const effect = container.effects[i];
      const pipe = renderPipes[effect.pipe];
      pipe.pop(effect, container, instructionSet);
    }
  }
}
function initProxyRenderable(layerGroup) {
  const root = layerGroup.root;
  layerGroup.proxyRenderable = new LayerRenderable({
    original: root,
    view: root.view
  });
}

export { buildInstructions, collectAllRenderables };
//# sourceMappingURL=buildInstructions.mjs.map
