import fs from 'fs'
import vuetify from 'vite-plugin-vuetify'
import type { NuxtI18nOptions } from '@nuxtjs/i18n'

let https = {}

const transpile = ['vuetify', 'pinia', 'pinia-orm', 'date-fns']

if (!process.env.NUXT_ENV) {
  throw new Error('Missing environment file - Run yarn install')
}

if (process.env.NUXT_ENV === 'dev') {
  https = {
    key: fs.readFileSync('env/local.logiciels.opentalent.fr.key'),
    cert: fs.readFileSync('env/local.logiciels.opentalent.fr.crt'),
  }
} else {
  transpile.push('lodash')
}

/**
 * Nuxt configuration
 *
 * @see https://nuxt.com/docs/api/configuration/nuxt-config
 */
export default defineNuxtConfig({
  ssr: true,
  routeRules: {
    // all routes will be generated at build time and cached permanently
    '/**': { prerender: true },
    // these pages will be background revalidated (ISR) at most every 60 seconds
    '/actualites': { isr: 60 },
    '/actualites/**': { isr: 60 },
    '/nous-rejoindre': { isr: 60 },
    '/nous-rejoindre/**': { isr: 60 },
  },
  title: 'Opentalent',
  runtimeConfig: {
    // Private config that is only available on the server
    env: '',
    siteUrl: '',
    apiBaseUrl: '',
    ap2iBaseUrl: '',
    agendaBaseUrl: '',
    fileStorageBaseUrl: '',
    hCaptchaSiteKey: '35360874-ebb1-4748-86e3-9b156d5bfc53',
    // Config within public will be also exposed to the client
    public: {
      env: '',
      siteUrl: '',
      apiBaseUrl: '',
      ap2iBaseUrl: '',
      agendaBaseUrl: '',
      fileStorageBaseUrl: '',
      hCaptchaSiteKey: '35360874-ebb1-4748-86e3-9b156d5bfc53',
    },
  },
  css: ['~/assets/style/main.scss', '~/assets/style/theme.scss'],
  hooks: {
    'builder:watch': console.log,
  },
  app: {
    head: {
      title: 'Opentalent',
      meta: [
        { charset: 'utf-8' },
        { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      ],
      link: [
        { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' },
        {
          rel: 'stylesheet',
          href: 'https://fonts.googleapis.com/css2?family=Barlow:wght@400;500;700&display=swap',
        },
      ],
    },
  },
  typescript: {
    strict: true,
  },
  modules: [
    // eslint-disable-next-line require-await
    async (_, nuxt) => {
      nuxt.hooks.hook('vite:extendConfig', (config) =>
        // @ts-expect-error A revoir après que les lignes aient été décommentées
        (config.plugins ?? []).push(
          vuetify()
          // Remplacer par cela quand l'issue https://github.com/vuetifyjs/vuetify-loader/issues/273 sera règlée..
          // voir aussi : https://github.com/nuxt/nuxt/issues/15412 et https://github.com/vuetifyjs/vuetify-loader/issues/290
          // voir aussi : https://github.com/jrutila/nuxt3-vuetify3-bug
          // vuetify({
          //     styles: { configFile: './assets/css/settings.scss' }
          // })
        )
      )
    },
    [
      '@pinia/nuxt',
      {
        autoImports: [
          // automatically imports `usePinia()`
          'defineStore',
          // automatically imports `usePinia()` as `usePiniaStore()`
          ['defineStore', 'definePiniaStore'],
        ],
      },
    ],
    '@pinia-orm/nuxt',
    'nuxt-lodash',
    '@nuxtjs/i18n',
    '@nuxt/devtools',
    'nuxt3-leaflet',
    '@nuxtjs/google-fonts',
    '@nuxtjs/sitemap',
    'nuxt-gtag',
    'nuxt-vitalizer',
  ],
  router: {
    options: {
      scrollBehaviorType: 'smooth',
    },
  },
  webfontloader: {
    google: {
      families: ['Barlow:300,400,500,700&display=swap'],
    },
  },
  devtools: {
    // @see https://github.com/nuxt/devtools
    enabled: true,
  },
  vite: {
    esbuild: {
      drop: process.env.DEBUG ? [] : ['console', 'debugger'],
      tsconfigRaw: {
        compilerOptions: {
          experimentalDecorators: true,
        },
      },
    },
    ssr: {
      noExternal: ['vuetify'],
    },
    server: {
      https,
      // @ts-ignore
      port: 443,
      hmr: {
        protocol: 'wss',
        port: 24680,
      },
    },
  },
  vuetify: {
    styles: { configFile: 'src/vuetify.scss' },
  },
  i18n: {
    langDir: 'lang',
    lazy: true,
    locales: [
      {
        code: 'fr',
        iso: 'fr-FR',
        file: 'fr.json',
        name: 'Français',
      },
    ],
    defaultLocale: 'fr',
    detectBrowserLanguage: false,
  } as NuxtI18nOptions,
  build: {
    transpile,
  },
  googleFonts: {
    families: {
      Barlow: true,
    },
    display: 'block',
  },
  gtag: {
    id: 'G-L8PZ9TEFNX',
    enabled: false,
  },
})
