package jetbrains.mps.kotlin.stubs.common.metadata;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import org.jetbrains.mps.openapi.model.SNode;
import kotlinx.metadata.KmConstructor;
import kotlinx.metadata.internal.metadata.deserialization.Flags;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SConceptOperations;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.kotlin.stubs.common.FunctionIdBuilder;
import org.jetbrains.mps.openapi.language.SContainmentLink;
import org.jetbrains.mps.openapi.language.SConcept;

@GeneratedClass(node = "r:d76e16ee-a528-4ca0-b2d6-9eed9a9b1d1c(jetbrains.mps.kotlin.stubs.common.metadata)/2993321679752331768", model = "r:d76e16ee-a528-4ca0-b2d6-9eed9a9b1d1c(jetbrains.mps.kotlin.stubs.common.metadata)")
public class KtConstructorParser {
  public static SNode parseConstructor(KmConstructor constructor, String receiverType, final VisitorContext context) {
    int flags = constructor.getFlags();
    final Boolean isSecondary = Flags.IS_SECONDARY.get(flags);
    final SNode node = (isSecondary ? SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x28bef6d7551af393L, "jetbrains.mps.kotlin.structure.SecondaryConstructor")) : SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x28bef6d7551af418L, "jetbrains.mps.kotlin.structure.PrimaryConstructor")));

    SLinkOperations.setTarget(node, LINKS.visibility$vnSV, SConceptOperations.createNewNode(SNodeOperations.asInstanceConcept(EnumFlags.getVisibility(Flags.VISIBILITY.get(flags)))));
    final FunctionIdBuilder functionId = new FunctionIdBuilder(context, FunctionIdBuilder.CONSTRUCTOR_ID_PREFIX, receiverType);


    constructor.getValueParameters().forEach((param) -> {
      SNode paramNode;
      if (isSecondary) {
        paramNode = SLinkOperations.addNewChild(SNodeOperations.cast(node, CONCEPTS.SecondaryConstructor$Lg), LINKS.parameters$dfEr, null);
      } else {
        paramNode = SLinkOperations.addNewChild(SNodeOperations.cast(node, CONCEPTS.PrimaryConstructor$QJ), LINKS.parameters$$EEQ, null);
      }
      functionId.addArgument(KtParameterParser.parseParameter(param, paramNode, context));
    });

    context.addAnnotations(constructor.visitExtensions(AnnotationExtension.type), node);

    // Set id
    functionId.applyOn(node);
    return node;
  }

  private static final class LINKS {
    /*package*/ static final SContainmentLink visibility$vnSV = MetaAdapterFactory.getContainmentLink(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x631027d1c4c4e03fL, 0x631027d1c4c4e040L, "visibility");
    /*package*/ static final SContainmentLink parameters$dfEr = MetaAdapterFactory.getContainmentLink(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x28bef6d755909980L, 0x28bef6d755909981L, "parameters");
    /*package*/ static final SContainmentLink parameters$$EEQ = MetaAdapterFactory.getContainmentLink(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x28bef6d7551af418L, 0x28bef6d7551af6dfL, "parameters");
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept SecondaryConstructor$Lg = MetaAdapterFactory.getConcept(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x28bef6d7551af393L, "jetbrains.mps.kotlin.structure.SecondaryConstructor");
    /*package*/ static final SConcept PrimaryConstructor$QJ = MetaAdapterFactory.getConcept(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x28bef6d7551af418L, "jetbrains.mps.kotlin.structure.PrimaryConstructor");
  }
}
