import { q as query } from '../../../chunks/db_Dsd4uyc_.mjs';
export { renderers } from '../../../renderers.mjs';

const GET = async () => {
  try {
    const result = await query(
      `SELECT
         p.id, p.name, p.slug, p.brand, p.ean, p.gtin,
         p.brand_normalized, p.model_normalized,
         p.cutout_image_url,
         o.price, o.original_price, o.currency, o.product_url, o.image_url, o.in_stock,
         p.colorway_normalized as color
       FROM product_offers o
       JOIN products p ON o.product_id = p.id
       WHERE o.in_stock = true
         AND p.is_sneaker = true
         AND o.image_url IS NOT NULL AND o.image_url != ''
         AND o.price > 0
       ORDER BY o.price ASC`
    );
    const seen = /* @__PURE__ */ new Set();
    const items = [];
    for (const row of result.rows) {
      if (seen.has(row.id)) continue;
      seen.add(row.id);
      const primaryImage = row.image_url?.startsWith("http") ? row.image_url : `https://sneakerpicks.nl/api/img/${row.id}`;
      const additionalImage = row.cutout_image_url ? `https://sneakerpicks.nl/api/img/${row.id}` : null;
      const productLink = `https://sneakerpicks.nl/sneakers/${row.slug || row.id}`;
      const currency = row.currency || "EUR";
      const hasDiscount = row.original_price && Number(row.original_price) > Number(row.price);
      const description = `${row.brand} ${row.name}${row.color ? " - " + row.color : ""}. Vergelijk prijzen bij meerdere shops op SneakerPicks.nl.`;
      items.push(`    <item>
      <g:id>${escXml(String(row.id))}</g:id>
      <title>${escXml(`${row.brand} ${row.name}`)}</title>
      <description>${escXml(description)}</description>
      <link>${escXml(productLink)}</link>
      <g:image_link>${escXml(primaryImage)}</g:image_link>${additionalImage && additionalImage !== primaryImage ? `
      <g:additional_image_link>${escXml(additionalImage)}</g:additional_image_link>` : ""}
      <g:price>${hasDiscount ? row.original_price : row.price} ${currency}</g:price>
      ${hasDiscount ? `<g:sale_price>${row.price} ${currency}</g:sale_price>` : ""}
      <g:brand>${escXml(row.brand || "")}</g:brand>
      ${row.ean ? `<g:gtin>${escXml(row.ean)}</g:gtin>` : ""}
      <g:condition>new</g:condition>
      <g:availability>${row.in_stock ? "in_stock" : "out_of_stock"}</g:availability>
      <g:google_product_category>Apparel &amp; Accessories &gt; Shoes &gt; Athletic Shoes</g:google_product_category>
      <g:product_type>Apparel &amp; Accessories &gt; Shoes</g:product_type>
      ${row.color ? `<g:color>${escXml(row.color)}</g:color>` : ""}
      <g:shipping>
        <g:country>NL</g:country>
        <g:service>Standard</g:service>
        <g:price>0.00 EUR</g:price>
      </g:shipping>
    </item>`);
    }
    const xml = `<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">
  <channel>
    <title>SneakerPicks.nl - Sneaker Price Comparison</title>
    <link>https://sneakerpicks.nl</link>
    <description>Best sneaker prices from Dutch retailers</description>
${items.join("\n")}
  </channel>
</rss>`;
    return new Response(xml, {
      status: 200,
      headers: {
        "Content-Type": "application/xml; charset=utf-8",
        "Cache-Control": "public, max-age=3600"
      }
    });
  } catch (err) {
    return new Response(`<!-- Error: ${err.message} -->`, {
      status: 500,
      headers: { "Content-Type": "application/xml" }
    });
  }
};
function escXml(s) {
  return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
}

const _page = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty({
  __proto__: null,
  GET
}, Symbol.toStringTag, { value: 'Module' }));

const page = () => _page;

export { page };
