<template><div /></template>

<script>
import { PluginSlotMixin } from './mixins.js'

export default {
  name: 'AutoRun',
  mixins: [PluginSlotMixin],
  data () {
    return {
      launchers: [],
      handleResponseName: null,
      handleErrorName: null
    }
  },
  methods: {
    getSlotExposedMethods () {
      return {}
    },
    initializeSlot (options) {
      this.launchers = options.launchers
      this.handleResponseName = options.handleResponse
      this.handleErrorName = options.handleError
      this.handleAutoRun('pageLoad')
    },
    handleAutoRun (event) {
      this.launchers.map(launcher => {
        if (launcher.trigger === event) {
          this.pluginIframeConnection.promise.then(iframe => {
            iframe[launcher.callback](this.context)
          })
        }
      })
    },
    async runPendingExecutions () {
      const iframe = await this.pluginIframeConnection.promise
      await Promise.all(this.launchers.map(async launcher => {
        if (launcher.trigger === 'pageExit') {
          await iframe[launcher.callback](this.context)
        }
      }))
    }
  }
}
</script>
