package jetbrains.mps.lang.feedback.skeleton.main;

/*Generated by MPS */

import jetbrains.mps.generator.runtime.Generated;
import jetbrains.mps.generator.impl.query.QueryProviderBase;
import jetbrains.mps.generator.template.CreateRootRuleContext;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SModelOperations;
import jetbrains.mps.generator.template.PropertyMacroContext;
import jetbrains.mps.util.NameUtil;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
import jetbrains.mps.generator.template.ReferenceMacroContext;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.generator.template.IfMacroContext;
import jetbrains.mps.internal.collections.runtime.Sequence;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.generator.template.SourceSubstituteMacroNodesContext;
import jetbrains.mps.generator.template.MapSrcMacroPostProcContext;
import org.jetbrains.mps.openapi.model.SModel;
import jetbrains.mps.lang.project.behavior.ModelReference__BehaviorDescriptor;
import org.jetbrains.mps.openapi.module.SRepository;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import java.util.List;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SConceptOperations;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.generator.template.WeavingMappingRuleContext;
import jetbrains.mps.generator.template.WeavingAnchorContext;
import jetbrains.mps.baseLanguage.behavior.StatementList__BehaviorDescriptor;
import jetbrains.mps.generator.template.TemplateVarContext;
import java.util.Map;
import jetbrains.mps.generator.impl.query.CreateRootCondition;
import java.util.HashMap;
import org.jetbrains.annotations.NotNull;
import jetbrains.mps.generator.impl.query.QueryKey;
import jetbrains.mps.generator.impl.GenerationFailureException;
import jetbrains.mps.generator.impl.query.WeaveRuleCondition;
import jetbrains.mps.generator.impl.query.WeaveRuleQuery;
import jetbrains.mps.generator.impl.query.WeaveAnchorQuery;
import org.jetbrains.annotations.Nullable;
import jetbrains.mps.generator.impl.query.SourceNodesQuery;
import java.util.Collection;
import jetbrains.mps.util.IterableUtil;
import jetbrains.mps.generator.impl.query.PropertyValueQuery;
import org.jetbrains.mps.openapi.language.SProperty;
import jetbrains.mps.generator.impl.query.IfMacroCondition;
import jetbrains.mps.generator.impl.query.ReferenceTargetQuery;
import jetbrains.mps.generator.impl.query.VariableValueQuery;
import jetbrains.mps.generator.impl.query.MapPostProcessor;
import org.jetbrains.mps.openapi.language.SConcept;
import org.jetbrains.mps.openapi.language.SReferenceLink;
import org.jetbrains.mps.openapi.language.SContainmentLink;

