import {jwennSlugs} from '../lib/oki-api'

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

const createSitemap = teks => (
  `<?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}/teks</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>
        ${teks
    .map(m => `
                    <url>
                        <loc>${`${url}/teks/${m}`}</loc>
                    </url>
                `)
    .join('')}
    </urlset>
    `
)

export default function Sitemap() {
  return null
}

export async function getServerSideProps({res}) {
  const request = await jwennSlugs()

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

  return {
    props: {}
  }
}
