'use strict';

var adapters = ['http', 'local'];

adapters.forEach(function (adapter) {
  describe('test.design_docs.js-' + adapter, function () {

    var dbs = {};

    beforeEach(function () {
      dbs.name = testUtils.adapterUrl(adapter, 'testdb');
    });

    afterEach(function (done) {
      testUtils.cleanup([dbs.name], done);
    });

    var doc = {
      _id: '_design/foo',
      views: {
        scores: {
          map: 'function (doc) { if (doc.score) { emit(null, doc.score); } }',
          reduce: 'function (keys, values, rereduce) { return sum(values); }'
        }
      },
      filters: { even: 'function (doc) { return doc.integer % 2 === 0; }' }
    };

    it('Test writing design doc', function (done) {
      var db = new PouchDB(dbs.name);
      db.post(doc, function (err) {
        should.not.exist(err, 'Wrote design doc');
        db.get('_design/foo', function (err) {
          done(err);
        });
      });
    });

    it('Changes filter', function (done) {
      var docs1 = [
        doc,
        {_id: '0', integer: 0},
        {_id: '1', integer: 1},
        {_id: '2', integer: 2},
        {_id: '3', integer: 3}
      ];
      var docs2 = [
        {_id: '4', integer: 4},
        {_id: '5', integer: 5},
        {_id: '6', integer: 6},
        {_id: '7', integer: 7}
      ];

      var db = new PouchDB(dbs.name);
      var count = 0;
      db.bulkDocs({ docs: docs1 }, function () {
        var changes = db.changes({
          live: true,
          filter: 'foo/even'
        }).on('change', function () {
          count += 1;
          if (count === 4) {
            changes.cancel();
          }
        }).on('complete', function (result) {
          result.status.should.equal('cancelled');
          done();
        }).on('error', done);
        db.bulkDocs({ docs: docs2 }, {});
      });
    });

    it('Basic views', function (done) {

      var docs1 = [
        doc,
        {_id: 'dale', score: 3},
        {_id: 'mikeal', score: 5},
        {_id: 'max', score: 4},
        {_id: 'nuno', score: 3}
      ];
      var db = new PouchDB(dbs.name);
      // Test invalid if adapter doesnt support mapreduce
      if (!db.query) {
        return done();
      }

      db.bulkDocs({ docs: docs1 }, function () {
        db.query('foo/scores', { reduce: false }, function (err, result) {
          result.rows.should.have.length(4, 'Correct # of results');
          db.query('foo/scores', function (err, result) {
            result.rows[0].value.should.equal(15, 'Reduce gave correct result');
            done();
          });
        });
      });
    });

    it('Indexing event', function (done) {
      var docs1 = [
        doc,
        {_id: 'dale', score: 3},
        {_id: 'mikeal', score: 5},
        {_id: 'max', score: 4},
        {_id: 'nuno', score: 3},
      ];
      var db = new PouchDB(dbs.name);
      // Test invalid if adapter doesnt support mapreduce
      if (!db.query || adapter !== 'local') {
        return done();
      }

      let indexingEvents = [];

      db.on('indexing', function (result) {
        indexingEvents.push(result);
      });

      db.bulkDocs({ docs: docs1 }, function () {
        db.query('foo/scores', { reduce: false }, function () {          
          indexingEvents.length.should.equal(2);
          indexingEvents[0]['indexed_docs'].should.equal(0);
          indexingEvents[1]['last_seq'].should.equal(5);
          indexingEvents[1]['results_count'].should.equal(5);
          indexingEvents[1]['indexed_docs'].should.equal(5);

          done();
        });
      });
    });

    it('Concurrent queries', function (done) {
      var db = new PouchDB(dbs.name);
      // Test invalid if adapter doesnt support mapreduce
      if (!db.query) {
        return done();
      }

      db.bulkDocs({
        docs: [
          doc,
          {_id: 'dale', score: 3}
        ]
      }, function () {
        var cnt = 0;
        db.query('foo/scores', { reduce: false }, function (err, result) {
          result.rows.should.have.length(1, 'Correct # of results');
          if (++cnt === 2) {
            done();
          }
        });
        db.query('foo/scores', { reduce: false }, function (err, result) {
          result.rows.should.have.length(1, 'Correct # of results');
          if (++cnt === 2) {
            done();
          }
        });
      });
    });

  });
});

@@ -2639,31 +2639,27 @@
 event', 
-function (done)
+async () =%3E
  %7B%0A     
@@ -2651,35 +2651,37 @@
 c () =%3E %7B%0A      
-var
+const
  docs1 = %5B%0A     
@@ -2828,35 +2828,35 @@
 %0A      %5D;%0A      
-var
+let
  db = new PouchD
@@ -2978,39 +2978,32 @@
 %7B%0A        return
- done()
 ;%0A      %7D%0A%0A     
@@ -3052,25 +3052,16 @@
 dexing',
- function
  (result
@@ -3053,32 +3053,35 @@
 exing', (result)
+ =%3E
  %7B%0A        index
@@ -3113,32 +3113,38 @@
      %7D);%0A%0A      
+await 
 db.bulkDocs(%7B do
@@ -3154,39 +3154,30 @@
  docs1 %7D
-, function () %7B
+);
 %0A      
- 
+await
  db.quer
@@ -3213,38 +3213,12 @@
 se %7D
-, function () %7B          %0A    
+);%0A%0A
     
@@ -3256,36 +3256,32 @@
 equal(2);%0A      
-    
 indexingEvents%5B0
@@ -3315,20 +3315,16 @@
 ual(0);%0A
-    
       in
@@ -3366,36 +3366,32 @@
 equal(5);%0A      
-    
 indexingEvents%5B1
@@ -3428,28 +3428,24 @@
 l(5);%0A      
-   
- 
 indexingEven
@@ -3486,49 +3486,8 @@
 (5);
-%0A%0A          done();%0A        %7D);%0A      %7D);
 %0A   

Update indexing event test to use async/await instead of callbacks