import { router } from '@inertiajs/vue3';
import { Notify } from 'quasar';
import type { FlashToast } from '@/types/ui';

const notifyTypeMap: Record<FlashToast['type'], 'positive' | 'info' | 'warning' | 'negative'> = {
    success: 'positive',
    info: 'info',
    warning: 'warning',
    error: 'negative',
};

export function initializeFlashToast(): void {
    router.on('flash', (event) => {
        const flash = (event as CustomEvent).detail?.flash;
        const data = flash?.toast as FlashToast | undefined;

        if (!data) {
            return;
        }

        Notify.create({
            type: notifyTypeMap[data.type],
            message: data.message,
            position: 'bottom',
        });
    });
}
