import { defineConfig } from 'vite';
import { resolve } from 'path';
import {VitePWA} from "vite-plugin-pwa";
import * as fs from "node:fs";
// Optional: TypeScript support ist ohne extra Plugin enthalten!
// Bei Bedarf: npm install --save-dev typescript

export default defineConfig({
    build: {
        outDir: 'dist',         // Output bleibt im public-Verzeichnis
        manifest: true,
        emptyOutDir: true,
        rollupOptions: {
            input: {
                css: resolve(__dirname, 'resources/css/styles.css'),
                js: resolve(__dirname, 'resources/js/main.js')
                // Falls TypeScript:
                // ts: 'resources/ts/app.ts'
            },
            plugins: []
        }
    },
    plugins: [
        VitePWA({
            filename: 'sw.js',
            registerType: 'autoUpdate',
            includeAssets: [
                'favicon.svg',
                'offline.html',
                'icons/icon-192x192.png',
                'icons/icon-512x512.png'
            ],
            manifest: {
                name: 'Meine Website',
                short_name: 'Website',
                start_url: '/',
                display: 'standalone',
                background_color: '#ffffff',
                theme_color: '#000000',
                icons: [
                    {
                        src: 'icons/icon-192x192.png',
                        sizes: '192x192',
                        type: 'image/png'
                    },
                    {
                        src: 'icons/icon-512x512.png',
                        sizes: '512x512',
                        type: 'image/png'
                    }
                ]
            },
            workbox: {
                globPatterns: ['**/*.{js,css,html,ico,png,svg}'],
                runtimeCaching: [
                    {
                        urlPattern: ({ request }) => request.destination === 'document',
                        handler: 'NetworkFirst',
                        options: {
                            cacheName: 'html-cache'
                        }
                    },
                    {
                        urlPattern: ({ request }) =>
                            request.destination === 'script' || request.destination === 'style',
                        handler: 'StaleWhileRevalidate',
                        options: {
                            cacheName: 'asset-cache'
                        }
                    },
                    {
                        urlPattern: ({ request }) => request.destination === 'image',
                        handler: 'CacheFirst',
                        options: {
                            cacheName: 'image-cache',
                            expiration: {
                                maxEntries: 50,
                                maxAgeSeconds: 60 * 60 * 24 * 30
                            }
                        }
                    }
                ],
                navigateFallback: '/offline.html'
            }
        })
    ],
    server: {
        https: {
            key: fs.readFileSync('./ssl/fullchain.pem'),
            cert: fs.readFileSync('./ssl/privkey.pem')
        },
        host: 'localhost',
    //port: 3000
    }
});
