import { defineConfig } from 'vitepress'
import { SitemapStream } from 'sitemap'
import { createWriteStream } from 'node:fs'
import { resolve } from 'node:path'


// https://vitepress.dev/reference/site-config
export default defineConfig({
  title: "dexorder",
  // this description is put into the meta tag for SEO
  description: "Power up Uniswap with limit orders, stoplosses, DCA's, breakout orders, and more. 100% noncustodial and EVM-native.",
  head: [
      [ 'meta', { name: 'viewport', content: 'width=device-width, initial-scale=1'}],
      [ 'link', { rel: 'icon', href: '/favicon.ico' }],
      [ 'link', { rel: 'canonical', href: 'https://dexorder.com/' }],
      [ 'link', { rel: 'preconnect', href: 'https://fonts.googleapis.com' }],
      [ 'link', { rel: 'preconnect', href: 'https://fonts.gstatic.com', crossorigin: '' }],
      [ 'link', { rel: 'stylesheet', href: 'https://fonts.googleapis.com/css2?family=Manrope:wght@200..800&family=Victor+Mono:ital,wght@0,100..700;1,100..700&display=swap' }],

      /*
      <!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-L6F3Z6SBC7"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-L6F3Z6SBC7');
</script>
       */
    [
      'script',
      { async: '', src: 'https://www.googletagmanager.com/gtag/js?id=G-L6F3Z6SBC7' }
    ],
    [
      'script',
      {},
      `window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
      gtag('config', 'G-L6F3Z6SBC7');`
    ]

  ],
  transformHead({assets}) {
    // put the path of local assets into the <head>
    let fontFile = assets.find(file => /forgotten_futurist\.\w+\.ttf/.test(file))
    // adjust the regex accordingly for new fonts
    // fontFile ||= assets.find(file => /forgotten_futurist\.\w+\.woff2/.test(file))
    if (fontFile) {
      return [
        [
          'link',
          {
            rel: 'preload',
            href: fontFile,
            as: 'font',
            type: 'font/ttf',
            crossorigin: ''
          }
        ]
      ]
    }
  },
  appearance: 'force-dark',
  themeConfig: {
    // https://vitepress.dev/reference/default-theme-config
    logo: {
      light: '/dexorder_full_lightmode.svg',
      dark: '/dexorder_full_darkmode.svg',
    },
    siteTitle: ' ',
    nav: [
      { text: 'Home', link: '/' },
      { text: 'Docs', link: '/introduction' },
      { text: 'App', link: 'https://app.dexorder.com/', target: 'dexorderapp', props: {} }
    ],

    sidebar: [
      {
        text: 'About',
        items: [
          {text: 'Introduction', link: '/introduction',},
          {text: 'Features', link: '/features',},
          {text: 'FAQ', link: '/faq',},
          {text: 'Fees', link: '/fees',},
        ]
      },
      {
        text: 'Docs',
        items: [
          {text: 'Liquidity', link: '/liquidity',},
          {text: 'Slippage', link: '/slippage',},
        ]
      },
      {
        text: 'Legal',
        items: [
          {text: 'Terms of Service', link: '/terms-of-service',},
          {text: 'Privacy Policy', link: '/privacy-policy',},
          {text: 'Execution Policy', link: '/execution-policy',},
        ]
      },
    ],

    socialLinks: [
      { icon: 'twitter', link: 'https://twitter.com/Dexorder_trade' },
      { icon: 'discord', link: 'https://discord.gg/fqp9JXXQyt' },
      { icon: 'github', link: 'https://github.com/dexorder-trade/contract' },
    ],

    footer: {
      copyright: `© ${new Date().getFullYear()} Dexorder Trading Services Ltd. (BVI)`,
    },

  },
  // FFS I cannot stop markdown-it from rendering (c) as ©
  markdown: {
    config: (md) => {
      md.disable(['replacements'], false)
    },
  },

  // Sitemap generation
  transformHtml: (_, id, { pageData }) => {
    if (!/[\\/]404\.html$/.test(id))
      links.push({
        // you might need to change this if not using clean urls mode
        url: pageData.relativePath.replace(/((^|\/)index)?\.md$/, '$2'),
        lastmod: pageData.lastUpdated
      })
  },
  buildEnd: async ({ outDir }) => {
    const sitemap = new SitemapStream({
      hostname: 'https://dexorder.com/'
    })
    const writeStream = createWriteStream(resolve(outDir, 'sitemap.xml'))
    sitemap.pipe(writeStream)
    links.forEach((link) => sitemap.write(link))
    sitemap.end()
    await new Promise((r) => writeStream.on('finish', r))
  },
})

const links = []
