import xml2js from "xml2js";

async function getData(id) {
  const res = await fetch(
    `https://storage.hamyanedalat.com/site-maps/site-map.xml`
  );
  const xml = await res.text();

  return xml;
}

async function parseXml(xml) {
  const parser = new xml2js.Parser();
  const result = await parser.parseStringPromise(xml);
  return result;
}

export default async function Sitemap() {
  const xml = await getData();
  const parsedXml = await parseXml(xml);

  // Construct your sitemap using the parsed XML data
  const urls = parsedXml.sitemapindex.sitemap.map((entry) => ({
    url: entry.loc[0],
    lastModified: entry.lastmod[0],
  }));

  return urls;
}
