import { mountInertiaRoot } from './bootstrap'
import React from 'react'
import { createInertiaApp } from '@inertiajs/react'
const pages = {
  ...import.meta.glob([
    './Pages/Help/**/*.jsx',
    '!./Pages/Help/**/__tests__/**',
    '!./Pages/Help/**/*.test.jsx',
  ]),
  ...import.meta.glob([
    './Pages/Profile/**/*.jsx',
    '!./Pages/Profile/**/__tests__/**',
    '!./Pages/Profile/**/*.test.jsx',
  ]),
  ...import.meta.glob([
    './Pages/Collection/**/*.jsx',
    '!./Pages/Collection/**/__tests__/**',
    '!./Pages/Collection/**/*.test.jsx',
  ]),
  ...import.meta.glob([
    './Pages/Group/**/*.jsx',
    '!./Pages/Group/**/__tests__/**',
    '!./Pages/Group/**/*.test.jsx',
  ]),
  ...import.meta.glob([
    './Pages/World/**/*.jsx',
    '!./Pages/World/**/__tests__/**',
    '!./Pages/World/**/*.test.jsx',
  ]),
  ...import.meta.glob([
    './Pages/Academy/**/*.jsx',
    '!./Pages/Academy/**/__tests__/**',
    '!./Pages/Academy/**/*.test.jsx',
  ]),
  ...import.meta.glob([
    './Pages/Enhance/**/*.jsx',
    '!./Pages/Enhance/**/__tests__/**',
    '!./Pages/Enhance/**/*.test.jsx',
  ]),
}

function resolvePage(name) {
  const path = `./Pages/${name}.jsx`
  const page = pages[path]

  if (!page) {
    throw new Error(`Unknown collections page: ${path}`)
  }

  return page().then((module) => module.default)
}

createInertiaApp({
  resolve: resolvePage,
  setup({ el, App, props }) {
    mountInertiaRoot(el, App, props)
  },
})
