import { createInertiaApp } from '@inertiajs/vue3';
import { createApp, h } from 'vue';
import 'vue-sonner/style.css';
import { Toaster } from '@/components/ui/sonner';
import { useFlashToast } from '@/composables/useFlashToast';
import { restoreConnection } from '@/lib/thermal-printer/stable-transport';

if ('serviceWorker' in navigator) {
    navigator.serviceWorker
        .register('/sw.js', { scope: '/' })
        .then((reg) => {
            console.log('[SW] Registered:', reg.scope);
        })
        .catch((err) => {
            console.error('[SW] Registration failed:', err);
        });
}

void restoreConnection();

const appName = import.meta.env.VITE_APP_NAME || 'Laravel';

createInertiaApp({
    title: (title) => (title ? `${title} - ${appName}` : appName),
    progress: {
        color: '#4B5563',
    },
    setup({ el, App, props, plugin }) {
        if (!el) {
            return;
        }

        const app = createApp({
            setup() {
                useFlashToast();

                return () => [h(App, props), h(Toaster)];
            },
        });

        app.use(plugin);
        app.mount(el);
    },
});
