package jetbrains.mps.kotlin.internal.generator.templates;

/*Generated by MPS */

import jetbrains.mps.generator.runtime.Generated;
import jetbrains.mps.generator.impl.query.QueryProviderBase;
import jetbrains.mps.generator.template.BaseMappingRuleContext;
import jetbrains.mps.kotlin.behavior.IStatement__BehaviorDescriptor;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.generator.template.SourceSubstituteMacroNodeContext;
import jetbrains.mps.generator.template.MappingScriptContext;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SModelOperations;
import java.util.Objects;
import java.util.List;
import org.jetbrains.mps.openapi.language.SAbstractConcept;
import jetbrains.mps.kotlin.behavior.INavigationTarget__BehaviorDescriptor;
import jetbrains.mps.generator.template.InsertMacroContext;
import java.util.Map;
import jetbrains.mps.generator.impl.query.ReductionRuleCondition;
import java.util.HashMap;
import org.jetbrains.annotations.NotNull;
import jetbrains.mps.generator.impl.query.QueryKey;
import jetbrains.mps.generator.template.ReductionRuleQueryContext;
import jetbrains.mps.generator.impl.GenerationFailureException;
import jetbrains.mps.generator.impl.query.ScriptCodeBlock;
import jetbrains.mps.generator.impl.query.SourceNodeQuery;
import org.jetbrains.annotations.Nullable;
import jetbrains.mps.generator.impl.query.InsertMacroQuery;
import jetbrains.mps.smodel.builder.SNodeBuilder;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import org.jetbrains.mps.openapi.language.SContainmentLink;
import org.jetbrains.mps.openapi.language.SReferenceLink;
import org.jetbrains.mps.openapi.language.SConcept;

