package jetbrains.mps.kotlin.baseLanguage.runtime.plugin;

/*Generated by MPS */

import jetbrains.mps.smodel.structure.Extension;
import jetbrains.mps.kotlin.baseLanguage.typeConversion.TypeConverterEngine;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.kotlin.baseLanguage.typeConversion.TypeConverterEngineWithClass;
import jetbrains.mps.kotlin.baseLanguage.typeConversion.ClassToClassEnum;
import jetbrains.mps.kotlin.baseLanguage.toKotlin.KotlinGenericClassBuilder;
import jetbrains.mps.kotlin.baseLanguage.toKotlin.KotlinClassTypeBuilder;
import jetbrains.mps.kotlin.baseLanguage.typeConversion.ClassToConceptEnum;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SConceptOperations;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.kotlin.baseLanguage.toKotlin.JavaToKtConversion;
import jetbrains.mps.baseLanguage.behavior.IClassifierType__BehaviorDescriptor;
import jetbrains.mps.smodel.builder.SNodeBuilder;
import org.jetbrains.mps.openapi.language.SConcept;
import org.jetbrains.mps.openapi.language.SReferenceLink;
import org.jetbrains.mps.openapi.language.SContainmentLink;

public class DefaultJavaToKtConversions extends Extension.Default<TypeConverterEngine<SNode, SNode>> {
  public DefaultJavaToKtConversions() {
    super("jetbrains.mps.kotlin.baseLanguage.runtime.JavaToKtConverters");
  }

