'use client'

import { useEffect, useRef } from "react"

interface Module {
  setup?: (wabt?: any) => Promise<undefined>
  cleanup?: () => Promise<undefined>
}

export default function PageScript({ script, wasm }: { script?: string, wasm?: Uint8Array }) {
  const moduleRef = useRef<Module | null>(null)

  useEffect(() => {
    if (!script) return undefined

    import(/* webpackIgnore: true */ script)
      .then(scriptModule => {
        if (!wasm) return [scriptModule]

        return WebAssembly.instantiate(Uint8Array.from(wasm)).then(({ instance }) => [scriptModule, instance])
      })
      .then(([scriptModule, wasmModule]) => {
        moduleRef.current = scriptModule

        if (scriptModule.setup && typeof scriptModule.setup === 'function')
          scriptModule.setup(wasmModule)
      })

    return () => {
      const mod = moduleRef.current

      if (mod?.cleanup && typeof mod.cleanup === 'function') {
        mod.cleanup()
      }
    }
  }, [])

  return null
}
