import { RSVPController } from '../src/RSVPController.js'

export class RSVPProvider extends HTMLElement {
  constructor() {
    super()
    const shadow = this.attachShadow({ mode: 'open' })
    const provider = document.createElement('state-provider')
    const slot = document.createElement('slot')
    provider.setAttribute('name', 'rsvp-controller')
    provider.appendChild(slot)
    shadow.appendChild(provider)

    const controller = new RSVPController()
    controller.onChange = this.provider.notifyObservers()

    this.addEventListener('rsvp-event', function(e) {
      let { action, payload } = e.detail
      controller.handleAction(action, payload)
    })
  }
}

window.customElements.define('rsvp-provider', RSVPProvider)
