var pull = require('pull-stream')
var tape = require('tape')
var Flume = require('../')
var MemLog = require('flumelog-memory')

module.exports = function (flume) {
  var errorEnabled = false

  var db = flume(MemLog(), false, (err, val, cb) => {
    setTimeout(() => {
      if (true === errorEnabled) {
        err = new Error('error enabled for testing')
      }
      val.map = true
      cb(err, val)
    }, Math.random() * 10)
  })

  tape('get map', function (t) {
    db.since(function (v) {
      console.log("SINCE", v)
    }, false)

    db.append({foo: 1}, function (err, seq) {
      if(err) throw err
      console.log("GET", err, seq)
      db.get(seq, function (err, value) {
        if(err) throw err
        console.log(  "GET", value)
        t.deepEqual(value.foo, 1)
        t.deepEqual(value.map, true)
        t.end()
      })
    })
  })

  tape('stream map only values', function (t) {
    db.append({foo: 2}, function (err, seq) {
      if(err) throw err
      console.log("GET", err, seq)
      pull(
        db.stream({seqs: false, values: true }),
        pull.collect(function (err, ary) {
          if(err) throw err
          t.deepEqual(ary, [ { foo: 1, map: true }, { foo: 2, map: true } ])
          t.end()
        })
      )
    })
  })

  tape('stream map only seqs', function (t) {
    db.append({foo: 3}, function (err, seq) {
      if(err) throw err
      console.log("GET", err, seq)
      pull(
        db.stream({seqs: true, values: false }),
        pull.collect(function (err, ary) {
          if(err) throw err
          t.deepEqual(ary,  [ 0, 1, 2 ])
          t.end()
        })
      )
    })
  })

  tape('stream map values and seqs', function (t) {
    db.append({foo: 4}, function (err, seq) {
      if(err) throw err
      console.log("GET", err, seq)
      pull(
        db.stream({seqs: true, values: true }),
        pull.collect(function (err, ary) {
          if(err) throw err
          t.deepEqual(ary,  [
            { value: { foo: 1, map: true }, seq: 0 },
            { value: { foo: 2, map: true }, seq: 1 },
            { value: { foo: 3, map: true }, seq: 2 },
            { value: { foo: 4, map: true }, seq: 3 }
          ])
          t.end()
        })
      )
    })
  })

  tape('get map with error', function (t) {
    errorEnabled = true
    db.append({foo: 13}, function (err, seq) {
      if(err) throw err
      console.log("GET", err, seq)
      db.get(seq, function (err, value) {
        console.log(  "GET", err, value)
        t.ok(err)
        t.end()
      })
    })
  })

  tape('close', function (t) {
    db.close(function () {
      t.end()
    })
  })
}

if(!module.parent)
  module.exports(Flume)



@@ -221,21 +221,16 @@
 false, (
-err, 
 val, cb)
@@ -305,14 +305,18 @@
     
-err = 
+return cb(
 new 
@@ -349,16 +349,17 @@
 esting')
+)
 %0A      %7D
@@ -359,16 +359,102 @@
       %7D%0A
+      val = JSON.parse(JSON.stringify(val))%0A      val.called = (val.called  %7C%7C 0) + 1%0A
       va
@@ -475,19 +475,20 @@
      cb(
-err
+null
 , val)%0A 
@@ -901,24 +901,61 @@
 .map, true)%0A
+        t.deepEqual(value.called, 1)%0A
         t.en
@@ -1295,32 +1295,43 @@
 ary, %5B %7B foo: 1,
+ called: 1,
  map: true %7D, %7B 
@@ -1329,32 +1329,43 @@
 rue %7D, %7B foo: 2,
+ called: 1,
  map: true %7D %5D)%0A
@@ -2132,24 +2132,35 @@
 e: %7B foo: 1,
+ called: 1,
  map: true %7D
@@ -2201,16 +2201,27 @@
  foo: 2,
+ called: 1,
  map: tr
@@ -2266,16 +2266,27 @@
  foo: 3,
+ called: 1,
  map: tr
@@ -2331,16 +2331,27 @@
  foo: 4,
+ called: 1,
  map: tr
@@ -2873,8 +2873,9 @@
 lume)%0A%0A%0A
+%0A

check number of times mapper is called