import { decodeEntities as O } from "./chunk-S3R3BYOJ.js";
import { hasKatex as S, getConfig as W, sanitizeText as k, renderKatexSanitized as G, common_default as K } from "./chunk-ABZYJK2D.js";
import { __name as s, log as d } from "./chunk-AGHRB4JF.js";
import { stringToIcon as P } from "../../../../../../@iconify_utils@3.1.0/node_modules/@iconify/utils/lib/icon/name.js";
import { getIconData as V } from "../../../../../../@iconify_utils@3.1.0/node_modules/@iconify/utils/lib/icon-set/get-icon.js";
import { iconToSVG as _ } from "../../../../../../@iconify_utils@3.1.0/node_modules/@iconify/utils/lib/svg/build.js";
import { replaceIDs as U } from "../../../../../../@iconify_utils@3.1.0/node_modules/@iconify/utils/lib/svg/id.js";
import { iconToHTML as X } from "../../../../../../@iconify_utils@3.1.0/node_modules/@iconify/utils/lib/svg/html.js";
import "../../../../../../d3-transition@3.0.1_d3-selection@3.0.0/node_modules/d3-transition/src/selection/index.js";
import "../../../../../../d3-zoom@3.0.0/node_modules/d3-zoom/src/transform.js";
import { marked as E } from "../../../../../../marked@16.4.2/node_modules/marked/lib/marked.esm.js";
import { dedent as Y } from "../../../../../../ts-dedent@2.2.0/node_modules/ts-dedent/esm/index.js";
import b from "../../../../../../d3-selection@3.0.0/node_modules/d3-selection/src/select.js";
var Z = {
  body: '<g><rect width="80" height="80" style="fill: #087ebf; stroke-width: 0px;"/><text transform="translate(21.16 64.67)" style="fill: #fff; font-family: ArialMT, Arial; font-size: 67.75px;"><tspan x="0" y="0">?</tspan></text></g>',
  height: 80,
  width: 80
}, w = /* @__PURE__ */ new Map(), L = /* @__PURE__ */ new Map(), ut = /* @__PURE__ */ s((r) => {
  for (const t of r) {
    if (!t.name)
      throw new Error(
        'Invalid icon loader. Must have a "name" property with non-empty string value.'
      );
    if (d.debug("Registering icon pack:", t.name), "loader" in t)
      L.set(t.name, t.loader);
    else if ("icons" in t)
      w.set(t.name, t.icons);
    else
      throw d.error("Invalid icon loader:", t), new Error('Invalid icon loader. Must have either "icons" or "loader" property.');
  }
}, "registerIconPacks"), R = /* @__PURE__ */ s(async (r, t) => {
  const e = P(r, !0, t !== void 0);
  if (!e)
    throw new Error(`Invalid icon name: ${r}`);
  const o = e.prefix || t;
  if (!o)
    throw new Error(`Icon name must contain a prefix: ${r}`);
  let n = w.get(o);
  if (!n) {
    const l = L.get(o);
    if (!l)
      throw new Error(`Icon set not found: ${e.prefix}`);
    try {
      n = { ...await l(), prefix: o }, w.set(o, n);
    } catch (a) {
      throw d.error(a), new Error(`Failed to load icon set: ${e.prefix}`);
    }
  }
  const i = V(n, e.name);
  if (!i)
    throw new Error(`Icon not found: ${r}`);
  return i;
}, "getRegisteredIconData"), q = /* @__PURE__ */ s(async (r) => {
  try {
    return await R(r), !0;
  } catch {
    return !1;
  }
}, "isIconAvailable"), J = /* @__PURE__ */ s(async (r, t, e) => {
  let o;
  try {
    o = await R(r, t == null ? void 0 : t.fallbackPrefix);
  } catch (l) {
    d.error(l), o = Z;
  }
  const n = _(o, t), i = X(U(n.body), {
    ...n.attributes,
    ...e
  });
  return k(i, W());
}, "getIconSVG");
function $(r, { markdownAutoWrap: t }) {
  const o = r.replace(/<br\/>/g, `
`).replace(/\n{2,}/g, `
`), n = Y(o);
  return t === !1 ? n.replace(/ /g, "&nbsp;") : n;
}
s($, "preprocessMarkdown");
function M(r, t = {}) {
  const e = $(r, t), o = E.lexer(e), n = [[]];
  let i = 0;
  function l(a, f = "normal") {
    a.type === "text" ? a.text.split(`
`).forEach((p, h) => {
      h !== 0 && (i++, n.push([])), p.split(" ").forEach((u) => {
        u = u.replace(/&#39;/g, "'"), u && n[i].push({ content: u, type: f });
      });
    }) : a.type === "strong" || a.type === "em" ? a.tokens.forEach((c) => {
      l(c, a.type);
    }) : a.type === "html" && n[i].push({ content: a.text, type: "normal" });
  }
  return s(l, "processNode"), o.forEach((a) => {
    var f;
    a.type === "paragraph" ? (f = a.tokens) == null || f.forEach((c) => {
      l(c);
    }) : a.type === "html" ? n[i].push({ content: a.text, type: "normal" }) : n[i].push({ content: a.raw, type: "normal" });
  }), n;
}
s(M, "markdownToLines");
function j(r, { markdownAutoWrap: t } = {}) {
  const e = E.lexer(r);
  function o(n) {
    var i, l, a;
    return n.type === "text" ? t === !1 ? n.text.replace(/\n */g, "<br/>").replace(/ /g, "&nbsp;") : n.text.replace(/\n */g, "<br/>") : n.type === "strong" ? `<strong>${(i = n.tokens) == null ? void 0 : i.map(o).join("")}</strong>` : n.type === "em" ? `<em>${(l = n.tokens) == null ? void 0 : l.map(o).join("")}</em>` : n.type === "paragraph" ? `<p>${(a = n.tokens) == null ? void 0 : a.map(o).join("")}</p>` : n.type === "space" ? "" : n.type === "html" ? `${n.text}` : n.type === "escape" ? n.text : (d.warn(`Unsupported markdown: ${n.type}`), n.raw);
  }
  return s(o, "output"), e.map(o).join("");
}
s(j, "markdownToHTML");
function D(r) {
  return Intl.Segmenter ? [...new Intl.Segmenter().segment(r)].map((t) => t.segment) : [...r];
}
s(D, "splitTextToChars");
function B(r, t) {
  const e = D(t.content);
  return v(r, [], e, t.type);
}
s(B, "splitWordToFitWidth");
function v(r, t, e, o) {
  if (e.length === 0)
    return [
      { content: t.join(""), type: o },
      { content: "", type: o }
    ];
  const [n, ...i] = e, l = [...t, n];
  return r([{ content: l.join(""), type: o }]) ? v(r, l, i, o) : (t.length === 0 && n && (t.push(n), e.shift()), [
    { content: t.join(""), type: o },
    { content: e.join(""), type: o }
  ]);
}
s(v, "splitWordToFitWidthRecursion");
function F(r, t) {
  if (r.some(({ content: e }) => e.includes(`
`)))
    throw new Error("splitLineToFitWidth does not support newlines in the line");
  return m(r, t);
}
s(F, "splitLineToFitWidth");
function m(r, t, e = [], o = []) {
  if (r.length === 0)
    return o.length > 0 && e.push(o), e.length > 0 ? e : [];
  let n = "";
  r[0].content === " " && (n = " ", r.shift());
  const i = r.shift() ?? { content: " ", type: "normal" }, l = [...o];
  if (n !== "" && l.push({ content: n, type: "normal" }), l.push(i), t(l))
    return m(r, t, e, l);
  if (o.length > 0)
    e.push(o), r.unshift(i);
  else if (i.content) {
    const [a, f] = B(t, i);
    e.push([a]), f.content && r.unshift(f);
  }
  return m(r, t, e);
}
s(m, "splitLineToFitWidthRecursion");
function T(r, t) {
  t && r.attr("style", t);
}
s(T, "applyStyle");
async function A(r, t, e, o, n = !1, i = W()) {
  const l = r.append("foreignObject");
  l.attr("width", `${10 * e}px`), l.attr("height", `${10 * e}px`);
  const a = l.append("xhtml:div"), f = S(t.label) ? await G(t.label.replace(K.lineBreakRegex, `
`), i) : k(t.label, i), c = t.isNode ? "nodeLabel" : "edgeLabel", p = a.append("span");
  p.html(f), T(p, t.labelStyle), p.attr("class", `${c} ${o}`), T(a, t.labelStyle), a.style("display", "table-cell"), a.style("white-space", "nowrap"), a.style("line-height", "1.5"), a.style("max-width", e + "px"), a.style("text-align", "center"), a.attr("xmlns", "http://www.w3.org/1999/xhtml"), n && a.attr("class", "labelBkg");
  let h = a.node().getBoundingClientRect();
  return h.width === e && (a.style("display", "table"), a.style("white-space", "break-spaces"), a.style("width", e + "px"), h = a.node().getBoundingClientRect()), l.node();
}
s(A, "addHtmlSpan");
function x(r, t, e) {
  return r.append("tspan").attr("class", "text-outer-tspan").attr("x", 0).attr("y", t * e - 0.1 + "em").attr("dy", e + "em");
}
s(x, "createTspan");
function H(r, t, e) {
  const o = r.append("text"), n = x(o, 1, t);
  y(n, e);
  const i = n.node().getComputedTextLength();
  return o.remove(), i;
}
s(H, "computeWidthOfText");
function Q(r, t, e) {
  var l;
  const o = r.append("text"), n = x(o, 1, t);
  y(n, [{ content: e, type: "normal" }]);
  const i = (l = n.node()) == null ? void 0 : l.getBoundingClientRect();
  return i && o.remove(), i;
}
s(Q, "computeDimensionOfText");
function z(r, t, e, o = !1) {
  const i = t.append("g"), l = i.insert("rect").attr("class", "background").attr("style", "stroke: none"), a = i.append("text").attr("y", "-10.1");
  let f = 0;
  for (const c of e) {
    const p = /* @__PURE__ */ s((u) => H(i, 1.1, u) <= r, "checkWidth"), h = p(c) ? [c] : F(c, p);
    for (const u of h) {
      const g = x(a, f, 1.1);
      y(g, u), f++;
    }
  }
  if (o) {
    const c = a.node().getBBox(), p = 2;
    return l.attr("x", c.x - p).attr("y", c.y - p).attr("width", c.width + 2 * p).attr("height", c.height + 2 * p), i.node();
  } else
    return a.node();
}
s(z, "createFormattedText");
function y(r, t) {
  r.text(""), t.forEach((e, o) => {
    const n = r.append("tspan").attr("font-style", e.type === "em" ? "italic" : "normal").attr("class", "text-inner-tspan").attr("font-weight", e.type === "strong" ? "bold" : "normal");
    o === 0 ? n.text(e.content) : n.text(" " + e.content);
  });
}
s(y, "updateTextContentAndStyles");
async function C(r, t = {}) {
  const e = [];
  r.replace(/(fa[bklrs]?):fa-([\w-]+)/g, (n, i, l) => (e.push(
    (async () => {
      const a = `${i}:${l}`;
      return await q(a) ? await J(a, void 0, { class: "label-icon" }) : `<i class='${k(n, t).replace(":", " ")}'></i>`;
    })()
  ), n));
  const o = await Promise.all(e);
  return r.replace(/(fa[bklrs]?):fa-([\w-]+)/g, () => o.shift() ?? "");
}
s(C, "replaceIconSubstring");
var ht = /* @__PURE__ */ s(async (r, t = "", {
  style: e = "",
  isTitle: o = !1,
  classes: n = "",
  useHtmlLabels: i = !0,
  isNode: l = !0,
  width: a = 200,
  addSvgBackground: f = !1
} = {}, c) => {
  if (d.debug(
    "XYZ createText",
    t,
    e,
    o,
    n,
    i,
    l,
    "addSvgBackground: ",
    f
  ), i) {
    const p = j(t, c), h = await C(O(p), c), u = t.replace(/\\\\/g, "\\"), g = {
      isNode: l,
      label: S(t) ? u : h,
      labelStyle: e.replace("fill:", "color:")
    };
    return await A(r, g, a, n, f, c);
  } else {
    const p = t.replace(/<br\s*\/?>/g, "<br/>"), h = M(p.replace("<br>", "<br/>"), c), u = z(
      a,
      r,
      h,
      t ? f : !1
    );
    if (l) {
      /stroke:/.exec(e) && (e = e.replace("stroke:", "lineColor:"));
      const g = e.replace(/stroke:[^;]+;?/g, "").replace(/stroke-width:[^;]+;?/g, "").replace(/fill:[^;]+;?/g, "").replace(/color:/g, "fill:");
      b(u).attr("style", g);
    } else {
      const g = e.replace(/stroke:[^;]+;?/g, "").replace(/stroke-width:[^;]+;?/g, "").replace(/fill:[^;]+;?/g, "").replace(/background:/g, "fill:");
      b(u).select("rect").attr("style", g.replace(/background:/g, "fill:"));
      const I = e.replace(/stroke:[^;]+;?/g, "").replace(/stroke-width:[^;]+;?/g, "").replace(/fill:[^;]+;?/g, "").replace(/color:/g, "fill:");
      b(u).select("text").attr("style", I);
    }
    return u;
  }
}, "createText");
export {
  Q as computeDimensionOfText,
  ht as createText,
  J as getIconSVG,
  ut as registerIconPacks,
  C as replaceIconSubstring,
  Z as unknownIcon
};
