import { LitElement } from 'lit-element'

import { ReferenceMap, create, error } from '../../things-real'

import client from '../../app-shell/client'

class ThingsProvider extends LitElement {
  static get is() {
    return 'things-provider'
  }

  static get properties() {
    return {
      refProvider: Object
    }
  }

  connectedCallback() {
    super.connectedCallback()

    this.refProvider = new ReferenceMap(
      async (boardId, resolve, reject) => {
        try {
          const data = await client.fetchBoard(boardId)

          var board = data.board
          var model = JSON.parse(board.model)

          var scene

          try {
            scene = await this.refProvider.get(boardId)
            console.warn('Board fetched more than twice.', boardId)
          } catch (e) {
            scene = create({
              model,
              mode: 0,
              refProvider: this.refProvider
            })

            // s.app.baseUrl = undefined;
          }

          resolve(scene, {
            ...board,
            model
          })
        } catch (e) {
          error(e)
          reject(e)
        }
      },
      async (id, ref) => {
        ref.dispose()
      }
    )

    this.dispatchEvent(new CustomEvent('change', { bubbles: true, composed: true }))
  }
}

customElements.define(ThingsProvider.is, ThingsProvider)
