import '../css/app.css';
import './bootstrap';

import { createInertiaApp } from '@inertiajs/react';
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers';
import { createRoot } from 'react-dom/client';
import { RouterProgress } from './Components/RouterProgress';
import { ToastProvider } from './Contexts/ToastContext';

const appName = import.meta.env.VITE_APP_NAME || 'biiproject kit v2';

createInertiaApp({
    title: (title) => {
        // Read appName from global window prop if injected, or default
        const pageProps = (window as any).document.getElementById('app')?.dataset.page;
        const appName = pageProps ? JSON.parse(pageProps).props?.system_settings?.app_name : 'biiproject kit v2';
        return `${title ? title + ' — ' : ''}${appName || 'biiproject kit v2'}`;
    },
    resolve: (name) =>
        resolvePageComponent(
            `./Pages/${name}.tsx`,
            import.meta.glob('./Pages/**/*.tsx'),
        ),
    setup({ el, App, props }) {
        const root = createRoot(el);
        root.render(
            <ToastProvider>
                <RouterProgress />
                <App {...props} />
            </ToastProvider>
        );
    },
    progress: false,
});
