import { VitePWA } from 'vite-plugin-pwa'

export function createPwaPlugin() {
  return VitePWA({
    registerType: 'autoUpdate',
    injectRegister: null,
    manifestFilename: 'site.webmanifest',
    includeAssets: [
      'favicon.ico',
      'favicon.svg',
      'favicon.png',
      'apple-touch-icon.png',
      'icon-192.png',
      'icon-512.png',
    ],
    manifest: {
      id: '/',
      name: '道德经 · 传统文化典籍',
      short_name: '道德经',
      description: '传统文化典籍资料库',
      lang: 'zh-CN',
      theme_color: '#0f3460',
      background_color: '#1a1a2e',
      display: 'standalone',
      orientation: 'portrait-primary',
      scope: '/',
      start_url: '/',
      categories: ['books', 'education'],
      icons: [
        {
          src: '/apple-touch-icon.png',
          sizes: '180x180',
          type: 'image/png',
          purpose: 'any',
        },
        {
          src: '/icon-192.png',
          sizes: '192x192',
          type: 'image/png',
          purpose: 'any',
        },
        {
          src: '/icon-512.png',
          sizes: '512x512',
          type: 'image/png',
          purpose: 'any',
        },
        {
          src: '/icon-512.png',
          sizes: '512x512',
          type: 'image/png',
          purpose: 'maskable',
        },
      ],
    },
    workbox: {
      globPatterns: [
        'assets/**/*.{js,css,woff2}',
        'favicon.ico',
        'favicon.svg',
        'favicon.png',
        'icon-192.png',
        'icon-512.png',
        'apple-touch-icon.png',
        'vp-icons.css',
        'site.webmanifest',
      ],
      globIgnores: ['**/*.html', 'images/**', 'assets/**/*.png', 'assets/**/*.jpg', 'assets/**/*.webp'],
      navigateFallback: null,
      cleanupOutdatedCaches: true,
    },
    devOptions: {
      enabled: false,
    },
  })
}
