import { mountInertiaRoot } from './bootstrap'
import React from 'react'
import { createInertiaApp } from '@inertiajs/react'
import UploadPage from './Pages/Upload/Index'

const staticPages = {
  'Upload/Index': UploadPage,
}

const dynamicPages = Object.fromEntries(
  Object.entries(import.meta.glob('./Pages/Enhance/**/*.jsx')).map(([path, resolver]) => [
    path.replace('./Pages/', '').replace('.jsx', ''),
    resolver,
  ])
)

createInertiaApp({
  resolve: (name) => {
    if (staticPages[name]) {
      return staticPages[name]
    }

    const page = dynamicPages[name]

    if (!page) {
      throw new Error(`Unknown upload page: ${name}`)
    }

    return page().then((module) => module.default)
  },
  setup({ el, App, props }) {
    mountInertiaRoot(el, App, props)
  },
})
