import xmlbuilder from 'xmlbuilder';
import * as CommonApi from '../pages/api/common';
import { baseUrl } from '../constants/urls';

export async function categoriesSitemapXml() {
  const baseUrl1 = 'https://indispare.com';

  // Use async/await to fetch data from the API
  try {
    const response = await CommonApi.getRequest(`${baseUrl}/api/generate-sitemap?type=categories`);
    const staticData = response?.data?.data;

  
    const root = xmlbuilder.create('urlset', { version: '1.0', encoding: 'UTF-8' })
      .att('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');

      staticData.forEach((data:any) => {
      root.ele('url')
        .ele('loc', `${data}`).up() // Replace 'url' with the actual field containing the product URL
        .ele('lastmod', new Date().toISOString()).up()
        .ele('changefreq', 'monthly').up()
        .ele('priority', '0.8').up();
    });

    return root.end({ pretty: true });
  } catch (error) {
    // Handle API request error
    console.error('Error fetching static data:', error);
    return null;
  }
}
