{  AppletDescription, AdapParams, ConfigType,
   params, flags, contains3d } from ./adapptypes.ts

// (At least some of) Joyce's pages have inline scripts that remove their
// own applet nodes, so we have to watch for them as they are added. Hence,
// this script runs at document_start, and watches for applets being added,
// setting up the joyceApplets structure as it goes

joyceApplets: AppletDescription[] .= []
obs := new MutationObserver (mutationList) =>
   for each change of mutationList
      for each newGenericNode of change.addedNodes
         newNode := (newGenericNode as HTMLElement)
         newParent := (change.target as HTMLElement)
         unless newNode.tagName is 'APPLET' then continue
         unless newNode.getAttribute('code') is 'Geometry' then continue
         id .= newParent.getAttribute 'id'
         unless id
            id = 'joyceApplet' + joyceApplets.length
            newParent.setAttribute 'id', id
         joyceApplets.push {
            html: newNode.outerHTML,
            params(newNode.children),
            id,
            width: parseInt(newNode.getAttribute('width') ?? '200'),
            height: parseInt(newNode.getAttribute('height') ?? '200') }

config := childList: true, subtree: true

obs.observe document.documentElement, config

function addScriptTag(url: string, params = '', module = false)
   return new Promise (resolve, reject) =>
      script := document.createElement 'script'
      script.addEventListener 'load', resolve
      script.addEventListener 'error', reject
      script.src = url
      if params then script.dataset.params = params
      if module then script.type = 'module'
      document.head.appendChild script

document.addEventListener "DOMContentLoaded", async =>
   finalJoyceApplets := document.querySelectorAll "applet[code='Geometry']"
   if finalJoyceApplets.length
      joyceApplets = []
      for jApp of finalJoyceApplets
         jParent := jApp.parentNode as HTMLElement
         unless jParent then continue
         id .= jParent.getAttribute 'id'
         unless id
            id = 'joyceApplet' + joyceApplets.length
            jParent.setAttribute 'id', id
         joyceApplets.push {
            html: jApp.outerHTML,
            params(jApp.children),
            id,
            width: parseInt(jApp.getAttribute('width') ?? '200'),
            height: parseInt(jApp.getAttribute('height') ?? '200') }
   if joyceApplets.length
      config := await browser.storage.local.get(flags) as ConfigType
      use3d .= false
      for each jApp of joyceApplets
         if contains3d jApp.params
            use3d = true
            break
      codebase .= browser.runtime.getURL 'deps/GeoGebra/HTML5/5.0/'
      codebase += (use3d or config.algebra) ? 'web3d' : 'webSimple'
      adapParams: AdapParams := {codebase, config, joyceApplets }
      apars := JSON.stringify(adapParams)
      addScriptTag(browser.runtime.getURL 'deps/GeoGebra/deployggb.js').then =>
         addScriptTag browser.runtime.getURL('adapptlet.js'), apars, true