@Generated
public class QueriesGenerated extends QueryProviderBase {
  public QueriesGenerated() {
    super(1);
  }
  public static boolean rule_Condition_0_0(final BaseMappingRuleContext _context) {
    return !((boolean) IStatement__BehaviorDescriptor.isLeftExpressionTarget_id69RFwVHPwd9.invoke(_context.getNode(), null));
  }
  public static boolean rule_Condition_0_1(final BaseMappingRuleContext _context) {
    SNode leftE = (!(SNodeOperations.isInstanceOf(SLinkOperations.getTarget(_context.getNode(), LINKS.left$UqF2), CONCEPTS.NavigationOperation$4I)) ? SLinkOperations.getTarget(_context.getNode(), LINKS.left$UqF2) : SLinkOperations.getTarget(SNodeOperations.cast(SLinkOperations.getTarget(_context.getNode(), LINKS.left$UqF2), CONCEPTS.NavigationOperation$4I), LINKS.target$C6zp));
    return SNodeOperations.isInstanceOf(leftE, CONCEPTS.GenericLeftExpression$Z4);
  }
  public static boolean rule_Condition_0_2(final BaseMappingRuleContext _context) {
    return SNodeOperations.isInstanceOf(SLinkOperations.getTarget(_context.getNode(), LINKS.target$xQFr), CONCEPTS.GenericLeftExpression$Z4) && (boolean) IStatement__BehaviorDescriptor.isLeftExpressionTarget_id69RFwVHPwd9.invoke(SNodeOperations.cast(SLinkOperations.getTarget(_context.getNode(), LINKS.target$xQFr), CONCEPTS.GenericLeftExpression$Z4), null);
  }
  public static boolean rule_Condition_0_3(final BaseMappingRuleContext _context) {
    return !((boolean) IStatement__BehaviorDescriptor.isLeftExpressionTarget_id69RFwVHPwd9.invoke(_context.getNode(), null)) && SNodeOperations.isInstanceOf(SLinkOperations.getTarget(_context.getNode(), LINKS.target$C6zp), CONCEPTS.GenericLeftExpression$Z4);
  }
  public static SNode sourceNodeQuery_0_0(final SourceSubstituteMacroNodeContext _context) {
    return SLinkOperations.getTarget(_context.getNode(), LINKS.getter$Kg37);
  }
  public static SNode sourceNodeQuery_0_1(final SourceSubstituteMacroNodeContext _context) {
    SNode leftE = (!(SNodeOperations.isInstanceOf(SLinkOperations.getTarget(_context.getNode(), LINKS.left$UqF2), CONCEPTS.NavigationOperation$4I)) ? SLinkOperations.getTarget(_context.getNode(), LINKS.left$UqF2) : SLinkOperations.getTarget(SNodeOperations.cast(SLinkOperations.getTarget(_context.getNode(), LINKS.left$UqF2), CONCEPTS.NavigationOperation$4I), LINKS.target$C6zp));
    return SLinkOperations.getTarget(SNodeOperations.cast(leftE, CONCEPTS.GenericLeftExpression$Z4), LINKS.setter$wD09);
  }
  public static SNode sourceNodeQuery_0_2(final SourceSubstituteMacroNodeContext _context) {
    return SLinkOperations.getTarget(SNodeOperations.getNodeAncestor(SLinkOperations.getTarget(_context.getNode(), LINKS.target$xQFr), CONCEPTS.Assignment$zN, false, false), LINKS.right$Uro5);
  }
  public static void mappingScript_CodeBlock_1(final MappingScriptContext _context) {
    ListSequence.fromList(SModelOperations.nodes(_context.getModel(), CONCEPTS.GenericLeftExpression$Z4)).where((it) -> Objects.equals(SNodeOperations.getContainingLink(it), LINKS.target$C6zp)).visitAll((it) -> {
      // Substitute all "this" by the navigation operand (use with(x) if more than one)
      List<SNode> getters = SNodeOperations.getNodeDescendants(SLinkOperations.getTarget(it, LINKS.getter$Kg37), CONCEPTS.ThisExpression$_g, false, new SAbstractConcept[]{});
      final SNode navigationOperand = INavigationTarget__BehaviorDescriptor.getNavigationOperand_id2gj5XQXMv4y.invoke(it);
      if (ListSequence.fromList(getters).count() > 1) {
        SLinkOperations.setTarget(it, LINKS.getter$Kg37, _quotation_createNode_x583g4_a0a0d0a0a0a8(SNodeOperations.copyNode(navigationOperand), SLinkOperations.getTarget(it, LINKS.getter$Kg37)));
      } else {
        ListSequence.fromList(getters).visitAll((thisOp) -> SNodeOperations.replaceWithAnother(thisOp, SNodeOperations.copyNode(navigationOperand)));
      }

      List<SNode> setters = SNodeOperations.getNodeDescendants(SLinkOperations.getTarget(it, LINKS.setter$wD09), CONCEPTS.ThisExpression$_g, false, new SAbstractConcept[]{});
      if (ListSequence.fromList(setters).count() > 1) {
        SLinkOperations.setTarget(it, LINKS.setter$wD09, _quotation_createNode_x583g4_a0a0g0a0a0a8(SNodeOperations.copyNode(navigationOperand), SLinkOperations.getTarget(it, LINKS.setter$wD09)));
      } else {
        ListSequence.fromList(setters).visitAll((thisOp) -> SNodeOperations.replaceWithAnother(thisOp, SNodeOperations.copyNode(navigationOperand)));
      }

      SNodeOperations.replaceWithAnother(SNodeOperations.getParent(it), it);
    });
  }
  public static SNode insertMacro_Query_0_0(final InsertMacroContext _context) {
    return SLinkOperations.getTarget(_context.getNode(), LINKS.target$C6zp);
  }
  private final Map<String, ReductionRuleCondition> rrcMethods = new HashMap<String, ReductionRuleCondition>();
  {
    int i = 0;
    rrcMethods.put("3181756179491244049", new RRC(i++));
    rrcMethods.put("5332309673432462871", new RRC(i++));
    rrcMethods.put("5332309673432423394", new RRC(i++));
    rrcMethods.put("3181756179491264370", new RRC(i++));
  }
  @Override
  @NotNull
  public ReductionRuleCondition getReductionRuleCondition(@NotNull QueryKey identity) {
    ReductionRuleCondition query = identity.forTemplateNode(rrcMethods);
    return (query != null ? query : super.getReductionRuleCondition(identity));
  }
  private static class RRC implements ReductionRuleCondition {
    private final int methodKey;
    public RRC(int methodKey) {
      this.methodKey = methodKey;
    }
    @Override
    public boolean check(ReductionRuleQueryContext ctx) throws GenerationFailureException {
      switch (methodKey) {
        case 0:
          return QueriesGenerated.rule_Condition_0_0(ctx);
        case 1:
          return QueriesGenerated.rule_Condition_0_1(ctx);
        case 2:
          return QueriesGenerated.rule_Condition_0_2(ctx);
        case 3:
          return QueriesGenerated.rule_Condition_0_3(ctx);
        default:
          throw new GenerationFailureException(String.format("Inconsistent QueriesGenerated: there's no condition method for rule %s (key: #%d)", ctx.getTemplateReference(), methodKey));
      }
    }
  }
  private final Map<String, ScriptCodeBlock> mscbMethods = new HashMap<String, ScriptCodeBlock>();
  {
    int i = 0;
    mscbMethods.put("5332309673431958491", new SCB(i++));
  }
  @Override
  @NotNull
  public ScriptCodeBlock getScriptCodeBlock(@NotNull QueryKey identity) {
    ScriptCodeBlock query = identity.forTemplateNode(mscbMethods);
    return (query != null ? query : super.getScriptCodeBlock(identity));
  }
  private static class SCB implements ScriptCodeBlock {
    private final int methodKey;
    public SCB(int methodKey) {
      this.methodKey = methodKey;
    }
    @Override
    public void invoke(MappingScriptContext ctx) throws GenerationFailureException {
      switch (methodKey) {
        case 0:
          QueriesGenerated.mappingScript_CodeBlock_1(ctx);
          return;
        default:
          throw new GenerationFailureException(String.format("There's no code block with method index %d ", methodKey));
      }
    }
  }
  private final Map<String, SourceNodeQuery> snqMethods = new HashMap<String, SourceNodeQuery>();
  {
    int i = 0;
    snqMethods.put("3181756179491263935", new SNQ(i++));
    snqMethods.put("5332309673432494738", new SNQ(i++));
    snqMethods.put("5332309673432439895", new SNQ(i++));
  }
  @NotNull
  @Override
  public SourceNodeQuery getSourceNodeQuery(@NotNull QueryKey identity) {
    SourceNodeQuery query = identity.forFunctionNode(snqMethods);
    return (query != null ? query : super.getSourceNodeQuery(identity));
  }
  private static class SNQ implements SourceNodeQuery {
    private final int methodKey;
    public SNQ(int methodKey) {
      this.methodKey = methodKey;
    }
    @Nullable
    public SNode evaluate(@NotNull SourceSubstituteMacroNodeContext ctx) throws GenerationFailureException {
      switch (methodKey) {
        case 0:
          return QueriesGenerated.sourceNodeQuery_0_0(ctx);
        case 1:
          return QueriesGenerated.sourceNodeQuery_0_1(ctx);
        case 2:
          return QueriesGenerated.sourceNodeQuery_0_2(ctx);
        default:
          throw new GenerationFailureException(String.format("Inconsistent QueriesGenerated: there's no method for query %s (key: #%d)", ctx.getTemplateReference(), methodKey));
      }
    }
  }
  private final Map<String, InsertMacroQuery> imqMethods = new HashMap<String, InsertMacroQuery>();
  {
    imqMethods.put("3181756179491288938", new IMQ(0));
  }
  @NotNull
  @Override
  public InsertMacroQuery getInsertMacroQuery(@NotNull QueryKey queryKey) {
    InsertMacroQuery query = queryKey.forTemplateNode(imqMethods);
    return (query != null ? query : super.getInsertMacroQuery(queryKey));
  }
  private static class IMQ implements InsertMacroQuery {
    private final int methodKey;
    /*package*/ IMQ(int methodKey) {
      this.methodKey = methodKey;
    }
    @Nullable
    public SNode evaluate(@NotNull InsertMacroContext ctx) throws GenerationFailureException {
      switch (methodKey) {
        case 0:
          return QueriesGenerated.insertMacro_Query_0_0(ctx);
        default:
          throw new GenerationFailureException(String.format("Inconsistent QueriesGenerated: there's no method for query %s (key: #%d)", ctx.getTemplateReference(), methodKey));
      }
    }
  }
  private static SNode _quotation_createNode_x583g4_a0a0d0a0a0a8(Object parameter_1, Object parameter_2) {
    SNode quotedNode_3 = null;
    SNode quotedNode_4 = null;
    SNode quotedNode_5 = null;
    SNode quotedNode_6 = null;
    SNode quotedNode_7 = null;
    SNodeBuilder nb = new SNodeBuilder(null, null).init(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, "jetbrains.mps.kotlin"), 0x40b4c3a5339a6979L, "FunctionCallExpression"));
    quotedNode_3 = nb.getResult();
    nb.setReference(MetaAdapterFactory.getReferenceLink(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x28bef6d7551af43fL, 0x1ba36e493d8ad4e9L, "function"), "b50d89c0-0fb9-4105-b652-222148c26a9b/kotlin:kotlin(jetbrains.mps.kotlin.stdlib/)/~.with<2>%280,Function1<0,1>%29");
    SNodeBuilder nb1 = new SNodeBuilder(null, null).init(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, "jetbrains.mps.kotlin"), 0x28bef6d7551af579L, "ValueArgument"));
    quotedNode_4 = nb1.getResult();
    quotedNode_6 = (SNode) parameter_1;
    if (quotedNode_6 != null) {
      quotedNode_4.addChild(MetaAdapterFactory.getContainmentLink(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x28bef6d7551af579L, 0x28bef6d7551af924L, "expression"), SNodeOperations.copyIfNecessary(quotedNode_6));
    }
    quotedNode_3.addChild(MetaAdapterFactory.getContainmentLink(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x5b1dd60162ecf00bL, 0x5b1dd60162ecf00cL, "arguments"), quotedNode_4);
    SNodeBuilder nb2 = new SNodeBuilder(null, null).init(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, "jetbrains.mps.kotlin"), 0x28bef6d7551af517L, "LambdaLiteral"));
    quotedNode_5 = nb2.getResult();
    quotedNode_7 = (SNode) parameter_2;
    if (quotedNode_7 != null) {
      quotedNode_5.addChild(MetaAdapterFactory.getContainmentLink(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x123d0b402b8869eeL, 0x123d0b402b8869f1L, "statements"), SNodeOperations.copyIfNecessary(quotedNode_7));
    }
    quotedNode_3.addChild(MetaAdapterFactory.getContainmentLink(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x6186632f85a74c27L, 0x28bef6d7551af77dL, "lambda"), quotedNode_5);
    return quotedNode_3;
  }
  private static SNode _quotation_createNode_x583g4_a0a0g0a0a0a8(Object parameter_1, Object parameter_2) {
    SNode quotedNode_3 = null;
    SNode quotedNode_4 = null;
    SNode quotedNode_5 = null;
    SNode quotedNode_6 = null;
    SNode quotedNode_7 = null;
    SNodeBuilder nb = new SNodeBuilder(null, null).init(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, "jetbrains.mps.kotlin"), 0x40b4c3a5339a6979L, "FunctionCallExpression"));
    quotedNode_3 = nb.getResult();
    nb.setReference(MetaAdapterFactory.getReferenceLink(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x28bef6d7551af43fL, 0x1ba36e493d8ad4e9L, "function"), "b50d89c0-0fb9-4105-b652-222148c26a9b/kotlin:kotlin(jetbrains.mps.kotlin.stdlib/)/~.with<2>%280,Function1<0,1>%29");
    SNodeBuilder nb1 = new SNodeBuilder(null, null).init(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, "jetbrains.mps.kotlin"), 0x28bef6d7551af579L, "ValueArgument"));
    quotedNode_4 = nb1.getResult();
    quotedNode_6 = (SNode) parameter_1;
    if (quotedNode_6 != null) {
      quotedNode_4.addChild(MetaAdapterFactory.getContainmentLink(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x28bef6d7551af579L, 0x28bef6d7551af924L, "expression"), SNodeOperations.copyIfNecessary(quotedNode_6));
    }
    quotedNode_3.addChild(MetaAdapterFactory.getContainmentLink(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x5b1dd60162ecf00bL, 0x5b1dd60162ecf00cL, "arguments"), quotedNode_4);
    SNodeBuilder nb2 = new SNodeBuilder(null, null).init(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, "jetbrains.mps.kotlin"), 0x28bef6d7551af517L, "LambdaLiteral"));
    quotedNode_5 = nb2.getResult();
    quotedNode_7 = (SNode) parameter_2;
    if (quotedNode_7 != null) {
      quotedNode_5.addChild(MetaAdapterFactory.getContainmentLink(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x123d0b402b8869eeL, 0x123d0b402b8869f1L, "statements"), SNodeOperations.copyIfNecessary(quotedNode_7));
    }
    quotedNode_3.addChild(MetaAdapterFactory.getContainmentLink(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x6186632f85a74c27L, 0x28bef6d7551af77dL, "lambda"), quotedNode_5);
    return quotedNode_3;
  }

  private static final class LINKS {
    /*package*/ static final SContainmentLink left$UqF2 = MetaAdapterFactory.getContainmentLink(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x28bef6d7551af595L, 0x28bef6d7551af94fL, "left");
    /*package*/ static final SContainmentLink target$C6zp = MetaAdapterFactory.getContainmentLink(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x28bef6d7551af450L, 0x28bef6d7551af73bL, "target");
    /*package*/ static final SReferenceLink target$xQFr = MetaAdapterFactory.getReferenceLink(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x4cca44497635e571L, 0x4cca44497635e572L, "target");
    /*package*/ static final SContainmentLink getter$Kg37 = MetaAdapterFactory.getContainmentLink(0xb17a9ec844a540a1L, 0x9216058d277424ebL, 0x2c27de6338059161L, 0x2c27de633805daddL, "getter");
    /*package*/ static final SContainmentLink setter$wD09 = MetaAdapterFactory.getContainmentLink(0xb17a9ec844a540a1L, 0x9216058d277424ebL, 0x2c27de6338059161L, 0x2c27de633805df0aL, "setter");
    /*package*/ static final SContainmentLink right$Uro5 = MetaAdapterFactory.getContainmentLink(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x28bef6d7551af595L, 0x28bef6d7551af952L, "right");
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept NavigationOperation$4I = MetaAdapterFactory.getConcept(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x28bef6d7551af450L, "jetbrains.mps.kotlin.structure.NavigationOperation");
    /*package*/ static final SConcept GenericLeftExpression$Z4 = MetaAdapterFactory.getConcept(0xb17a9ec844a540a1L, 0x9216058d277424ebL, 0x2c27de6338059161L, "jetbrains.mps.kotlin.internal.structure.GenericLeftExpression");
    /*package*/ static final SConcept Assignment$zN = MetaAdapterFactory.getConcept(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x28bef6d7551af595L, "jetbrains.mps.kotlin.structure.Assignment");
    /*package*/ static final SConcept ThisExpression$_g = MetaAdapterFactory.getConcept(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x28bef6d7551af598L, "jetbrains.mps.kotlin.structure.ThisExpression");
  }
}
