import { defineConfig } from "astro/config";
import { filterSitemapByDefaultLocale, i18n } from "astro-i18n-aut/integration";
import sitemap from "@astrojs/sitemap";
import Icons from "unplugin-icons/vite";
import mdx from "@astrojs/mdx";
import glsl from "vite-plugin-glsl";

import svelte from "@astrojs/svelte";
import UnoCSS from "unocss/astro";

const defaultLocale = "de";
const locales = {
  en: "en",
  de: "de",
};

const DEFAULT_LAYOUT = "@layouts/Post.astro";

function setDefaultLayout() {
  return function (_, file) {
    const { frontmatter } = file.data.astro;
    if (!frontmatter.layout) frontmatter.layout = DEFAULT_LAYOUT;
  };
}

export default defineConfig({
  site: "https://max-richter.dev",
  trailingSlash: "never",
  prefetch: true,
  image: {
    remotePatterns: [{ protocol: "https" }],
  },
  build: {
    format: "file",
  },
  vite: {
    plugins: [
      glsl(),
      Icons({
        compiler: "svelte",
      }),
    ],
    server: {
      watch: {
        ignored: ["**/node_modules/**", "**/dist/**", "**/.git/**"],
      },
    },
  },
  markdown: {
    remarkPlugins: [setDefaultLayout],
  },
  integrations: [
    i18n({
      exclude: ["pages/**/*.json.ts", "pages/api/**/*"],
      locales,
      defaultLocale,
    }),
    mdx(),
    svelte(),
    UnoCSS({
      injectReset: true,
    }),
    sitemap({
      i18n: {
        locales,
        defaultLocale,
      },
      filter: filterSitemapByDefaultLocale({ defaultLocale }),
    }),
  ],
});
