var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
import parser from "fast-xml-parser";
import {canonicalURL} from "../util.js";
function validateRSS(args) {
  const {rssData, srcFile} = args;
  if (!rssData.title)
    throw new Error(`[${srcFile}] rss.title required`);
  if (!rssData.description)
    throw new Error(`[${srcFile}] rss.description required`);
  if (rssData.item)
    throw new Error(`[${srcFile}] \`item: Function\` should be \`items: Item[]\``);
  if (!Array.isArray(rssData.items))
    throw new Error(`[${srcFile}] rss.items should be an array of items`);
}
function generateRSS(args) {
  validateRSS(args);
  const {srcFile, feedURL, rssData, site} = args;
  if (rssData.item)
    throw new Error(`[${srcFile}] rss() \`item()\` function was deprecated, and is now \`items: object[]\`.`);
  let xml = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"`;
  if (rssData.xmlns) {
    for (const [k, v] of Object.entries(rssData.xmlns)) {
      xml += ` xmlns:${k}="${v}"`;
    }
  }
  xml += `>`;
  xml += `<channel>`;
  xml += `<title><![CDATA[${rssData.title}]]></title>`;
  xml += `<description><![CDATA[${rssData.description}]]></description>`;
  xml += `<link>${canonicalURL(feedURL, site).href}</link>`;
  if (typeof rssData.customData === "string")
    xml += rssData.customData;
  for (const result of rssData.items) {
    xml += `<item>`;
    if (typeof result !== "object")
      throw new Error(`[${srcFile}] rss.items expected an object. got: "${JSON.stringify(result)}"`);
    if (!result.title)
      throw new Error(`[${srcFile}] rss.items required "title" property is missing. got: "${JSON.stringify(result)}"`);
    if (!result.link)
      throw new Error(`[${srcFile}] rss.items required "link" property is missing. got: "${JSON.stringify(result)}"`);
    xml += `<title><![CDATA[${result.title}]]></title>`;
    xml += `<link>${canonicalURL(result.link, site).href}</link>`;
    if (result.description)
      xml += `<description><![CDATA[${result.description}]]></description>`;
    if (result.pubDate) {
      if (typeof result.pubDate === "number" || typeof result.pubDate === "string") {
        result.pubDate = new Date(result.pubDate);
      } else if (result.pubDate instanceof Date === false) {
        throw new Error("[${filename}] rss.item().pubDate must be a Date");
      }
      xml += `<pubDate>${result.pubDate.toUTCString()}</pubDate>`;
    }
    if (typeof result.customData === "string")
      xml += result.customData;
    xml += `</item>`;
  }
  xml += `</channel></rss>`;
  const isValid = parser.validate(xml);
  if (isValid !== true) {
    throw new Error(isValid);
  }
  return xml;
}
function generateRssFunction(site, route) {
  let result = {};
  return {
    generator: function rssUtility(args) {
      if (!site) {
        throw new Error(`[${route.component}] rss() tried to generate RSS but "buildOptions.site" missing in astro.config.mjs`);
      }
      const _a = args, {dest} = _a, rssData = __objRest(_a, ["dest"]);
      const feedURL = dest || "/feed.xml";
      result.url = feedURL;
      result.xml = generateRSS({rssData, site, srcFile: route.component, feedURL});
    },
    rss: result
  };
}
export {
  generateRSS,
  generateRssFunction,
  validateRSS
};
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vLi4vLi4vc3JjL2NvcmUvc3NyL3Jzcy50cyJdLAogICJtYXBwaW5ncyI6ICI7Ozs7Ozs7Ozs7Ozs7OztBQUVBO0FBQ0E7QUFHTyxxQkFBcUIsTUFBNkI7QUFDdkQsUUFBTSxDQUFFLFNBQVMsV0FBWTtBQUM3QixNQUFJLENBQUMsUUFBUTtBQUFPLFVBQU0sSUFBSSxNQUFNLElBQUk7QUFDeEMsTUFBSSxDQUFDLFFBQVE7QUFBYSxVQUFNLElBQUksTUFBTSxJQUFJO0FBQzlDLE1BQUssUUFBZ0I7QUFBTSxVQUFNLElBQUksTUFBTSxJQUFJO0FBQy9DLE1BQUksQ0FBQyxN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