@Generated
public class QueriesGenerated extends QueryProviderBase {
  public QueriesGenerated() {
    super(1);
  }
  public static boolean createRootRule_Condition_0_0(final CreateRootRuleContext _context) {
    return ListSequence.fromList(SModelOperations.roots(_context.getInputModel(), CONCEPTS.FeedbackPerConceptRoot$Vm)).isNotEmpty();
  }
  public static Object propertyMacro_GetValue_2_0(final PropertyMacroContext _context) {
    String simpleName = _context.getInputModel().getName().getSimpleName();
    return _context.getTemplateValue() + NameUtil.multiWordCapitalize(simpleName);
  }
  public static Object propertyMacro_GetValue_3_0(final PropertyMacroContext _context) {
    return SPropertyOperations.getString(_context.getNode(), PROPS.name$MnvL);
  }
  public static Object referenceMacro_GetReferent_3_0(final ReferenceMacroContext _context) {
    return SLinkOperations.getTarget(_context.getNode(), LINKS.concept$NMNv);
  }
  public static Object referenceMacro_GetReferent_3_1(final ReferenceMacroContext _context) {
    return _context.getOutputNodeByInputNodeAndMappingLabel(_context.getNode(), "feedbackMember");
  }
  public static boolean ifMacro_Condition_1_0(final IfMacroContext _context) {
    return Sequence.fromIterable(((Iterable<SNode>) _context.getVariable("var:models"))).isNotEmpty();
  }
  public static Iterable<SNode> sourceNodesQuery_2_0(final SourceSubstituteMacroNodesContext _context) {
    return SModelOperations.roots(_context.getInputModel(), CONCEPTS.FeedbackPerConceptRoot$Vm);
  }
  public static Iterable<SNode> sourceNodesQuery_3_0(final SourceSubstituteMacroNodesContext _context) {
    return SLinkOperations.getChildren(_context.getNode(), LINKS.feedbacks$E$KJ);
  }
  public static Iterable<SNode> sourceNodesQuery_3_1(final SourceSubstituteMacroNodesContext _context) {
    return SLinkOperations.getChildren(_context.getNode(), LINKS.feedbacks$E$KJ);
  }
  public static void mapSrcMacro_post_1_0(final MapSrcMacroPostProcContext _context) {
    Iterable<SNode> seq = Sequence.fromIterable(((Iterable<SNode>) _context.getVariable("var:models"))).select((it) -> {
      SModel resolved = ModelReference__BehaviorDescriptor.toModelReference_id2BHFktfnfdc.invoke(it).resolve(((SRepository) _context.getVariable("var:repo")));
      assert resolved != null;
      SNode feedbackClass = _context.getOutputNodeByMappingLabel("feedbackAspectClass", resolved);
      return feedbackClass;
    }).where((it) -> it != null);
    if (Sequence.fromIterable(seq).isEmpty()) {
      SNodeOperations.deleteNode(SNodeOperations.getNodeAncestor(_context.getOutputNode(), CONCEPTS.IfStatement$Q4, false, false));
    } else {
      final List<SNode> args = SLinkOperations.getChildren(SNodeOperations.cast(_context.getOutputNode(), CONCEPTS.StaticMethodCall$Fg), LINKS.actualArgument$pzdx);
      ListSequence.fromList(args).clear();
      Sequence.fromIterable(seq).visitAll((feedbackClass) -> {
        SNode newExpr = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x10ab8473cc5L, "jetbrains.mps.baseLanguage.structure.GenericNewExpression"));
        SNode classCreator = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x2724644c0ac833a5L, "jetbrains.mps.baseLanguage.structure.DefaultClassCreator"));
        SLinkOperations.setTarget(classCreator, LINKS.classifier$9NRM, feedbackClass);
        SLinkOperations.setTarget(newExpr, LINKS.creator$BsHW, classCreator);
        ListSequence.fromList(args).addElement(newExpr);
      });
    }
  }
  public static SNode weavingRule_ContextQuery_0_0(final WeavingMappingRuleContext _context) {
    return SNodeOperations.getParent(_context.getOutputNodeByInputNodeAndMappingLabel(_context.getNode(), "aspectLoop"));
  }
  public static SNode weaving_AnchorQuery_0_0(final WeavingAnchorContext _context) {
    return (SNode) StatementList__BehaviorDescriptor.getFirstStatement_id4GU1DgEHJ2u.invoke(SNodeOperations.cast(_context.getMainContextNode(), CONCEPTS.StatementList$m_));
  }
  public static Object varMacro_Value_1_0(final TemplateVarContext _context) {
    SRepository repository = _context.getOriginalInputModel().getRepository();
    return repository;
  }
  public static Object varMacro_Value_1_1(final TemplateVarContext _context) {
    // rather we generate in lang.descriptor refs to all ILanguageAspect instances in all of the models in the module,
    // then we use weaves/reductions to use this references in order to restore the ref to the <GeneratedAspectDescriptor>.
    // 
    return ListSequence.fromList(SLinkOperations.getChildren(SLinkOperations.getTarget(_context.getNode(), LINKS.language$rnIG), LINKS.model$2Sf4)).where((it) -> {
      SModel resolved = ModelReference__BehaviorDescriptor.toModelReference_id2BHFktfnfdc.invoke(it).resolve(((SRepository) _context.getVariable("var:repo")));
      return isEmptyString(SPropertyOperations.getString(it, PROPS.stereotype$h2Bb)) && resolved != null;
    });
  }
  private final Map<String, CreateRootCondition> crcMethods = new HashMap<String, CreateRootCondition>();
  {
    int i = 0;
    crcMethods.put("7291380803382344427", new CRC(i++));
  }
  @Override
  @NotNull
  public CreateRootCondition getCreateRootRuleCondition(@NotNull QueryKey identity) {
    CreateRootCondition query = identity.forTemplateNode(crcMethods);
    return (query != null ? query : super.getCreateRootRuleCondition(identity));
  }
  private static class CRC implements CreateRootCondition {
    private final int methodKey;
    public CRC(int methodKey) {
      this.methodKey = methodKey;
    }
    @Override
    public boolean check(@NotNull CreateRootRuleContext ctx) throws GenerationFailureException {
      switch (methodKey) {
        case 0:
          return QueriesGenerated.createRootRule_Condition_0_0(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, WRQ> wrcnMethods = new HashMap<String, WRQ>();
  {
    int i = 0;
    wrcnMethods.put("1938781798615490891", new WRQ(i++));
  }
  @Override
  @NotNull
  public WeaveRuleCondition getWeaveRuleCondition(@NotNull QueryKey identity) {
    WRQ query = identity.forTemplateNode(wrcnMethods);
    return (query != null ? query : super.getWeaveRuleCondition(identity));
  }
  @Override
  @NotNull
  public WeaveRuleQuery getWeaveRuleQuery(@NotNull QueryKey identity) {
    WRQ query = identity.forTemplateNode(wrcnMethods);
    return (query != null ? query : super.getWeaveRuleQuery(identity));
  }
  @NotNull
  @Override
  public WeaveAnchorQuery getWeaveAnchorQuery(@NotNull QueryKey identity) {
    WRQ query = identity.forTemplateNode(wrcnMethods);
    return (query != null ? query : super.getWeaveAnchorQuery(identity));
  }
  private static class WRQ implements WeaveRuleQuery, WeaveRuleCondition, WeaveAnchorQuery {
    private final int methodKey;
    public WRQ(int methodKey) {
      this.methodKey = methodKey;
    }
    @Override
    public boolean check(@NotNull WeavingMappingRuleContext ctx) throws GenerationFailureException {
      switch (methodKey) {
        case 0:
          return true;
        default:
          throw new GenerationFailureException(String.format("Inconsistent QueriesGenerated: there's no condition method for rule %s (key: #%d)", ctx.getTemplateReference(), methodKey));
      }
    }
    @Override
    public SNode contextNode(WeavingMappingRuleContext ctx) throws GenerationFailureException {
      switch (methodKey) {
        case 0:
          return QueriesGenerated.weavingRule_ContextQuery_0_0(ctx);
        default:
          throw new GenerationFailureException(String.format("Inconsistent QueriesGenerated: there's no context node query method for weaving rule %s (key: #%d)", ctx.getTemplateReference(), methodKey));
      }

    }
    @Nullable
    @Override
    public SNode anchorNode(WeavingAnchorContext ctx) throws GenerationFailureException {
      switch (methodKey) {
        case 0:
          return QueriesGenerated.weaving_AnchorQuery_0_0(ctx);
        default:
          throw new GenerationFailureException(String.format("Inconsistent QueriesGenerated: there's no anchor query method for rule %s (key: #%d)", ctx.getTemplateReference(), methodKey));
      }
    }
  }
  private final Map<String, SourceNodesQuery> snsqMethods = new HashMap<String, SourceNodesQuery>();
  {
    int i = 0;
    snsqMethods.put("4996591504622268461", new SNsQ(i++));
    snsqMethods.put("2823715683251121880", new SNsQ(i++));
    snsqMethods.put("6285588811486475822", new SNsQ(i++));
  }
  @NotNull
  @Override
  public SourceNodesQuery getSourceNodesQuery(@NotNull QueryKey identity) {
    SourceNodesQuery query = identity.forFunctionNode(snsqMethods);
    return (query != null ? query : super.getSourceNodesQuery(identity));
  }
  private static class SNsQ implements SourceNodesQuery {
    private final int methodKey;
    public SNsQ(int methodKey) {
      this.methodKey = methodKey;
    }
    @NotNull
    public Collection<SNode> evaluate(@NotNull SourceSubstituteMacroNodesContext ctx) throws GenerationFailureException {
      switch (methodKey) {
        case 0:
          return IterableUtil.asCollection(QueriesGenerated.sourceNodesQuery_2_0(ctx));
        case 1:
          return IterableUtil.asCollection(QueriesGenerated.sourceNodesQuery_3_0(ctx));
        case 2:
          return IterableUtil.asCollection(QueriesGenerated.sourceNodesQuery_3_1(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, PropertyValueQuery> pvqMethods = new HashMap<String, PropertyValueQuery>();
  {
    int i = 0;
    pvqMethods.put("8878450512091269427", new PVQ(i++, MetaAdapterFactory.getProperty(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x110396eaaa4L, 0x110396ec041L, "name"), "GeneratedFeedbackAspect"));
    pvqMethods.put("4754973013260639228", new PVQ(i++, MetaAdapterFactory.getProperty(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x110396eaaa4L, 0x110396ec041L, "name"), "Generated_FeedbackDescriptor"));
  }
  @NotNull
  @Override
  public PropertyValueQuery getPropertyValueQuery(@NotNull QueryKey identity) {
    PropertyValueQuery query = identity.forTemplateNode(pvqMethods);
    return (query != null ? query : super.getPropertyValueQuery(identity));
  }
  private static class PVQ extends PropertyValueQuery.Base {
    private final int methodKey;
    /*package*/ PVQ(int methodKey, SProperty property, String templateValue) {
      super(property, templateValue);
      this.methodKey = methodKey;
    }
    @Nullable
    public Object evaluate(@NotNull PropertyMacroContext ctx) throws GenerationFailureException {
      switch (methodKey) {
        case 0:
          return QueriesGenerated.propertyMacro_GetValue_2_0(ctx);
        case 1:
          return QueriesGenerated.propertyMacro_GetValue_3_0(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, IfMacroCondition> imcMethods = new HashMap<String, IfMacroCondition>();
  {
    int i = 0;
    imcMethods.put("668954446732559948", new IfMC(i++));
  }
  @NotNull
  @Override
  public IfMacroCondition getIfMacroCondition(@NotNull QueryKey identity) {
    IfMacroCondition query = identity.forTemplateNode(imcMethods);
    return (query != null ? query : super.getIfMacroCondition(identity));
  }
  private static class IfMC implements IfMacroCondition {
    private final int methodKey;
    public IfMC(int methodKey) {
      this.methodKey = methodKey;
    }
    @Override
    public boolean check(@NotNull IfMacroContext ctx) throws GenerationFailureException {
      switch (methodKey) {
        case 0:
          return QueriesGenerated.ifMacro_Condition_1_0(ctx);
        default:
          throw new GenerationFailureException(String.format("Inconsistent QueriesGenerated: there's no condition method for if macro %s (key: #%d)", ctx.getTemplateReference(), methodKey));
      }
    }
  }
  private final Map<String, ReferenceTargetQuery> rtqMethods = new HashMap<String, ReferenceTargetQuery>();
  {
    rtqMethods.put("2916065699089599886", new RTQ(0, "BaseConcept"));
    rtqMethods.put("6285588811486484649", new RTQ(1, "PROVIDER"));
  }
  @NotNull
  @Override
  public ReferenceTargetQuery getReferenceTargetQuery(@NotNull QueryKey queryKey) {
    ReferenceTargetQuery query = queryKey.forTemplateNode(rtqMethods);
    return (query != null ? query : super.getReferenceTargetQuery(queryKey));
  }
  private static class RTQ extends ReferenceTargetQuery.Base {
    private final int methodKey;
    /*package*/ RTQ(int methodKey, String templateValue) {
      super(templateValue);
      this.methodKey = methodKey;
    }
    @Nullable
    public Object evaluate(@NotNull ReferenceMacroContext ctx) throws GenerationFailureException {
      switch (methodKey) {
        case 0:
          return QueriesGenerated.referenceMacro_GetReferent_3_0(ctx);
        case 1:
          return QueriesGenerated.referenceMacro_GetReferent_3_1(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, VariableValueQuery> vvqMethods = new HashMap<String, VariableValueQuery>();
  {
    vvqMethods.put("8878450512091070846", new VVQ(0));
    vvqMethods.put("1938781798616685024", new VVQ(1));
  }
  @NotNull
  @Override
  public VariableValueQuery getVariableValueQuery(@NotNull QueryKey queryKey) {
    VariableValueQuery query = queryKey.forTemplateNode(vvqMethods);
    return (query != null ? query : super.getVariableValueQuery(queryKey));
  }
  private static class VVQ implements VariableValueQuery {
    private final int methodKey;
    /*package*/ VVQ(int methodKey) {
      this.methodKey = methodKey;
    }
    @Nullable
    public Object evaluate(@NotNull TemplateVarContext ctx) throws GenerationFailureException {
      switch (methodKey) {
        case 0:
          return QueriesGenerated.varMacro_Value_1_0(ctx);
        case 1:
          return QueriesGenerated.varMacro_Value_1_1(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, MapPostProcessor> mppMethods = new HashMap<String, MapPostProcessor>();
  {
    mppMethods.put("6624226371216036235", new PPQ(0));
  }
  @NotNull
  @Override
  public MapPostProcessor getMapPostProcessor(@NotNull QueryKey queryKey) {
    MapPostProcessor query = queryKey.forTemplateNode(mppMethods);
    return (query != null ? query : super.getMapPostProcessor(queryKey));
  }
  private static class PPQ implements MapPostProcessor {
    private final int methodKey;
    /*package*/ PPQ(int methodKey) {
      this.methodKey = methodKey;
    }
    public void invoke(@NotNull MapSrcMacroPostProcContext ctx) throws GenerationFailureException {
      switch (methodKey) {
        case 0:
          QueriesGenerated.mapSrcMacro_post_1_0(ctx);
          return;
        default:
          throw new GenerationFailureException(String.format("Inconsistent QueriesGenerated: there's no method for query %s (key: #%d)", ctx.getTemplateReference(), methodKey));
      }
    }
  }
  private static boolean isEmptyString(String str) {
    return str == null || str.isEmpty();
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept FeedbackPerConceptRoot$Vm = MetaAdapterFactory.getConcept(0x517077fde44f4338L, 0xa4751d29781dfdb8L, 0x6530303593ae1607L, "jetbrains.mps.lang.feedback.skeleton.structure.FeedbackPerConceptRoot");
    /*package*/ static final SConcept IfStatement$Q4 = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b217L, "jetbrains.mps.baseLanguage.structure.IfStatement");
    /*package*/ static final SConcept StaticMethodCall$Fg = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbbebabf09L, "jetbrains.mps.baseLanguage.structure.StaticMethodCall");
    /*package*/ static final SConcept StatementList$m_ = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b200L, "jetbrains.mps.baseLanguage.structure.StatementList");
  }

  private static final class PROPS {
    /*package*/ static final SProperty name$MnvL = MetaAdapterFactory.getProperty(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x110396eaaa4L, 0x110396ec041L, "name");
    /*package*/ static final SProperty stereotype$h2Bb = MetaAdapterFactory.getProperty(0x86ef829012bb4ca7L, 0x947f093788f263a9L, 0x5869770da61dfe27L, 0x5c5cb5cdd09abfb1L, "stereotype");
  }

  private static final class LINKS {
    /*package*/ static final SReferenceLink concept$NMNv = MetaAdapterFactory.getReferenceLink(0x517077fde44f4338L, 0xa4751d29781dfdb8L, 0x6530303593ae1607L, 0x63c2f3669ce56d5dL, "concept");
    /*package*/ static final SContainmentLink feedbacks$E$KJ = MetaAdapterFactory.getContainmentLink(0x517077fde44f4338L, 0xa4751d29781dfdb8L, 0x6530303593ae1607L, 0x6530303593ae9cf2L, "feedbacks");
    /*package*/ static final SContainmentLink actualArgument$pzdx = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x11857355952L, 0xf8c78301aeL, "actualArgument");
    /*package*/ static final SReferenceLink classifier$9NRM = MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x2724644c0ac833a5L, 0x2724644c0ac833a6L, "classifier");
    /*package*/ static final SContainmentLink creator$BsHW = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x10ab8473cc5L, 0x10ab847b486L, "creator");
    /*package*/ static final SContainmentLink language$rnIG = MetaAdapterFactory.getContainmentLink(0xf4ad079dbc714ffbL, 0x96009328705cf998L, 0x7d2f7947ef1533a5L, 0x179194ecf7e0953bL, "language");
    /*package*/ static final SContainmentLink model$2Sf4 = MetaAdapterFactory.getContainmentLink(0x86ef829012bb4ca7L, 0x947f093788f263a9L, 0x5869770da61dfe1eL, 0x5869770da61dfe2bL, "model");
  }
}
