import {jwennAwtisSlug, jwennTeksSlug} from '../lib/oki-api'

const url = process.env.NEXT_PUBLIC_SITE_URL || 'http://localhost'

const createSitemap = (teksSlug, awtisSlug) => (
  `<?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
      <url>
        <loc>${url}</loc>
        <priority>1</priority>
      </url>
      <url>
        <loc>${url}/paroles</loc>
        <priority>0.9</priority>
      </url>
      <url>
        <loc>${url}/soutyen</loc>
        <priority>0.8</priority>
      </url>
      <url>
        <loc>${url}/awtis</loc>
        <priority>0.6</priority>
      </url>
      <url>
        <loc>${url}/soumet</loc>
        <priority>0.5</priority>
      </url>
        ${teksSlug
    .map(slug => `
                    <url>
                        <loc>${`${url}/paroles/${slug}`}</loc>
                    </url>
                `)
    .join('')}
    ${awtisSlug
    .map(slug => `
                      <url>
                          <loc>${`${url}/awtis/${slug}`}</loc>
                      </url>
                  `)
    .join('')}
    </urlset>
    `
)

export default function Sitemap() {
  return null
}

export async function getServerSideProps({res}) {
  const teksSlug = await jwennTeksSlug()
  const awtisSlug = await jwennAwtisSlug()

  res.setHeader('Content-Type', 'text/xml')
  res.write(createSitemap(teksSlug, awtisSlug))
  res.end()

  return {
    props: {}
  }
}
