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

import React, { lazy } from 'react';
import { createRoot } from 'react-dom/client';
import { createInertiaApp } from '@inertiajs/react';
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers';
import store from '../js/Auth/store';
import { Provider } from 'react-redux';

const appName = import.meta.env.VITE_APP_NAME || 'GogoSantri';
const Layout = lazy(() => import('@/Components/Layout'));

createInertiaApp({
    title: (title) => `${title} - ${appName}`,
    resolve: (name) => {
        return resolvePageComponent(`./Pages/${name}.jsx`, import.meta.glob('./Pages/**/**.jsx'))
            .then((page) => {
                page.default.layout = (page) => <LayoutWrapper>{page}</LayoutWrapper>;
                return page;
            });
    },
    setup({ el, App, props }) {
        const root = createRoot(el);

        root.render(
            <Provider store={store}>
                <App {...props} />
            </Provider>
        );
    },

    progress: {
        color: '#4B5563',
    },
});

import { usePage } from '@inertiajs/react';

const LayoutWrapper = ({ children }) => {
    const { url } = usePage();
    const isLoginPage = url.startsWith('/login') || url.startsWith('/auth/login');

    return isLoginPage ? <>{children}</> : <Layout>{children}</Layout>;
};

