import { assign as b, escapeHtml as u, unescapeAll as m } from "./common/utils.js";
const o = {};
o.code_inline = function(n, r, i, l, t) {
  const e = n[r];
  return "<code" + t.renderAttrs(e) + ">" + u(e.content) + "</code>";
};
o.code_block = function(n, r, i, l, t) {
  const e = n[r];
  return "<pre" + t.renderAttrs(e) + "><code>" + u(n[r].content) + `</code></pre>
`;
};
o.fence = function(n, r, i, l, t) {
  const e = n[r], s = e.info ? m(e.info).trim() : "";
  let c = "", g = "";
  if (s) {
    const f = s.split(/(\s+)/g);
    c = f[0], g = f.slice(2).join("");
  }
  let a;
  if (i.highlight ? a = i.highlight(e.content, c, g) || u(e.content) : a = u(e.content), a.indexOf("<pre") === 0)
    return a + `
`;
  if (s) {
    const f = e.attrIndex("class"), d = e.attrs ? e.attrs.slice() : [];
    f < 0 ? d.push(["class", i.langPrefix + c]) : (d[f] = d[f].slice(), d[f][1] += " " + i.langPrefix + c);
    const p = {
      attrs: d
    };
    return `<pre><code${t.renderAttrs(p)}>${a}</code></pre>
`;
  }
  return `<pre><code${t.renderAttrs(e)}>${a}</code></pre>
`;
};
o.image = function(n, r, i, l, t) {
  const e = n[r];
  return e.attrs[e.attrIndex("alt")][1] = t.renderInlineAsText(e.children, i, l), t.renderToken(n, r, i);
};
o.hardbreak = function(n, r, i) {
  return i.xhtmlOut ? `<br />
` : `<br>
`;
};
o.softbreak = function(n, r, i) {
  return i.breaks ? i.xhtmlOut ? `<br />
` : `<br>
` : `
`;
};
o.text = function(n, r) {
  return u(n[r].content);
};
o.html_block = function(n, r) {
  return n[r].content;
};
o.html_inline = function(n, r) {
  return n[r].content;
};
function h() {
  this.rules = b({}, o);
}
h.prototype.renderAttrs = function(r) {
  let i, l, t;
  if (!r.attrs)
    return "";
  for (t = "", i = 0, l = r.attrs.length; i < l; i++)
    t += " " + u(r.attrs[i][0]) + '="' + u(r.attrs[i][1]) + '"';
  return t;
};
h.prototype.renderToken = function(r, i, l) {
  const t = r[i];
  let e = "";
  if (t.hidden)
    return "";
  t.block && t.nesting !== -1 && i && r[i - 1].hidden && (e += `
`), e += (t.nesting === -1 ? "</" : "<") + t.tag, e += this.renderAttrs(t), t.nesting === 0 && l.xhtmlOut && (e += " /");
  let s = !1;
  if (t.block && (s = !0, t.nesting === 1 && i + 1 < r.length)) {
    const c = r[i + 1];
    (c.type === "inline" || c.hidden || c.nesting === -1 && c.tag === t.tag) && (s = !1);
  }
  return e += s ? `>
` : ">", e;
};
h.prototype.renderInline = function(n, r, i) {
  let l = "";
  const t = this.rules;
  for (let e = 0, s = n.length; e < s; e++) {
    const c = n[e].type;
    typeof t[c] < "u" ? l += t[c](n, e, r, i, this) : l += this.renderToken(n, e, r);
  }
  return l;
};
h.prototype.renderInlineAsText = function(n, r, i) {
  let l = "";
  for (let t = 0, e = n.length; t < e; t++)
    switch (n[t].type) {
      case "text":
        l += n[t].content;
        break;
      case "image":
        l += this.renderInlineAsText(n[t].children, r, i);
        break;
      case "html_inline":
      case "html_block":
        l += n[t].content;
        break;
      case "softbreak":
      case "hardbreak":
        l += `
`;
        break;
    }
  return l;
};
h.prototype.render = function(n, r, i) {
  let l = "";
  const t = this.rules;
  for (let e = 0, s = n.length; e < s; e++) {
    const c = n[e].type;
    c === "inline" ? l += this.renderInline(n[e].children, r, i) : typeof t[c] < "u" ? l += t[c](n, e, r, i, this) : l += this.renderToken(n, e, r, i);
  }
  return l;
};
export {
  h as default
};
