"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const index = require("../../_virtual/index5.cjs");
const require$$0 = require("react/jsx-runtime");
const React = require("react");
var hasRequiredDist;
function requireDist() {
  if (hasRequiredDist) return index.__exports;
  hasRequiredDist = 1;
  var __assign = index.__exports && index.__exports.__assign || function() {
    __assign = Object.assign || function(t) {
      for (var s, i = 1, n = arguments.length; i < n; i++) {
        s = arguments[i];
        for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
          t[p] = s[p];
      }
      return t;
    };
    return __assign.apply(this, arguments);
  };
  var __rest = index.__exports && index.__exports.__rest || function(s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
      t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
      for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
        if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
          t[p[i]] = s[p[i]];
      }
    return t;
  };
  Object.defineProperty(index.__exports, "__esModule", { value: true });
  index.__exports.default = hardenReactMarkdown;
  var jsx_runtime_1 = require$$0;
  var react_1 = React;
  function hardenReactMarkdown(MarkdownComponent) {
    return function HardenedReactMarkdown(props) {
      var _a = props.defaultOrigin, defaultOrigin = _a === void 0 ? "" : _a, _b = props.allowedLinkPrefixes, allowedLinkPrefixes = _b === void 0 ? [] : _b, _c = props.allowedImagePrefixes, allowedImagePrefixes = _c === void 0 ? [] : _c, userComponents = props.components, reactMarkdownProps = __rest(props, ["defaultOrigin", "allowedLinkPrefixes", "allowedImagePrefixes", "components"]);
      var hasSpecificLinkPrefixes = allowedLinkPrefixes.length && !allowedLinkPrefixes.every(function(p) {
        return p === "*";
      });
      var hasSpecificImagePrefixes = allowedImagePrefixes.length && !allowedImagePrefixes.every(function(p) {
        return p === "*";
      });
      if (!defaultOrigin && (hasSpecificLinkPrefixes || hasSpecificImagePrefixes)) {
        throw new Error("defaultOrigin is required when allowedLinkPrefixes or allowedImagePrefixes are provided");
      }
      var parseUrl = function(url) {
        if (typeof url !== "string")
          return null;
        try {
          var urlObject = new URL(url);
          return urlObject;
        } catch (error) {
          if (defaultOrigin) {
            try {
              var urlObject = new URL(url, defaultOrigin);
              return urlObject;
            } catch (error2) {
              return null;
            }
          }
          return null;
        }
      };
      var isPathRelativeUrl = function(url) {
        if (typeof url !== "string")
          return false;
        return url.startsWith("/");
      };
      var transformUrl = function(url, allowedPrefixes) {
        if (!url)
          return null;
        var parsedUrl = parseUrl(url);
        if (!parsedUrl)
          return null;
        if (allowedPrefixes.includes("*")) {
          var inputWasRelative_1 = isPathRelativeUrl(url);
          var urlString_1 = parseUrl(url);
          if (urlString_1) {
            if (inputWasRelative_1) {
              return urlString_1.pathname + urlString_1.search + urlString_1.hash;
            }
            return urlString_1.href;
          }
          return null;
        }
        var inputWasRelative = isPathRelativeUrl(url);
        var urlString = parseUrl(url);
        if (urlString && allowedPrefixes.some(function(prefix) {
          var parsedPrefix = parseUrl(prefix);
          if (!parsedPrefix) {
            return false;
          }
          if (parsedPrefix.origin !== urlString.origin) {
            return false;
          }
          return urlString.href.startsWith(parsedPrefix.href);
        })) {
          if (inputWasRelative) {
            return urlString.pathname + urlString.search + urlString.hash;
          }
          return urlString.href;
        }
        return null;
      };
      var hardenedComponents = {
        a: function(_a2) {
          var href = _a2.href, children = _a2.children, node = _a2.node, props2 = __rest(_a2, ["href", "children", "node"]);
          var transformedUrl = transformUrl(href, allowedLinkPrefixes);
          if (transformedUrl !== null) {
            if (userComponents === null || userComponents === void 0 ? void 0 : userComponents.a) {
              return (0, react_1.createElement)(userComponents.a, __assign({ href: transformedUrl, children, node, target: "_blank", rel: "noopener noreferrer" }, props2));
            }
            return (0, jsx_runtime_1.jsx)("a", __assign({ href: transformedUrl }, props2, { target: "_blank", rel: "noopener noreferrer", children }));
          }
          return (0, jsx_runtime_1.jsxs)("span", { className: "text-gray-500", title: "Blocked URL: ".concat(href), children: [children, " [blocked]"] });
        },
        img: function(_a2) {
          var src = _a2.src, alt = _a2.alt, node = _a2.node, props2 = __rest(_a2, ["src", "alt", "node"]);
          var transformedUrl = transformUrl(src, allowedImagePrefixes);
          if (transformedUrl !== null) {
            if (userComponents === null || userComponents === void 0 ? void 0 : userComponents.img) {
              return (0, react_1.createElement)(userComponents.img, __assign({ src: transformedUrl, alt, node }, props2));
            }
            return (0, jsx_runtime_1.jsx)("img", __assign({ src: transformedUrl, alt }, props2));
          }
          return (0, jsx_runtime_1.jsxs)("span", { className: "inline-block bg-gray-200 dark:bg-gray-700 text-gray-600 dark:text-gray-400 px-3 py-1 rounded text-sm", children: ["[Image blocked: ", alt || "No description", "]"] });
        }
      };
      var mergedComponents = __assign(__assign({}, userComponents), hardenedComponents);
      var componentProps = __assign(__assign({}, reactMarkdownProps), { components: mergedComponents });
      return (0, react_1.createElement)(MarkdownComponent, componentProps);
    };
  }
  return index.__exports;
}
exports.__require = requireDist;
//# sourceMappingURL=dist-index.cjs.map
