import { defineConfig } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'
import { VitePWA } from 'vite-plugin-pwa'
import fs from 'fs'
import path from 'path'

// Read bicorder.json at build time
const bicorderData = JSON.parse(
  fs.readFileSync(path.resolve(__dirname, '../bicorder.json'), 'utf-8')
)

// Read bicorder_model.json at build time
const bicorderModel = JSON.parse(
  fs.readFileSync(path.resolve(__dirname, '../analysis/bicorder_model.json'), 'utf-8')
)

export default defineConfig({
  base: './',
  plugins: [
    svelte(),
    VitePWA({
      registerType: 'autoUpdate',
      includeAssets: ['favicon.ico', 'favicon.svg', 'icon-192.png', 'icon-512.png', 'icon-maskable-192.png', 'icon-maskable-512.png'],
      manifest: {
        name: 'Protocol Bicorder',
        short_name: 'Bicorder',
        description: 'A diagnostic tool for the study of protocols',
        theme_color: '#1a1a2e',
        background_color: '#1a1a2e',
        display: 'standalone',
        icons: [
          {
            src: 'icon-192.png',
            sizes: '192x192',
            type: 'image/png',
            purpose: 'any maskable'
          },
          {
            src: 'icon-512.png',
            sizes: '512x512',
            type: 'image/png',
            purpose: 'any maskable'
          },
          {
            src: 'icon-maskable-192.png',
            sizes: '192x192',
            type: 'image/png',
            purpose: 'maskable'
          },
          {
            src: 'icon-maskable-512.png',
            sizes: '512x512',
            type: 'image/png',
            purpose: 'maskable'
          }
        ]
      },
      workbox: {
        globPatterns: ['**/*.{js,css,html,ico,png,svg,json}']
      }
    })
  ],
  define: {
    '__BICORDER_DATA__': JSON.stringify(bicorderData),
    '__BICORDER_MODEL__': JSON.stringify(bicorderModel)
  }
})
