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

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import kotlinx.metadata.internal.common.KmModuleFragment;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.kotlin.stubs.common.references.KotlinJvmReferenceSolver;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import java.util.List;
import kotlinx.metadata.KmDeclarationContainer;
import java.util.ArrayList;
import org.jetbrains.mps.openapi.language.SContainmentLink;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;

@GeneratedClass(node = "r:d76e16ee-a528-4ca0-b2d6-9eed9a9b1d1c(jetbrains.mps.kotlin.stubs.common.metadata)/3463082351152539737", model = "r:d76e16ee-a528-4ca0-b2d6-9eed9a9b1d1c(jetbrains.mps.kotlin.stubs.common.metadata)")
public class KtModuleParser {
  public static void parseModule(KmModuleFragment fragment, SNode root, KotlinJvmReferenceSolver factory, String modelName) {
    parseModule(fragment, root, new VisitorContext(factory, modelName, root));
  }
  public static void parseModule(KmModuleFragment fragment, SNode root, final VisitorContext context) {
    fragment.getClasses().forEach((kmClass) -> context.addClass(KtClassParser.parseClass(kmClass, context, false)));
    ListSequence.fromList(SLinkOperations.getChildren(root, LINKS.declarations$NgHw)).addSequence(ListSequence.fromList(parseDeclarations(fragment.getPkg(), context.getPackageName(), context)));
    context.flushClasses();
  }

  public static List<SNode> parseDeclarations(KmDeclarationContainer pkg, final String parentFqName, final VisitorContext context) {
    final List<SNode> decls = new ArrayList<SNode>();

    pkg.getTypeAliases().forEach((typeAlias) -> ListSequence.fromList(decls).addElement(KtTypeAliasParser.parseTypeAlias(typeAlias, parentFqName, context)));

    pkg.getProperties().forEach((prop) -> ListSequence.fromList(decls).addElement(KtPropertyVisitor.parseProperty(prop, parentFqName, context)));

    pkg.getFunctions().forEach((fun) -> ListSequence.fromList(decls).addElement(KtFunctionParser.parseFunction(fun, parentFqName, context)));

    return decls;
  }

  private static final class LINKS {
    /*package*/ static final SContainmentLink declarations$NgHw = MetaAdapterFactory.getContainmentLink(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x28bef6d7551af529L, 0x28bef6d7551af889L, "declarations");
  }
}
