package jetbrains.mps.transformation.test.inputLang.generator.outputLang.template.test_reduceOneToMany;

/*Generated by MPS */

import jetbrains.mps.generator.runtime.Generated;
import jetbrains.mps.generator.runtime.TemplateDeclarationBase;
import org.jetbrains.mps.openapi.model.SNodeReference;
import jetbrains.mps.smodel.SNodePointer;
import jetbrains.mps.generator.runtime.FragmentResult;
import org.jetbrains.annotations.NotNull;
import jetbrains.mps.generator.runtime.TemplateContext;
import jetbrains.mps.generator.runtime.GenerationException;
import jetbrains.mps.generator.runtime.TemplateExecutionEnvironment;
import java.util.List;
import org.jetbrains.mps.openapi.model.SNode;
import java.util.ArrayList;
import jetbrains.mps.generator.template.SourceSubstituteMacroNodesContext;
import java.util.Collection;
import jetbrains.mps.generator.runtime.TemplateUtil;
import jetbrains.mps.generator.runtime.ApplySink;
import jetbrains.mps.generator.runtime.MetaObjectContainer;
import org.jetbrains.mps.openapi.language.SConcept;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import org.jetbrains.mps.openapi.language.SProperty;
import org.jetbrains.mps.openapi.language.SContainmentLink;

@Generated
public class Template_reduce_InputNode_A extends TemplateDeclarationBase {

  public Template_reduce_InputNode_A() {
    super(new MO());
  }

  public SNodeReference getTemplateNode() {
    return new SNodePointer("r:eca8e1c7-93fd-4ddf-9db6-91f9c2320691(jetbrains.mps.transformation.test.inputLang.generator.outputLang.template.test_reduceOneToMany@generator)", "3893401255414100167");
  }


  protected FragmentResult applyPart0(@NotNull final TemplateContext context) throws GenerationException {
    final TemplateExecutionEnvironment environment = context.getEnvironment();
    final List<SNode> tlist1 = new ArrayList<SNode>();
    final Iterable<SNode> loopList1 = QueriesGenerated.sourceNodesQuery_2_0(new SourceSubstituteMacroNodesContext(context, loopMacroRef));
    for (SNode itnode1 : loopList1) {
      if (itnode1 == null) {
        continue;
      }
      TemplateContext context1 = context.subContext(itnode1);
      final SNode tnode2 = environment.createOutputNode(myConcepts[0]);
      tnode2.setProperty(myProperties[0], "child #1");
      tlist1.add(tnode2);
    }
    FragmentResult rv = listFragment(0, tlist1);
    return rv;
  }
  protected FragmentResult applyPart1(@NotNull final TemplateContext context) throws GenerationException {
    final TemplateExecutionEnvironment environment = context.getEnvironment();
    final SNode tnode1 = environment.createOutputNode(myConcepts[0]);
    tnode1.setProperty(myProperties[0], "child #2");
    FragmentResult rv = nodeFragment(0, tnode1);
    return rv;
  }
  protected FragmentResult applyPart2(@NotNull final TemplateContext context) throws GenerationException {
    final TemplateExecutionEnvironment environment = context.getEnvironment();
    Collection<SNode> tlist1 = null;
    tlist1 = environment.callSite(new Template_reduce_InputNode_A_included(), new SNodePointer("r:eca8e1c7-93fd-4ddf-9db6-91f9c2320691(jetbrains.mps.transformation.test.inputLang.generator.outputLang.template.test_reduceOneToMany@generator)", "633481135935144635")).apply(context);
    FragmentResult rv = listFragment(0, tlist1);
    return rv;
  }
  protected FragmentResult applyPart3(@NotNull final TemplateContext context) throws GenerationException {
    final TemplateExecutionEnvironment environment = context.getEnvironment();
    Collection<SNode> tlist1 = null;
    try {
      TemplateContext switchContext1 = context;
      tlist1 = environment.trySwitch(templateSwitchNodeNoInput, switchContext1);
      if (tlist1 == null) {
        final SNode tnode2 = environment.createOutputNode(myConcepts[0]);
        tlist1 = TemplateUtil.singletonList(tnode2);
      }
    } finally {
    }
    FragmentResult rv = listFragment(0, tlist1);
    return rv;
  }
  @Override
  public void apply(TemplateContext context, ApplySink sink) throws GenerationException {
    applyPart0(context).reportTo(sink);
    applyPart1(context).reportTo(sink);
    applyPart2(context).reportTo(sink);
    applyPart3(context).reportTo(sink);
  }

  /*package*/ static final class MO implements MetaObjectContainer {
    @Override
    public SConcept[] concepts() {
      SConcept[] rv = new SConcept[1];
      rv[0] = MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0x157a9668bf58417bL, 0x893e53d86388dc56L, "jetbrains.mps.transformation.test.outputLang"), 0x1164564a526L, "OutputNode");
      return rv;
    }

    @Override
    public SProperty[] properties() {
      SProperty[] rv = new SProperty[1];
      rv[0] = MetaAdapterFactory.getProperty(0x157a9668bf58417bL, 0x893e53d86388dc56L, 0x1164564a526L, 0x11645b5a797L, "text");
      return rv;
    }


    @Override
    public SContainmentLink[] aggregations() {
      SContainmentLink[] rv = new SContainmentLink[1];
      rv[0] = MetaAdapterFactory.getContainmentLink(0x157a9668bf58417bL, 0x893e53d86388dc56L, 0x116455d922fL, 0x11645a94e4aL, "outputChild");
      return rv;
    }
  }
  private static final SNodePointer loopMacroRef = new SNodePointer("r:eca8e1c7-93fd-4ddf-9db6-91f9c2320691(jetbrains.mps.transformation.test.inputLang.generator.outputLang.template.test_reduceOneToMany@generator)", "3893401255414161782");
  private static final SNodePointer templateSwitchNodeNoInput = new SNodePointer("r:eca8e1c7-93fd-4ddf-9db6-91f9c2320691(jetbrains.mps.transformation.test.inputLang.generator.outputLang.template.test_reduceOneToMany@generator)", "1892993302480310366");
}
