'use strict'

export default function (rosters) {
  let handler = {
    set (target, prop, value, receiver) {
      if (typeof value !== 'object') {
        let ok = Reflect.set(target, prop, value)
        if (ok) {
          let link = rosters.get(receiver)
          if (link.has(prop)) {
            link.get(prop).forEach(f => f(value))
          } else {
            link.set(prop, new Set())
          }
        }
        return ok
      } else if (value) {
        let p = deepProxy(value)
        return Reflect.set(target, prop, p)
      }
    }
  }

  function deepProxy (obj) {
    let sets = [],
        out = Array.isArray(obj) ? [] : {}
    Object.keys(obj).forEach(k => {
      if (typeof obj[k] === 'object') {
        if (obj[k]) out[k] = deepProxy(obj[k])
      } else {
        sets.push(k)
        out[k] = obj[k]
      }
    })
    let p = new Proxy(out, handler)
    let link = rosters.get(p) || rosters.set(p, new Map()).get(p)
    sets.forEach(k => !link.has(k) && link.set(k, new Set()))
    return p
  }
  return deepProxy
}

@@ -595,27 +595,8 @@
 let 
-sets = %5B%5D,%0A        
 out 
@@ -668,24 +668,34 @@
 %7B%0A      if (
+obj%5Bk%5D && 
 typeof obj%5Bk
@@ -723,20 +723,8 @@
     
- if (obj%5Bk%5D)
  out
@@ -766,29 +766,8 @@
 e %7B%0A
-        sets.push(k)%0A
     
@@ -855,26 +855,8 @@
 nk =
- rosters.get(p) %7C%7C
  ros
@@ -893,12 +893,24 @@
     
-sets
+Object.keys(obj)
 .for

add scopes to subscriptions in store