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