'use strict'

const array_stream = require('../array_stream');
const resolve = require('path').resolve;
const readFile = require('fs').readFile;

module.exports = (entrypoint) => {

    entrypoint.module_root = entrypoint.base = '/node_modules/';

    return {    
        mic: (name, options, cb) => {

            // TODO access check

            readFile(resolve('./instance.nq'), (err, data) => {

                if (err) {
                    return cb(err); 
                }

                data = data.toString().split('\n');
                data.forEach((triple, index) => {
                    if (triple[0] === '#') {
                        return; //console.log('Comment:', triple);
                    }
                    data[index] = triple.replace(/<([^<>]*)>/g, '$1').split(' ', 3);
                });

                cb(null, array_stream(data));
            });
            //cb(null, require(entrypoint.network + '/' + name));
        },
        mod: (name, options, cb) => {
            cb(null, require(entrypoint.module_root + name));
        }
    };
};

@@ -257,24 +257,200 @@
 rn %7B
+%0A
     
-%0A        mic
+    stdin: process.stdin,%0A        stdout: process.stdout,%0A        stderr: process.stderr,%0A        modules: %7B%7D,%0A        instances: %7B%7D,%0A        events: %7B%7D,%0A        streams: %7B%7D,%0A        read
 : (n
@@ -655,16 +655,50 @@
      %7D%0A%0A
+                let triples = %5B%5D;%0A
         
@@ -832,16 +832,27 @@
  === '#'
+ %7C%7C !triple
 ) %7B%0A    
@@ -883,42 +883,8 @@
 rn; 
-//console.log('Comment:', triple);
 %0A   
@@ -926,19 +926,14 @@
     
-data%5Bindex%5D
+triple
  = t
@@ -982,16 +982,207 @@
  ', 3);%0A
+                    triples.push(%7B%0A                        subject: triple%5B0%5D,%0A                        predicate: triple%5B1%5D,%0A                        object: triple%5B2%5D%0A                    %7D);%0A
         
@@ -1232,20 +1232,23 @@
 _stream(
-data
+triples
 ));%0A    

test with triple as an object, instead of array