const Sublayer = require('./Sublayer')

class Memberlayer extends Sublayer {
  constructor (master, options) {
    super(master, options)

    this.master.mainlayer.on('add', this.featureOnMainModified.bind(this))
    this.master.mainlayer.on('remove', this.featureOnMainModified.bind(this))
    this.on('add', this.featureMemberModified.bind(this))
    this.on('remove', this.featureMemberModified.bind(this))
  }

  featureMemberModified (feature, data) {
    feature.memberOf.forEach(master => {
      this.master.mainlayer.scheduleReprocess(master.relation.id)
    })
  }

  featureOnMainModified (feature) {
    if (!feature.members) {
      return
    }

    feature.members.forEach(member => {
      if (member.id in this.visibleFeatures) {
        this.scheduleReprocess(member.id)
      }
    })
  }

  twigData (ob) {
    let result = super.twigData(ob)

    for (var k in this.master.mainlayer.visibleFeatures) {
      let feature = this.master.mainlayer.visibleFeatures[k]
      if (feature.object.members) {
        feature.object.members.forEach((member, sequence) => {
          if (member.id === ob.id) {
            if (!('masters' in result)) {
              result.masters = []
            }

            result.masters.push({
              id: feature.id,
              type: feature.object.type,
              osm_id: feature.object.osm_id,
              tags: feature.object.tags,
              meta: feature.object.meta,
              role: member.role,
              sequence
            })
          }
        })
      }
    }

    return result
  }
}

module.exports = Memberlayer

@@ -139,37 +139,78 @@
 %0A    this.master
-.main
+layer = this.master.mainlayer%0A%0A    this.master
 layer.on('add', 
@@ -259,29 +259,24 @@
  this.master
-.main
 layer.on('re
@@ -541,29 +541,24 @@
  this.master
-.main
 layer.schedu
@@ -910,37 +910,32 @@
 k in this.master
-.main
 layer.visibleFea
@@ -970,29 +970,24 @@
  this.master
-.main
 layer.visibl

use this.masterlayer for referencing master layer