import PackageNode from './package-node';
import ResourceGroupNode from './resource-group-node';
import ModelGroupNode from './model-group-node';
import ResourceNode from './resource-node';
import ModelNode from './model-node';
import ActionNode from './action-node';

export default class ApibParser {
  constructor() {
    this.nodeClasses = [
      ActionNode,
      ModelNode,
      ResourceNode,
      ModelGroupNode,
      ResourceGroupNode,
      PackageNode
    ];
  }

  async parse(markdownString) {
    let lines = markdownString.split(/\r?\n/);
    let root = new PackageNode();

    root.name = 'Document';

    let parsingNote = false;
    let node = root;

    for (let line of lines) {
      if (!parsingNote && line[0] === '#') {
        let depth = line.indexOf(' ');
        let parent = node.findRecentParent(depth);
        let nodeClass = this.nodeClasses.find(p => p.canAcceptHeader(line));
        node = new nodeClass();
        node.header = line;
        parent.addChild(node);
      } else if (line.trim().indexOf(':::') === 0) {
        parsingNote = !parsingNote;
        node.lines.push(line);
      } else {
        node.lines.push(line);
      }
    }

    return root;
  }
}

@@ -1,12 +1,48 @@
+import ApibNode from './apib-node';%0A
 import Packa
@@ -63,32 +63,32 @@
 /package-node';%0A
-
 import ResourceG
@@ -609,23 +609,20 @@
 new 
-Package
+Apib
 Node();%0A
 %0A   
@@ -621,38 +621,29 @@
 ();%0A
-%0A
     
-root.name = 'Document'
+let node = root
 ;%0A
-%0A
     
@@ -666,37 +666,16 @@
 = false;
-%0A    let node = root;
 %0A%0A    fo
@@ -982,23 +982,129 @@
     
-parent.addChild
+node.depth = depth;%0A        node.parent = parent;%0A        parent.checkAcceptableChild(node);%0A        parent.children.push
 (nod
@@ -1285,15 +1285,66 @@
   %7D%0A
-
     %7D%0A%0A
+    // Normalize the depth.%0A    root.setDepth(0);%0A%0A
     

make parser more general