var crdt = require('..')
var a = require('assertions')

exports['test'] = function (t) {

  var doc = new crdt.Doc()
  var set = doc.createSet('type', 'thing')
  var set2 = doc.createSet('type', 'other')

  function log(set) {

    set.on('add', function (row) {
      console.log('add', set.value,'->', row.state)
    })
    set.on('remove', function (row) {
      console.log('rm', set.value,'->', row.state)
    })

  }

  log(set)
  log(set2)

  doc.add({id: 'a', type: 'thing', what: 3})
  doc.add({id: 'b', type: 'thing', what: 5})
  doc.add({id: 'a', type: 'other', what: 7})
  doc.add({id: 'c', type: 'thing', what: 9})

  console.log(set.toJSON())
  console.log(set2.toJSON())

  a.deepEqual(set.toJSON(), [
    {id: 'b', type: 'thing', what: 5},
    {id: 'c', type: 'thing', what: 9},
  ])

  a.deepEqual(set2.toJSON(), [
    {id: 'a', type: 'other', what: 7}
  ])

  console.log('passed')
  t.end()
}

@@ -905,8 +905,875 @@
 end()%0A%7D%0A
+%0Aexports%5B'test - post'%5D = function (t) %7B%0A%0A  var doc = new crdt.Doc()%0A%0A  doc.add(%7Bid: 'a', type: 'thing', what: 3%7D)%0A  doc.add(%7Bid: 'b', type: 'thing', what: 5%7D)%0A  doc.add(%7Bid: 'a', type: 'other', what: 7%7D)%0A  doc.add(%7Bid: 'c', type: 'thing', what: 9%7D)%0A%0A  var set = doc.createSet('type', 'thing')%0A  var set2 = doc.createSet('type', 'other')%0A%0A  function log(set) %7B%0A%0A    set.on('add', function (row) %7B%0A      console.log('add', set.value,'-%3E', row.state)%0A    %7D)%0A    set.on('remove', function (row) %7B%0A      console.log('rm', set.value,'-%3E', row.state)%0A    %7D)%0A%0A  %7D%0A%0A  log(set)%0A  log(set2)%0A%0A  console.log(set.toJSON())%0A  console.log(set2.toJSON())%0A%0A  a.deepEqual(set.toJSON(), %5B%0A    %7Bid: 'b', type: 'thing', what: 5%7D,%0A    %7Bid: 'c', type: 'thing', what: 9%7D,%0A  %5D)%0A%0A  a.deepEqual(set2.toJSON(), %5B%0A    %7Bid: 'a', type: 'other', what: 7%7D%0A  %5D)%0A%0A  console.log('passed')%0A  t.end()%0A%7D%0A%0A%0A

allow set creation after document load