import { defineNuxtConfig } from 'nuxt/config'
import { i18nPages } from './i18n/i18n-pages'

// Hilfsfunktion, um Objekt mit locales auf reine Strings zu mappen
function flattenPages(pagesObj: Record<string, Record<string, string>>) {
  const result: Record<string, string> = {}
  for (const pageKey in pagesObj) {
    for (const locale in pagesObj[pageKey]) {
      // z.B. 'index.de' = '/'
      result[`${pageKey}.${locale}`] = pagesObj[pageKey][locale]
    }
  }
  return result
}

export default defineNuxtConfig({
  app: {
    head: {
      title: 'digimedialoop', 
      htmlAttrs: {
        lang: 'de',
      },
      link: [
        { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' },
      ],
      charset: 'utf-16',
      viewport: 'width=device-width, initial-scale=1, maximum-scale=5',
    }
  },
  compatibilityDate: '2024-11-01',
  devtools: { enabled: true },
  vite: {
    css: {
      preprocessorOptions: {
        sass: {
          additionalData: `
            @use "~/assets/styles/bootstrap.sass" as *\n
            @use "~/assets/styles/main.sass" as *\n
          `
        }
      }
    }
  },
  modules: [
    '@nuxt/image',
    '@nuxt/eslint',
    '@pinia/nuxt',
    '@nuxtjs/i18n',
    ['@pinia/nuxt', {
      autoImports: [
        'defineStore',
        'storeToRefs',
        ['defineStore', 'definePiniaStore']
      ]
    }],
  ],
  image: {
    debug: true,
    strapi: {
      baseURL: 'https://strapi.digimedialoop.de'
    },
    domains: ['strapi.digimedialoop.de'],
    format: ['webp'],
    quality: 80
  },
  runtimeConfig: {
    public: {
      appUrl: process.env.APP_URL,
      cmsBaseUrl: process.env.CMS_URL, 
      cmsToken: process.env.CMS_TOKEN
    }
  },
  components: [
    { path: '~/components', pathPrefix: false },
    { path: '~/components/template', pathPrefix: false }
  ],
  i18n: {
    defaultLocale: 'de',
    strategy: 'prefix_except_default',
    locales: [
      { code: 'de', name: 'Deutsch', file: 'de.json' },
      { code: 'en', name: 'English', file: 'en.json' },
      { code: 'es', name: 'Español', file: 'es.json' },
      { code: 'fr', name: 'Français', file: 'fr.json' },
      { code: 'it', name: 'Italiano', file: 'it.json' },
      { code: 'tr', name: 'Türkçe', file: 'tr.json' }
    ],
    customRoutes: 'config',
    pages: flattenPages(i18nPages),    
    bundle: {
      optimizeTranslationDirective: false
    }
  },
  pinia: {
    autoImports: [
      'defineStore',
      ['defineStore', 'definePiniaStore'],
      'storeToRefs'
    ]
  },
  nitro: {
    prerender: {
      crawlLinks: true,
      failOnError: false,
      //routes: ['/', '/webagency'] // Wichtige Routen vorrendern , '/impressum', '/datenschutz'
    }
  }
})
