import { assignWithDepth_default as E, directiveRegex as $, sanitizeDirective as X, detectType as Y, common_default as b } from "./chunk-ABZYJK2D.js";
import { __name as s, log as g } from "./chunk-AGHRB4JF.js";
import { d as G } from "../../../../../../../../_virtual/index4.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 w from "../../../../../../lodash-es@4.17.22/node_modules/lodash-es/memoize.js";
import J from "../../../../../../lodash-es@4.17.22/node_modules/lodash-es/merge.js";
import q, { stepBefore as Z, stepAfter as K } from "../../../../../../d3-shape@3.2.0/node_modules/d3-shape/src/curve/step.js";
import Q from "../../../../../../d3-shape@3.2.0/node_modules/d3-shape/src/curve/natural.js";
import { monotoneY as V, monotoneX as tt } from "../../../../../../d3-shape@3.2.0/node_modules/d3-shape/src/curve/monotone.js";
import et from "../../../../../../d3-shape@3.2.0/node_modules/d3-shape/src/curve/linearClosed.js";
import rt from "../../../../../../d3-shape@3.2.0/node_modules/d3-shape/src/curve/linear.js";
import nt from "../../../../../../d3-shape@3.2.0/node_modules/d3-shape/src/curve/catmullRom.js";
import it from "../../../../../../d3-shape@3.2.0/node_modules/d3-shape/src/curve/catmullRomOpen.js";
import ot from "../../../../../../d3-shape@3.2.0/node_modules/d3-shape/src/curve/catmullRomClosed.js";
import at from "../../../../../../d3-shape@3.2.0/node_modules/d3-shape/src/curve/cardinal.js";
import st from "../../../../../../d3-shape@3.2.0/node_modules/d3-shape/src/curve/cardinalOpen.js";
import ct from "../../../../../../d3-shape@3.2.0/node_modules/d3-shape/src/curve/cardinalClosed.js";
import lt from "../../../../../../d3-shape@3.2.0/node_modules/d3-shape/src/curve/bundle.js";
import { bumpY as ut, bumpX as ht } from "../../../../../../d3-shape@3.2.0/node_modules/d3-shape/src/curve/bump.js";
import dt from "../../../../../../d3-shape@3.2.0/node_modules/d3-shape/src/curve/basisOpen.js";
import ft from "../../../../../../d3-shape@3.2.0/node_modules/d3-shape/src/curve/basisClosed.js";
import mt from "../../../../../../d3-shape@3.2.0/node_modules/d3-shape/src/curve/basis.js";
import gt from "../../../../../../d3-selection@3.0.0/node_modules/d3-selection/src/select.js";
var vt = "​", pt = {
  curveBasis: mt,
  curveBasisClosed: ft,
  curveBasisOpen: dt,
  curveBumpX: ht,
  curveBumpY: ut,
  curveBundle: lt,
  curveCardinalClosed: ct,
  curveCardinalOpen: st,
  curveCardinal: at,
  curveCatmullRomClosed: ot,
  curveCatmullRomOpen: it,
  curveCatmullRom: nt,
  curveLinear: rt,
  curveLinearClosed: et,
  curveMonotoneX: tt,
  curveMonotoneY: V,
  curveNatural: Q,
  curveStep: q,
  curveStepAfter: K,
  curveStepBefore: Z
}, yt = /\s*(?:(\w+)(?=:):|(\w+))\s*(?:(\w+)|((?:(?!}%{2}).|\r?\n)*))?\s*(?:}%{2})?/gi, xt = /* @__PURE__ */ s(function(e, t) {
  const r = F(e, /(?:init\b)|(?:initialize\b)/);
  let n = {};
  if (Array.isArray(r)) {
    const a = r.map((l) => l.args);
    X(a), n = E(n, [...a]);
  } else
    n = r.args;
  if (!n)
    return;
  let o = Y(e, t);
  const i = "config";
  return n[i] !== void 0 && (o === "flowchart-v2" && (o = "flowchart"), n[o] = n[i], delete n[i]), n;
}, "detectInit"), F = /* @__PURE__ */ s(function(e, t = null) {
  var r, n;
  try {
    const o = new RegExp(
      `[%]{2}(?![{]${yt.source})(?=[}][%]{2}).*
`,
      "ig"
    );
    e = e.trim().replace(o, "").replace(/'/gm, '"'), g.debug(
      `Detecting diagram directive${t !== null ? " type:" + t : ""} based on the text:${e}`
    );
    let i;
    const a = [];
    for (; (i = $.exec(e)) !== null; )
      if (i.index === $.lastIndex && $.lastIndex++, i && !t || t && ((r = i[1]) != null && r.match(t)) || t && ((n = i[2]) != null && n.match(t))) {
        const l = i[1] ? i[1] : i[2], c = i[3] ? i[3].trim() : i[4] ? JSON.parse(i[4].trim()) : null;
        a.push({ type: l, args: c });
      }
    return a.length === 0 ? { type: e, args: null } : a.length === 1 ? a[0] : a;
  } catch (o) {
    return g.error(
      `ERROR: ${o.message} - Unable to parse directive type: '${t}' based on the text: '${e}'`
    ), { type: void 0, args: null };
  }
}, "detectDirective"), ie = /* @__PURE__ */ s(function(e) {
  return e.replace($, "");
}, "removeDirectives"), Mt = /* @__PURE__ */ s(function(e, t) {
  for (const [r, n] of t.entries())
    if (n.match(e))
      return r;
  return -1;
}, "isSubstringInArray");
function _(e, t) {
  if (!e)
    return t;
  const r = `curve${e.charAt(0).toUpperCase() + e.slice(1)}`;
  return pt[r] ?? t;
}
s(_, "interpolateToCurve");
function A(e, t) {
  const r = e.trim();
  if (r)
    return t.securityLevel !== "loose" ? G.sanitizeUrl(r) : r;
}
s(A, "formatUrl");
var $t = /* @__PURE__ */ s((e, ...t) => {
  const r = e.split("."), n = r.length - 1, o = r[n];
  let i = window;
  for (let a = 0; a < n; a++)
    if (i = i[r[a]], !i) {
      g.error(`Function name: ${e} not found in window`);
      return;
    }
  i[o](...t);
}, "runFunc");
function S(e, t) {
  return !e || !t ? 0 : Math.sqrt(Math.pow(t.x - e.x, 2) + Math.pow(t.y - e.y, 2));
}
s(S, "distance");
function N(e) {
  let t, r = 0;
  e.forEach((o) => {
    r += S(o, t), t = o;
  });
  const n = r / 2;
  return B(e, n);
}
s(N, "traverseEdge");
function R(e) {
  return e.length === 1 ? e[0] : N(e);
}
s(R, "calcLabelPosition");
var L = /* @__PURE__ */ s((e, t = 2) => {
  const r = Math.pow(10, t);
  return Math.round(e * r) / r;
}, "roundNumber"), B = /* @__PURE__ */ s((e, t) => {
  let r, n = t;
  for (const o of e) {
    if (r) {
      const i = S(o, r);
      if (i === 0)
        return r;
      if (i < n)
        n -= i;
      else {
        const a = n / i;
        if (a <= 0)
          return r;
        if (a >= 1)
          return { x: o.x, y: o.y };
        if (a > 0 && a < 1)
          return {
            x: L((1 - a) * r.x + a * o.x, 5),
            y: L((1 - a) * r.y + a * o.y, 5)
          };
      }
    }
    r = o;
  }
  throw new Error("Could not find a suitable point for the given distance");
}, "calculatePoint"), bt = /* @__PURE__ */ s((e, t, r) => {
  g.info(`our points ${JSON.stringify(t)}`), t[0] !== r && (t = t.reverse());
  const o = B(t, 25), i = e ? 10 : 5, a = Math.atan2(t[0].y - o.y, t[0].x - o.x), l = { x: 0, y: 0 };
  return l.x = Math.sin(a) * i + (t[0].x + o.x) / 2, l.y = -Math.cos(a) * i + (t[0].y + o.y) / 2, l;
}, "calcCardinalityPosition");
function D(e, t, r) {
  const n = structuredClone(r);
  g.info("our points", n), t !== "start_left" && t !== "start_right" && n.reverse();
  const o = 25 + e, i = B(n, o), a = 10 + e * 0.5, l = Math.atan2(n[0].y - i.y, n[0].x - i.x), c = { x: 0, y: 0 };
  return t === "start_left" ? (c.x = Math.sin(l + Math.PI) * a + (n[0].x + i.x) / 2, c.y = -Math.cos(l + Math.PI) * a + (n[0].y + i.y) / 2) : t === "end_right" ? (c.x = Math.sin(l - Math.PI) * a + (n[0].x + i.x) / 2 - 5, c.y = -Math.cos(l - Math.PI) * a + (n[0].y + i.y) / 2 - 5) : t === "end_left" ? (c.x = Math.sin(l) * a + (n[0].x + i.x) / 2 - 5, c.y = -Math.cos(l) * a + (n[0].y + i.y) / 2 - 5) : (c.x = Math.sin(l) * a + (n[0].x + i.x) / 2, c.y = -Math.cos(l) * a + (n[0].y + i.y) / 2), c;
}
s(D, "calcTerminalLabelPosition");
function H(e) {
  let t = "", r = "";
  for (const n of e)
    n !== void 0 && (n.startsWith("color:") || n.startsWith("text-align:") ? r = r + n + ";" : t = t + n + ";");
  return { style: t, labelStyle: r };
}
s(H, "getStylesFromArray");
var T = 0, wt = /* @__PURE__ */ s(() => (T++, "id-" + Math.random().toString(36).substr(2, 12) + "-" + T), "generateId");
function O(e) {
  let t = "";
  const r = "0123456789abcdef", n = r.length;
  for (let o = 0; o < e; o++)
    t += r.charAt(Math.floor(Math.random() * n));
  return t;
}
s(O, "makeRandomHex");
var St = /* @__PURE__ */ s((e) => O(e.length), "random"), Bt = /* @__PURE__ */ s(function() {
  return {
    x: 0,
    y: 0,
    fill: void 0,
    anchor: "start",
    style: "#666",
    width: 100,
    height: 100,
    textMargin: 0,
    rx: 0,
    ry: 0,
    valign: void 0,
    text: ""
  };
}, "getTextObj"), Ct = /* @__PURE__ */ s(function(e, t) {
  const r = t.text.replace(b.lineBreakRegex, " "), [, n] = P(t.fontSize), o = e.append("text");
  o.attr("x", t.x), o.attr("y", t.y), o.style("text-anchor", t.anchor), o.style("font-family", t.fontFamily), o.style("font-size", n), o.style("font-weight", t.fontWeight), o.attr("fill", t.fill), t.class !== void 0 && o.attr("class", t.class);
  const i = o.append("tspan");
  return i.attr("x", t.x + t.textMargin * 2), i.attr("fill", t.fill), i.text(r), o;
}, "drawSimpleText"), Pt = w(
  (e, t, r) => {
    if (!e || (r = Object.assign(
      { fontSize: 12, fontWeight: 400, fontFamily: "Arial", joinWith: "<br/>" },
      r
    ), b.lineBreakRegex.test(e)))
      return e;
    const n = e.split(" ").filter(Boolean), o = [];
    let i = "";
    return n.forEach((a, l) => {
      const c = v(`${a} `, r), u = v(i, r);
      if (c > t) {
        const { hyphenatedStrings: m, remainingWord: h } = Wt(a, t, "-", r);
        o.push(i, ...m), i = h;
      } else u + c >= t ? (o.push(i), i = a) : i = [i, a].filter(Boolean).join(" ");
      l + 1 === n.length && o.push(i);
    }), o.filter((a) => a !== "").join(r.joinWith);
  },
  (e, t, r) => `${e}${t}${r.fontSize}${r.fontWeight}${r.fontFamily}${r.joinWith}`
), Wt = w(
  (e, t, r = "-", n) => {
    n = Object.assign(
      { fontSize: 12, fontWeight: 400, fontFamily: "Arial", margin: 0 },
      n
    );
    const o = [...e], i = [];
    let a = "";
    return o.forEach((l, c) => {
      const u = `${a}${l}`;
      if (v(u, n) >= t) {
        const y = c + 1, m = o.length === y, h = `${u}${r}`;
        i.push(m ? u : h), a = "";
      } else
        a = u;
    }), { hyphenatedStrings: i, remainingWord: a };
  },
  (e, t, r = "-", n) => `${e}${t}${r}${n.fontSize}${n.fontWeight}${n.fontFamily}`
);
function z(e, t) {
  return C(e, t).height;
}
s(z, "calculateTextHeight");
function v(e, t) {
  return C(e, t).width;
}
s(v, "calculateTextWidth");
var C = w(
  (e, t) => {
    const { fontSize: r = 12, fontFamily: n = "Arial", fontWeight: o = 400 } = t;
    if (!e)
      return { width: 0, height: 0 };
    const [, i] = P(r), a = ["sans-serif", n], l = e.split(b.lineBreakRegex), c = [], u = gt("body");
    if (!u.remove)
      return { width: 0, height: 0, lineHeight: 0 };
    const p = u.append("svg");
    for (const m of a) {
      let h = 0;
      const d = { width: 0, height: 0, lineHeight: 0 };
      for (const k of l) {
        const W = Bt();
        W.text = k || vt;
        const I = Ct(p, W).style("font-size", i).style("font-weight", o).style("font-family", m), x = (I._groups || I)[0][0].getBBox();
        if (x.width === 0 && x.height === 0)
          throw new Error("svg element not in render tree");
        d.width = Math.round(Math.max(d.width, x.width)), h = Math.round(x.height), d.height += h, d.lineHeight = Math.round(Math.max(d.lineHeight, h));
      }
      c.push(d);
    }
    p.remove();
    const y = isNaN(c[1].height) || isNaN(c[1].width) || isNaN(c[1].lineHeight) || c[0].height > c[1].height && c[0].width > c[1].width && c[0].lineHeight > c[1].lineHeight ? 0 : 1;
    return c[y];
  },
  (e, t) => `${e}${t.fontSize}${t.fontWeight}${t.fontFamily}`
), f, It = (f = class {
  constructor(t = !1, r) {
    this.count = 0, this.count = r ? r.length : 0, this.next = t ? () => this.count++ : () => Date.now();
  }
}, s(f, "InitIDGenerator"), f), M, Lt = /* @__PURE__ */ s(function(e) {
  return M = M || document.createElement("div"), e = escape(e).replace(/%26/g, "&").replace(/%23/g, "#").replace(/%3B/g, ";"), M.innerHTML = e, unescape(M.textContent);
}, "entityDecode");
function Tt(e) {
  return "str" in e;
}
s(Tt, "isDetailedError");
var Et = /* @__PURE__ */ s((e, t, r, n) => {
  var i;
  if (!n)
    return;
  const o = (i = e.node()) == null ? void 0 : i.getBBox();
  o && e.append("text").text(n).attr("text-anchor", "middle").attr("x", o.x + o.width / 2).attr("y", -r).attr("class", t);
}, "insertTitle"), P = /* @__PURE__ */ s((e) => {
  if (typeof e == "number")
    return [e, e + "px"];
  const t = parseInt(e ?? "", 10);
  return Number.isNaN(t) ? [void 0, void 0] : e === String(t) ? [t, e + "px"] : [t, e];
}, "parseFontSize");
function j(e, t) {
  return J({}, e, t);
}
s(j, "cleanAndMerge");
var oe = {
  assignWithDepth: E,
  wrapLabel: Pt,
  calculateTextHeight: z,
  calculateTextWidth: v,
  calculateTextDimensions: C,
  cleanAndMerge: j,
  detectInit: xt,
  detectDirective: F,
  isSubstringInArray: Mt,
  interpolateToCurve: _,
  calcLabelPosition: R,
  calcCardinalityPosition: bt,
  calcTerminalLabelPosition: D,
  formatUrl: A,
  getStylesFromArray: H,
  generateId: wt,
  random: St,
  runFunc: $t,
  entityDecode: Lt,
  insertTitle: Et,
  isLabelCoordinateInPath: U,
  parseFontSize: P,
  InitIDGenerator: It
}, ae = /* @__PURE__ */ s(function(e) {
  let t = e;
  return t = t.replace(/style.*:\S*#.*;/g, function(r) {
    return r.substring(0, r.length - 1);
  }), t = t.replace(/classDef.*:\S*#.*;/g, function(r) {
    return r.substring(0, r.length - 1);
  }), t = t.replace(/#\w+;/g, function(r) {
    const n = r.substring(1, r.length - 1);
    return /^\+?\d+$/.test(n) ? "ﬂ°°" + n + "¶ß" : "ﬂ°" + n + "¶ß";
  }), t;
}, "encodeEntities"), se = /* @__PURE__ */ s(function(e) {
  return e.replace(/ﬂ°°/g, "&#").replace(/ﬂ°/g, "&").replace(/¶ß/g, ";");
}, "decodeEntities"), ce = /* @__PURE__ */ s((e, t, {
  counter: r = 0,
  prefix: n,
  suffix: o
}, i) => i || `${n ? `${n}_` : ""}${e}_${t}_${r}${o ? `_${o}` : ""}`, "getEdgeId");
function Ft(e) {
  return e ?? null;
}
s(Ft, "handleUndefinedAttr");
function U(e, t) {
  const r = Math.round(e.x), n = Math.round(e.y), o = t.replace(
    /(\d+\.\d+)/g,
    (i) => Math.round(parseFloat(i)).toString()
  );
  return o.includes(r.toString()) || o.includes(n.toString());
}
s(U, "isLabelCoordinateInPath");
export {
  vt as ZERO_WIDTH_SPACE,
  z as calculateTextHeight,
  v as calculateTextWidth,
  j as cleanAndMerge,
  se as decodeEntities,
  ae as encodeEntities,
  wt as generateId,
  ce as getEdgeId,
  H as getStylesFromArray,
  Ft as handleUndefinedAttr,
  _ as interpolateToCurve,
  Tt as isDetailedError,
  P as parseFontSize,
  St as random,
  ie as removeDirectives,
  oe as utils_default,
  Pt as wrapLabel
};
