import { getServerSideSitemapLegacy } from "next-sitemap";
import { GetServerSideProps } from "next";
import { customerApi } from "@/api/api";

export const getServerSideProps = async (ctx) => {
  // Method to source urls from cms
  const res = await customerApi.get("/products?limit=0&page=1&field=slug");
  const fields = [];
  if (
    res.data &&
    res.data.success === true &&
    res.data.result &&
    res.data.result.data &&
    res.data.result.data.length > 0
  ) {
    for (const item of res.data.result.data) {
      fields.push({
        loc: process.env.NEXT_PUBLIC_WEBSITE + "/" + item.slug,
        lastmod: new Date().toISOString(),
        changefreq: "daily",
        priority: "1",
      });
    }
  }

  return getServerSideSitemapLegacy(ctx, fields);
};

// Default export to prevent next.js errors
export default function Sitemap() {}