  public void activate() {
    engine = new TypeConverterEngineWithClass<SNode, SNode>(CONCEPTS.ClassifierType$bL, LINKS.classifier$cxMr);

    // Class -> Class
    for (ClassToClassEnum entry : ClassToClassEnum.values()) {
      if (entry.withGenerics) {
        engine.declareMapping(entry.javaPtr, new KotlinGenericClassBuilder(entry.kotlinPtr, false));
      } else {
        engine.declareMapping(entry.javaPtr, new KotlinClassTypeBuilder(entry.kotlinPtr, false));
      }
    }

    // Concept -> Class
    for (ClassToConceptEnum entry : ClassToConceptEnum.values()) {
      engine.declareMapping(entry.javaConcept, new KotlinClassTypeBuilder(entry.kotlinPtr, entry.toKotlinNullable));
    }

    // ClassifierType -> JavaClassType
    engine.declareMapping(CONCEPTS.ClassifierType$bL, (it) -> {
      SNode node = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0x9e4ff22b60f143efL, 0xa50bf9f0fcec22e0L, 0x68fcefc6c20600f5L, "jetbrains.mps.kotlin.javaRefs.structure.JavaClassType"));
      SNode clType = SNodeOperations.cast(it, CONCEPTS.ClassifierType$bL);
      SLinkOperations.setTarget(node, LINKS.javaClass$CQOW, SLinkOperations.getTarget(clType, LINKS.classifier$cxMr));
      JavaToKtConversion.convertProjectionsInto(IClassifierType__BehaviorDescriptor.getTypeParameters_id6r77ob2URYe.invoke(clType), SLinkOperations.getChildren(node, LINKS.typeProjections$vhti));
      return node;
    });

    // KtClassifierType -> ClassType
    engine.declareMapping(CONCEPTS.KotlinClassifierType$jV, (it) -> {
      // Welcome back to kotlin realm little class type :)
      SNode node = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x28bef6d7551af4dfL, "jetbrains.mps.kotlin.structure.ClassType"));
      SNode clType = SNodeOperations.cast(it, CONCEPTS.KotlinClassifierType$jV);
      SLinkOperations.setTarget(node, LINKS.class$ExdX, SLinkOperations.getTarget(clType, LINKS.classifier$5Cta));
      JavaToKtConversion.convertProjectionsInto(SLinkOperations.getChildren(clType, LINKS.parameter$EcOI), SLinkOperations.getChildren(node, LINKS.typeProjections$vhti));
      return node;
    });

    // KtTypeParameterReference -> TypeParameterReference
    engine.declareMapping(CONCEPTS.KotlinTypeParameterReference$T7, (it) -> createTypeParameterReference_euwhrf_a0a1a0p0b(SLinkOperations.getTarget(SNodeOperations.as(it, CONCEPTS.KotlinTypeParameterReference$T7), LINKS.typeParameter$U125)));

    // TypeVariableReference -> JavaTypeVariableReference
    engine.declareMapping(CONCEPTS.TypeVariableReference$WL, (it) -> createJavaTypeVariableReferenceType_euwhrf_a0a1a0s0b(SLinkOperations.getTarget(SNodeOperations.as(it, CONCEPTS.TypeVariableReference$WL), LINKS.typeVariableDeclaration$Lz1I)));


    // KtFileClass -> KtFileClass (type specific to BL)
    engine.declareMapping(CONCEPTS.KotlinFileClassifierType$M1, (it) -> SNodeOperations.as(it, CONCEPTS.KotlinFileClassifierType$M1));

  }

  private TypeConverterEngineWithClass<SNode, SNode> engine;


  public TypeConverterEngine<SNode, SNode> get() {
    return engine;
  }
  private static SNode createTypeParameterReference_euwhrf_a0a1a0p0b(SNode p0) {
    SNodeBuilder n0 = new SNodeBuilder().init(CONCEPTS.TypeParameterReference$ya);
    n0.setReferenceTarget(LINKS.parameter$ofYr, p0);
    return n0.getResult();
  }
  private static SNode createJavaTypeVariableReferenceType_euwhrf_a0a1a0s0b(SNode p0) {
    SNodeBuilder n0 = new SNodeBuilder().init(CONCEPTS.JavaTypeVariableReferenceType$TS);
    n0.setReferenceTarget(LINKS.typeVariable$LHQO, p0);
    return n0.getResult();
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept ClassifierType$bL = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101de48bf9eL, "jetbrains.mps.baseLanguage.structure.ClassifierType");
    /*package*/ static final SConcept KotlinClassifierType$jV = MetaAdapterFactory.getConcept(0x2405a196e75d462cL, 0x938bae8e3fac20aaL, 0x68fcefc6c20b5c6cL, "jetbrains.mps.baseLanguage.kotlinRefs.structure.KotlinClassifierType");
    /*package*/ static final SConcept KotlinTypeParameterReference$T7 = MetaAdapterFactory.getConcept(0x2405a196e75d462cL, 0x938bae8e3fac20aaL, 0x4c40dac702493da9L, "jetbrains.mps.baseLanguage.kotlinRefs.structure.KotlinTypeParameterReference");
    /*package*/ static final SConcept TypeVariableReference$WL = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x102467229d8L, "jetbrains.mps.baseLanguage.structure.TypeVariableReference");
    /*package*/ static final SConcept KotlinFileClassifierType$M1 = MetaAdapterFactory.getConcept(0x2405a196e75d462cL, 0x938bae8e3fac20aaL, 0x13429f603fcbe305L, "jetbrains.mps.baseLanguage.kotlinRefs.structure.KotlinFileClassifierType");
    /*package*/ static final SConcept TypeParameterReference$ya = MetaAdapterFactory.getConcept(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x21e0c9232886358dL, "jetbrains.mps.kotlin.structure.TypeParameterReference");
    /*package*/ static final SConcept JavaTypeVariableReferenceType$TS = MetaAdapterFactory.getConcept(0x9e4ff22b60f143efL, 0xa50bf9f0fcec22e0L, 0x729709d72e03236aL, "jetbrains.mps.kotlin.javaRefs.structure.JavaTypeVariableReferenceType");
  }

  private static final class LINKS {
    /*package*/ static final SReferenceLink classifier$cxMr = MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101de48bf9eL, 0x101de490babL, "classifier");
    /*package*/ static final SReferenceLink javaClass$CQOW = MetaAdapterFactory.getReferenceLink(0x9e4ff22b60f143efL, 0xa50bf9f0fcec22e0L, 0x68fcefc6c20600f5L, 0x68fcefc6c2060179L, "javaClass");
    /*package*/ static final SContainmentLink typeProjections$vhti = MetaAdapterFactory.getContainmentLink(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x5b1dd60162c97579L, 0x5b1dd60162c9757cL, "typeProjections");
    /*package*/ static final SReferenceLink class$ExdX = MetaAdapterFactory.getReferenceLink(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x5c7be90f2440b378L, 0x5c7be90f2440b37bL, "class");
    /*package*/ static final SReferenceLink classifier$5Cta = MetaAdapterFactory.getReferenceLink(0x2405a196e75d462cL, 0x938bae8e3fac20aaL, 0x68fcefc6c20b5c6cL, 0x68fcefc6c20e3720L, "classifier");
    /*package*/ static final SContainmentLink parameter$EcOI = MetaAdapterFactory.getContainmentLink(0x2405a196e75d462cL, 0x938bae8e3fac20aaL, 0x68fcefc6c20b5c6cL, 0x102419671abL, "parameter");
    /*package*/ static final SReferenceLink typeParameter$U125 = MetaAdapterFactory.getReferenceLink(0x2405a196e75d462cL, 0x938bae8e3fac20aaL, 0x4c40dac702493da9L, 0x4c40dac702494c3aL, "typeParameter");
    /*package*/ static final SReferenceLink typeVariableDeclaration$Lz1I = MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x102467229d8L, 0x1024673a581L, "typeVariableDeclaration");
    /*package*/ static final SReferenceLink parameter$ofYr = MetaAdapterFactory.getReferenceLink(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x21e0c9232886358dL, 0x21e0c9232886358eL, "parameter");
    /*package*/ static final SReferenceLink typeVariable$LHQO = MetaAdapterFactory.getReferenceLink(0x9e4ff22b60f143efL, 0xa50bf9f0fcec22e0L, 0x729709d72e03236aL, 0x729709d72e03236dL, "typeVariable");
  }
}
