/* md.jsx — minimal, dependency-free markdown → React renderer
   Supports: ## / ### headings, **bold**, *italic*, [links](url),
   `code`, unordered (-) & ordered (1.) lists, > blockquotes, paragraphs. */
(function () {
  const { createElement: h, Fragment } = React;

  // inline: bold, italic, code, links
  function inline(text, keyBase) {
    const nodes = [];
    let rest = text;
    let k = 0;
    // combined regex for the inline tokens
    const re = /(\*\*([^*]+)\*\*)|(\*([^*]+)\*)|(`([^`]+)`)|(\[([^\]]+)\]\(([^)]+)\))/;
    let m;
    while ((m = re.exec(rest))) {
      if (m.index > 0) nodes.push(rest.slice(0, m.index));
      if (m[1]) nodes.push(h("strong", { key: keyBase + "-b" + k++ }, m[2]));
      else if (m[3]) nodes.push(h("em", { key: keyBase + "-i" + k++ }, m[4]));
      else if (m[5]) nodes.push(h("code", { key: keyBase + "-c" + k++, className: "mono", style: { fontSize: ".88em", background: "hsl(var(--muted))", padding: ".1em .35em", borderRadius: ".3em" } }, m[6]));
      else if (m[7]) nodes.push(h("a", { key: keyBase + "-a" + k++, href: m[9], target: "_blank", rel: "noopener noreferrer" }, m[8]));
      rest = rest.slice(m.index + m[0].length);
    }
    if (rest) nodes.push(rest);
    return nodes;
  }

  function MD({ text }) {
    if (!text) return null;
    const lines = text.replace(/\r/g, "").split("\n");
    const blocks = [];
    let i = 0;
    let key = 0;
    while (i < lines.length) {
      let line = lines[i];
      if (!line.trim()) { i++; continue; }

      // headings
      if (/^### /.test(line)) { blocks.push(h("h3", { key: key++ }, inline(line.slice(4), "h3" + key))); i++; continue; }
      if (/^## /.test(line)) { blocks.push(h("h2", { key: key++ }, inline(line.slice(3), "h2" + key))); i++; continue; }
      if (/^# /.test(line)) { blocks.push(h("h2", { key: key++ }, inline(line.slice(2), "h1" + key))); i++; continue; }

      // blockquote
      if (/^> /.test(line)) {
        const buf = [];
        while (i < lines.length && /^> /.test(lines[i])) { buf.push(lines[i].slice(2)); i++; }
        blocks.push(h("blockquote", { key: key++ }, inline(buf.join(" "), "bq" + key)));
        continue;
      }

      // ordered list
      if (/^\d+\.\s/.test(line)) {
        const items = [];
        while (i < lines.length && /^\d+\.\s/.test(lines[i])) {
          items.push(h("li", { key: items.length }, inline(lines[i].replace(/^\d+\.\s/, ""), "ol" + key + items.length)));
          i++;
        }
        blocks.push(h("ol", { key: key++ }, items));
        continue;
      }

      // unordered list
      if (/^[-*]\s/.test(line)) {
        const items = [];
        while (i < lines.length && /^[-*]\s/.test(lines[i])) {
          items.push(h("li", { key: items.length }, inline(lines[i].replace(/^[-*]\s/, ""), "ul" + key + items.length)));
          i++;
        }
        blocks.push(h("ul", { key: key++ }, items));
        continue;
      }

      // paragraph (gather until blank or block start)
      const buf = [line];
      i++;
      while (i < lines.length && lines[i].trim() && !/^(#{1,3}\s|>\s|\d+\.\s|[-*]\s)/.test(lines[i])) { buf.push(lines[i]); i++; }
      blocks.push(h("p", { key: key++ }, inline(buf.join(" "), "p" + key)));
    }
    return h(Fragment, null, blocks);
  }

  Object.assign(window, { MD });
})();
