const fs = require('fs')
const addPage = (page) => {
  const path = page
    .replace('src/pages/', '')
    .replace('.js', '')
    .replace('.mdx', '')
    .replace('.jsx', '')
    .replace('.ts', '')
    .replace('.tsx', '')
    .replace('index', '')
    .replace('indexx',"")
    .replace('x',"")
    .replace('/x',"")
  // Check if the path is '/index' and replace it with an empty string
  const route = path === '/index' ? '' : path;
  const cleanRoute = route.endsWith('/') ? route.slice(0, -1) : route;
  return `  <url>
    <loc>${`${process.env.NEXT_PUBLIC_APP_WEB_URL}/${cleanRoute}`}</loc>
    <lastmod>${new Date().toISOString()}</lastmod>
    <changefreq>monthly</changefreq>
    <priority>1.0</priority>
  </url>`
}

 const generateSitemap = async () => {
  const { globby } = await import('globby');

  // excludes Nextjs files and API routes.
  const pages = await globby([
    'src/pages/**/*{.js,.jsx,.ts,.tsx,.mdx}',
    '!src/pages/_*.js',
    '!src/pages/api',
    '!src/pages/_*.jsx'
  ])
  const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  ${pages.map(addPage).join('\n')}
  </urlset>`
  fs.writeFileSync('public/sitemap.xml', sitemap)
}
generateSitemap()
