<script>
import commonMixin from '@/base/mixins/common.js'
export default {
  name: 'bs-snap',
  render () { },
  mixins: [commonMixin('interaction')],
  props: {
    layerId: {
      type: String
    },
    actived: {
      type: Boolean,
      default: true
    },
    features: {
      type: Object
    }
  },
  watch: {
    actived (flag) {
      this.originInstance.setActive(flag)
    },
    features (val) {
      this.reload()
    },
    layerId (val) {
      this.reload()
    }
  },
  methods: {
    load () {
      const { ol, $rootComponent, actived, layerId, features } = this
      if (this.originInstance) {
        $rootComponent.removeInteraction(this.originInstance)
      }
      let vector = null
      if (layerId) {
        if ($rootComponent.getLayer(layerId)) {
          vector = $rootComponent.getLayer(layerId)
        }
      }
      const snap = new ol.interaction.Snap({
        source: vector ? vector.getSource() : undefined,
        features: features || undefined
      })
      this.originInstance = snap
      $rootComponent.addInteraction(snap, actived)
    }
  }
}
</script>
