import { a7 as zt, a8 as At, ap as Lt, ao as Nt, a9 as Tt, aa as Et, _ as u, l as et, aU as Bt, ab as It, av as Rt, g as _, ae as Xt } from "./index-Bh-wTBlD.js";
import { m as Dt } from "./chunk-4BX2VUAB-CkAjNgA_-Bs9A5Kfv.js";
import { M as Ot } from "./mermaid-parser.core-DtLRLmJt-Bs8fzU82.js";
var G = /* @__PURE__ */ u((e, n) => {
  const a = e <= 1 ? e * 100 : e;
  if (a < 0 || a > 100)
    throw new Error(
      `${n} must be between 0-1 (decimal) or 0-100 (percentage). Received: ${e}`
    );
  return a;
}, "toPercent"), B = /* @__PURE__ */ u((e, n, a) => ({
  x: G(n, `${a} evolution`),
  y: G(e, `${a} visibility`)
}), "toCoordinates"), tt = /* @__PURE__ */ u((e) => {
  if (e) {
    if (e === "+<>")
      return "bidirectional";
    if (e === "+<")
      return "backward";
    if (e === "+>")
      return "forward";
  }
}, "getFlowFromPort"), Yt = /* @__PURE__ */ u((e) => {
  if (!(e != null && e.startsWith("+")))
    return {};
  const n = /^\+'([^']*)'/.exec(e), a = n == null ? void 0 : n[1];
  return e.includes("<>") ? { flow: "bidirectional", label: a } : e.includes("<") ? { flow: "backward", label: a } : e.includes(">") ? { flow: "forward", label: a } : { label: a };
}, "extractFlowFromArrow"), Ft = /* @__PURE__ */ u((e, n) => {
  if (Dt(e, n), e.size && n.setSize(e.size.width, e.size.height), e.evolution) {
    const a = e.evolution.stages.map((m) => m.secondName ? `${m.name.trim()} / ${m.secondName.trim()}` : m.name.trim()), h = e.evolution.stages.filter((m) => m.boundary !== void 0).map((m) => m.boundary);
    n.updateAxes({ stages: a, stageBoundaries: h });
  }
  if (e.anchors.forEach((a) => {
    const h = B(a.visibility, a.evolution, `Anchor "${a.name}"`);
    n.addNode(a.name, a.name, h.x, h.y, "anchor");
  }), e.components.forEach((a) => {
    var h;
    const m = B(
      a.visibility,
      a.evolution,
      `Component "${a.name}"`
    ), k = a.label ? (a.label.negX ? -1 : 1) * a.label.offsetX : void 0, i = a.label ? (a.label.negY ? -1 : 1) * a.label.offsetY : void 0, p = (h = a.decorator) == null ? void 0 : h.strategy;
    n.addNode(
      a.name,
      a.name,
      m.x,
      m.y,
      "component",
      k,
      i,
      a.inertia,
      p
    );
  }), e.notes.forEach((a) => {
    const h = B(a.visibility, a.evolution, `Note "${a.text}"`);
    n.addNote(a.text, h.x, h.y);
  }), e.pipelines.forEach((a) => {
    const h = n.getNode(a.parent);
    if (!h || typeof h.y != "number")
      throw new Error(
        `Pipeline "${a.parent}" must reference an existing component with coordinates.`
      );
    const m = h.y;
    n.startPipeline(a.parent), a.components.forEach((k) => {
      const i = `${a.parent}_${k.name}`, p = k.label ? (k.label.negX ? -1 : 1) * k.label.offsetX : void 0, b = k.label ? (k.label.negY ? -1 : 1) * k.label.offsetY : void 0, L = G(k.evolution, `Pipeline component "${k.name}" evolution`);
      n.addNode(
        i,
        k.name,
        L,
        m,
        "pipeline-component",
        p,
        b
      ), n.addPipelineComponent(a.parent, i);
    });
  }), e.links.forEach((a) => {
    const h = !!a.arrow && (a.arrow.includes("-.->") || a.arrow.includes(".-."));
    let m = tt(a.fromPort) ?? tt(a.toPort);
    const { flow: k, label: i } = Yt(a.arrow);
    !m && k && (m = k);
    const p = a.linkLabel, b = i ?? p;
    n.addLink(a.from, a.to, h, b, m);
  }), e.evolves.forEach((a) => {
    const h = n.getNode(a.component);
    if ((h == null ? void 0 : h.y) !== void 0) {
      const m = G(a.target, `Evolve target for "${a.component}"`);
      n.addTrend(a.component, m, h.y);
    }
  }), e.annotations.length > 0) {
    const a = e.annotations[0], h = B(a.x, a.y, "Annotations box");
    n.setAnnotationsBox(h.x, h.y);
  }
  e.annotation.forEach((a) => {
    const h = B(a.x, a.y, `Annotation ${a.number}`);
    n.addAnnotation(a.number, [{ x: h.x, y: h.y }], a.text);
  }), e.accelerators.forEach((a) => {
    const h = B(a.x, a.y, `Accelerator "${a.name}"`);
    n.addAccelerator(a.name, h.x, h.y);
  }), e.deaccelerators.forEach((a) => {
    const h = B(
      a.x,
      a.y,
      `Deaccelerator "${a.name}"`
    );
    n.addDeaccelerator(a.name, h.x, h.y);
  });
}, "populateDb"), at = {
  parser: {
    // @ts-expect-error - WardleyDB is not assignable to DiagramDB
    yy: void 0
  },
  parse: /* @__PURE__ */ u(async (e) => {
    var n;
    const a = await Ot("wardley", e);
    et.debug(a);
    const h = (n = at.parser) == null ? void 0 : n.yy;
    if (!h || typeof h.addNode != "function")
      throw new Error(
        "parser.parser?.yy was not a WardleyDB. This is due to a bug within Mermaid, please report this issue at https://github.com/mermaid-js/mermaid/issues."
      );
    Ft(a, h);
  }, "parse")
}, Q, Wt = (Q = class {
  constructor() {
    this.nodes = /* @__PURE__ */ new Map(), this.links = [], this.trends = /* @__PURE__ */ new Map(), this.pipelines = /* @__PURE__ */ new Map(), this.annotations = [], this.notes = [], this.accelerators = [], this.deaccelerators = [], this.axes = {};
  }
  addNode(e) {
    const n = this.nodes.get(e.id) ?? { id: e.id, label: e.label }, a = {
      ...n,
      ...e,
      className: e.className ?? n.className,
      labelOffsetX: e.labelOffsetX ?? n.labelOffsetX,
      labelOffsetY: e.labelOffsetY ?? n.labelOffsetY
    };
    this.nodes.set(e.id, a);
  }
  addLink(e) {
    this.links.push(e);
  }
  addTrend(e) {
    this.trends.set(e.nodeId, e);
  }
  startPipeline(e) {
    this.pipelines.set(e, { nodeId: e, componentIds: [] });
    const n = this.nodes.get(e);
    n && (n.isPipelineParent = !0);
  }
  addPipelineComponent(e, n) {
    const a = this.pipelines.get(e);
    a && a.componentIds.push(n);
    const h = this.nodes.get(n);
    h && (h.inPipeline = !0);
  }
  addAnnotation(e) {
    this.annotations.push(e);
  }
  addNote(e) {
    this.notes.push(e);
  }
  addAccelerator(e) {
    this.accelerators.push(e);
  }
  addDeaccelerator(e) {
    this.deaccelerators.push(e);
  }
  setAnnotationsBox(e, n) {
    this.annotationsBox = { x: e, y: n };
  }
  setAxes(e) {
    this.axes = {
      ...this.axes,
      ...e
    };
  }
  setSize(e, n) {
    this.size = { width: e, height: n };
  }
  getNode(e) {
    return this.nodes.get(e);
  }
  build() {
    const e = [];
    for (const n of this.nodes.values()) {
      if (typeof n.x != "number" || typeof n.y != "number")
        throw new Error(`Node "${n.label}" is missing coordinates`);
      e.push(n);
    }
    return {
      nodes: e,
      links: [...this.links],
      trends: [...this.trends.values()],
      pipelines: [...this.pipelines.values()],
      annotations: [...this.annotations],
      notes: [...this.notes],
      accelerators: [...this.accelerators],
      deaccelerators: [...this.deaccelerators],
      annotationsBox: this.annotationsBox,
      axes: { ...this.axes },
      size: this.size
    };
  }
  clear() {
    this.nodes.clear(), this.links = [], this.trends.clear(), this.pipelines.clear(), this.annotations = [], this.notes = [], this.accelerators = [], this.deaccelerators = [], this.annotationsBox = void 0, this.axes = {}, this.size = void 0;
  }
}, u(Q, "WardleyBuilder"), Q), S = new Wt();
function z(e) {
  const n = _();
  return Xt(e.trim(), n);
}
u(z, "textSanitizer");
function rt() {
  return _()["wardley-beta"];
}
u(rt, "getConfig");
function nt(e, n, a, h, m, k, i, p, b) {
  S.addNode({
    id: e,
    label: z(n),
    x: a,
    y: h,
    className: m,
    labelOffsetX: k,
    labelOffsetY: i,
    inertia: p,
    sourceStrategy: b
  });
}
u(nt, "addNode");
function ot(e, n, a = !1, h, m) {
  S.addLink({ source: e, target: n, dashed: a, label: h, flow: m });
}
u(ot, "addLink");
function it(e, n, a) {
  S.addTrend({ nodeId: e, targetX: n, targetY: a });
}
u(it, "addTrend");
function st(e, n, a) {
  S.addAnnotation({
    number: e,
    coordinates: n,
    text: a ? z(a) : void 0
  });
}
u(st, "addAnnotation");
function lt(e, n, a) {
  S.addNote({
    text: z(e),
    x: n,
    y: a
  });
}
u(lt, "addNote");
function dt(e, n, a) {
  S.addAccelerator({
    name: z(e),
    x: n,
    y: a
  });
}
u(dt, "addAccelerator");
function ct(e, n, a) {
  S.addDeaccelerator({
    name: z(e),
    x: n,
    y: a
  });
}
u(ct, "addDeaccelerator");
function pt(e, n) {
  S.setAnnotationsBox(e, n);
}
u(pt, "setAnnotationsBox");
function ht(e, n) {
  S.setSize(e, n);
}
u(ht, "setSize");
function xt(e) {
  S.startPipeline(e);
}
u(xt, "startPipeline");
function gt(e, n) {
  S.addPipelineComponent(e, n);
}
u(gt, "addPipelineComponent");
function ft(e) {
  const n = {};
  e.xLabel && (n.xLabel = z(e.xLabel)), e.yLabel && (n.yLabel = z(e.yLabel)), e.stages && (n.stages = e.stages.map((a) => z(a))), e.stageBoundaries && (n.stageBoundaries = e.stageBoundaries), S.setAxes(n);
}
u(ft, "updateAxes");
function yt(e) {
  return S.getNode(e);
}
u(yt, "getNode");
function ut() {
  return S.build();
}
u(ut, "getWardleyData");
function mt() {
  S.clear(), Rt();
}
u(mt, "clear");
var qt = {
  getConfig: rt,
  addNode: nt,
  addLink: ot,
  addTrend: it,
  addAnnotation: st,
  addNote: lt,
  addAccelerator: dt,
  addDeaccelerator: ct,
  setAnnotationsBox: pt,
  setSize: ht,
  startPipeline: xt,
  addPipelineComponent: gt,
  updateAxes: ft,
  getNode: yt,
  getWardleyData: ut,
  clear: mt,
  setAccTitle: Et,
  getAccTitle: Tt,
  setDiagramTitle: Nt,
  getDiagramTitle: Lt,
  getAccDescription: At,
  setAccDescription: zt
}, jt = ["Genesis", "Custom Built", "Product", "Commodity"], Gt = /* @__PURE__ */ u(() => {
  var e, n, a, h, m, k, i, p, b, L, f, X;
  const { themeVariables: w } = _();
  return {
    backgroundColor: ((e = w.wardley) == null ? void 0 : e.backgroundColor) ?? w.background ?? "#fff",
    axisColor: ((n = w.wardley) == null ? void 0 : n.axisColor) ?? "#000",
    axisTextColor: ((a = w.wardley) == null ? void 0 : a.axisTextColor) ?? w.primaryTextColor ?? "#222",
    gridColor: ((h = w.wardley) == null ? void 0 : h.gridColor) ?? "rgba(100, 100, 100, 0.2)",
    componentFill: ((m = w.wardley) == null ? void 0 : m.componentFill) ?? "#fff",
    componentStroke: ((k = w.wardley) == null ? void 0 : k.componentStroke) ?? "#000",
    componentLabelColor: ((i = w.wardley) == null ? void 0 : i.componentLabelColor) ?? w.primaryTextColor ?? "#222",
    linkStroke: ((p = w.wardley) == null ? void 0 : p.linkStroke) ?? "#000",
    evolutionStroke: ((b = w.wardley) == null ? void 0 : b.evolutionStroke) ?? "#dc3545",
    annotationStroke: ((L = w.wardley) == null ? void 0 : L.annotationStroke) ?? "#000",
    annotationTextColor: ((f = w.wardley) == null ? void 0 : f.annotationTextColor) ?? w.primaryTextColor ?? "#222",
    annotationFill: ((X = w.wardley) == null ? void 0 : X.annotationFill) ?? w.background ?? "#fff"
  };
}, "getTheme"), _t = /* @__PURE__ */ u(() => {
  const e = _()["wardley-beta"];
  return {
    width: (e == null ? void 0 : e.width) ?? 900,
    height: (e == null ? void 0 : e.height) ?? 600,
    padding: (e == null ? void 0 : e.padding) ?? 48,
    nodeRadius: (e == null ? void 0 : e.nodeRadius) ?? 6,
    nodeLabelOffset: (e == null ? void 0 : e.nodeLabelOffset) ?? 8,
    axisFontSize: (e == null ? void 0 : e.axisFontSize) ?? 12,
    labelFontSize: (e == null ? void 0 : e.labelFontSize) ?? 10,
    showGrid: (e == null ? void 0 : e.showGrid) ?? !1,
    useMaxWidth: (e == null ? void 0 : e.useMaxWidth) ?? !0
  };
}, "getConfigValues"), Ht = /* @__PURE__ */ u((e, n, a, h) => {
  var m, k;
  et.debug(`Rendering Wardley map
` + e);
  const i = _t(), p = Gt(), b = i.nodeRadius * 1.6, L = h.db, f = L.getWardleyData(), X = L.getDiagramTitle(), w = ((m = f.size) == null ? void 0 : m.width) ?? i.width, $ = ((k = f.size) == null ? void 0 : k.height) ?? i.height, D = Bt(n);
  D.selectAll("*").remove(), It(D, $, w, i.useMaxWidth), D.attr("viewBox", `0 0 ${w} ${$}`);
  const P = D.append("g").attr("class", "wardley-map"), H = D.append("defs");
  H.append("marker").attr("id", `arrow-${n}`).attr("viewBox", "0 0 10 10").attr("refX", 9).attr("refY", 5).attr("markerWidth", 6).attr("markerHeight", 6).attr("orient", "auto-start-reverse").append("path").attr("d", "M 0 0 L 10 5 L 0 10 z").attr("fill", p.evolutionStroke).attr("stroke", "none"), H.append("marker").attr("id", `link-arrow-end-${n}`).attr("viewBox", "0 0 10 10").attr("refX", 9).attr("refY", 5).attr("markerWidth", 5).attr("markerHeight", 5).attr("orient", "auto").append("path").attr("d", "M 0 0 L 10 5 L 0 10 z").attr("fill", p.linkStroke).attr("stroke", "none"), H.append("marker").attr("id", `link-arrow-start-${n}`).attr("viewBox", "0 0 10 10").attr("refX", 1).attr("refY", 5).attr("markerWidth", 5).attr("markerHeight", 5).attr("orient", "auto").append("path").attr("d", "M 10 0 L 0 5 L 10 10 z").attr("fill", p.linkStroke).attr("stroke", "none"), P.append("rect").attr("class", "wardley-background").attr("width", w).attr("height", $).attr("fill", p.backgroundColor);
  const O = w - i.padding * 2, Y = $ - i.padding * 2;
  X && P.append("text").attr("class", "wardley-title").attr("x", w / 2).attr("y", i.padding / 2).attr("fill", p.axisTextColor).attr("font-size", i.axisFontSize * 1.05).attr("font-weight", "bold").attr("text-anchor", "middle").attr("dominant-baseline", "middle").text(X);
  const N = /* @__PURE__ */ u((t) => i.padding + t / 100 * O, "projectX"), T = /* @__PURE__ */ u((t) => $ - i.padding - t / 100 * Y, "projectY"), W = P.append("g").attr("class", "wardley-axes");
  W.append("line").attr("x1", i.padding).attr("x2", w - i.padding).attr("y1", $ - i.padding).attr("y2", $ - i.padding).attr("stroke", p.axisColor).attr("stroke-width", 1), W.append("line").attr("x1", i.padding).attr("x2", i.padding).attr("y1", i.padding).attr("y2", $ - i.padding).attr("stroke", p.axisColor).attr("stroke-width", 1);
  const wt = f.axes.xLabel ?? "Evolution", kt = f.axes.yLabel ?? "Visibility";
  W.append("text").attr("class", "wardley-axis-label wardley-axis-label-x").attr("x", i.padding + O / 2).attr("y", $ - i.padding / 4).attr("fill", p.axisTextColor).attr("font-size", i.axisFontSize).attr("font-weight", "bold").attr("text-anchor", "middle").text(wt), W.append("text").attr("class", "wardley-axis-label wardley-axis-label-y").attr("x", i.padding / 3).attr("y", i.padding + Y / 2).attr("fill", p.axisTextColor).attr("font-size", i.axisFontSize).attr("font-weight", "bold").attr("text-anchor", "middle").attr(
    "transform",
    `rotate(-90 ${i.padding / 3} ${i.padding + Y / 2})`
  ).text(kt);
  const F = f.axes.stages && f.axes.stages.length > 0 ? f.axes.stages : jt;
  if (F.length > 0) {
    const t = P.append("g").attr("class", "wardley-stages"), o = f.axes.stageBoundaries, r = [];
    if (o && o.length === F.length) {
      let s = 0;
      o.forEach((l) => {
        r.push({ start: s, end: l }), s = l;
      });
    } else {
      const s = 1 / F.length;
      F.forEach((l, d) => {
        r.push({
          start: d * s,
          end: (d + 1) * s
        });
      });
    }
    F.forEach((s, l) => {
      const d = r[l], g = i.padding + d.start * O, x = i.padding + d.end * O, y = (g + x) / 2;
      l > 0 && t.append("line").attr("x1", g).attr("x2", g).attr("y1", i.padding).attr("y2", $ - i.padding).attr("stroke", "#000").attr("stroke-width", 1).attr("stroke-dasharray", "5 5").attr("opacity", 0.8), t.append("text").attr("class", "wardley-stage-label").attr("x", y).attr("y", $ - i.padding / 1.5).attr("fill", p.axisTextColor).attr("font-size", i.axisFontSize - 2).attr("text-anchor", "middle").text(s);
    });
  }
  if (i.showGrid) {
    const t = P.append("g").attr("class", "wardley-grid");
    for (let o = 1; o < 4; o++) {
      const r = o / 4, s = i.padding + O * r;
      t.append("line").attr("x1", s).attr("x2", s).attr("y1", i.padding).attr("y2", $ - i.padding).attr("stroke", p.gridColor).attr("stroke-dasharray", "2 6"), t.append("line").attr("x1", i.padding).attr("x2", w - i.padding).attr("y1", $ - i.padding - Y * r).attr("y2", $ - i.padding - Y * r).attr("stroke", p.gridColor).attr("stroke-dasharray", "2 6");
    }
  }
  const c = /* @__PURE__ */ new Map();
  if (f.nodes.forEach((t) => {
    c.set(t.id, {
      x: N(t.x),
      y: T(t.y),
      node: t
    });
  }), f.pipelines.length > 0) {
    const t = P.append("g").attr("class", "wardley-pipelines"), o = P.append("g").attr("class", "wardley-pipeline-links");
    f.pipelines.forEach((r) => {
      if (r.componentIds.length === 0)
        return;
      const s = r.componentIds.map((x) => ({ id: x, pos: c.get(x), node: f.nodes.find((y) => y.id === x) })).filter((x) => x.pos && x.node).sort((x, y) => x.node.x - y.node.x);
      for (let x = 0; x < s.length - 1; x++) {
        const y = s[x], v = s[x + 1];
        o.append("line").attr("class", "wardley-pipeline-evolution-link").attr("x1", y.pos.x).attr("y1", y.pos.y).attr("x2", v.pos.x).attr("y2", v.pos.y).attr("stroke", p.linkStroke).attr("stroke-width", 1).attr("stroke-dasharray", "4 4");
      }
      let l = 1 / 0, d = -1 / 0, g = 0;
      if (r.componentIds.forEach((x) => {
        const y = c.get(x);
        y && (l = Math.min(l, y.x), d = Math.max(d, y.x), g = y.y);
      }), l !== 1 / 0 && d !== -1 / 0) {
        const x = i.nodeRadius * 4, y = g - x / 2, v = c.get(r.nodeId);
        if (v) {
          const C = (l + d) / 2;
          v.x = C, v.y = y - b / 6;
        }
        t.append("rect").attr("class", "wardley-pipeline-box").attr("x", l - 15).attr("y", y).attr("width", d - l + 30).attr("height", x).attr("fill", "none").attr("stroke", p.axisColor).attr("stroke-width", 1.5).attr("rx", 4).attr("ry", 4);
      }
    });
  }
  const U = P.append("g").attr("class", "wardley-links"), J = /* @__PURE__ */ new Map();
  f.pipelines.forEach((t) => {
    J.set(t.nodeId, new Set(t.componentIds));
  });
  const K = f.links.filter((t) => {
    if (!c.has(t.source) || !c.has(t.target))
      return !1;
    const o = J.get(t.target);
    return !(o != null && o.has(t.source));
  });
  U.selectAll("line").data(K).enter().append("line").attr("class", (t) => `wardley-link${t.dashed ? " wardley-link--dashed" : ""}`).attr("x1", (t) => {
    const o = c.get(t.source), r = c.get(t.target), s = f.nodes.find((x) => x.id === t.source).isPipelineParent ? b / Math.sqrt(2) : i.nodeRadius, l = r.x - o.x, d = r.y - o.y, g = Math.sqrt(l * l + d * d);
    return o.x + l / g * s;
  }).attr("y1", (t) => {
    const o = c.get(t.source), r = c.get(t.target), s = f.nodes.find((x) => x.id === t.source).isPipelineParent ? b / Math.sqrt(2) : i.nodeRadius, l = r.x - o.x, d = r.y - o.y, g = Math.sqrt(l * l + d * d);
    return o.y + d / g * s;
  }).attr("x2", (t) => {
    const o = c.get(t.source), r = c.get(t.target), s = f.nodes.find((x) => x.id === t.target).isPipelineParent ? b / Math.sqrt(2) : i.nodeRadius, l = o.x - r.x, d = o.y - r.y, g = Math.sqrt(l * l + d * d);
    return r.x + l / g * s;
  }).attr("y2", (t) => {
    const o = c.get(t.source), r = c.get(t.target), s = f.nodes.find((x) => x.id === t.target).isPipelineParent ? b / Math.sqrt(2) : i.nodeRadius, l = o.x - r.x, d = o.y - r.y, g = Math.sqrt(l * l + d * d);
    return r.y + d / g * s;
  }).attr("stroke", p.linkStroke).attr("stroke-width", 1).attr("stroke-dasharray", (t) => t.dashed ? "6 6" : null).attr("marker-end", (t) => t.flow === "forward" || t.flow === "bidirectional" ? `url(#link-arrow-end-${n})` : null).attr("marker-start", (t) => t.flow === "backward" || t.flow === "bidirectional" ? `url(#link-arrow-start-${n})` : null), U.selectAll("text").data(K.filter((t) => t.label)).enter().append("text").attr("class", "wardley-link-label").attr("x", (t) => {
    const o = c.get(t.source), r = c.get(t.target), s = (o.x + r.x) / 2, l = r.y - o.y, d = r.x - o.x, g = Math.sqrt(d * d + l * l), x = 8, y = l / g;
    return s + y * x;
  }).attr("y", (t) => {
    const o = c.get(t.source), r = c.get(t.target), s = (o.y + r.y) / 2, l = r.x - o.x, d = r.y - o.y, g = Math.sqrt(l * l + d * d), x = 8, y = -l / g;
    return s + y * x;
  }).attr("fill", p.axisTextColor).attr("font-size", i.labelFontSize).attr("text-anchor", "middle").attr("dominant-baseline", "middle").attr("transform", (t) => {
    const o = c.get(t.source), r = c.get(t.target), s = (o.x + r.x) / 2, l = (o.y + r.y) / 2, d = r.x - o.x, g = r.y - o.y, x = Math.sqrt(d * d + g * g), y = 8, v = g / x, C = -d / x, I = s + v * y, q = l + C * y;
    let R = Math.atan2(g, d) * 180 / Math.PI;
    return (R > 90 || R < -90) && (R += 180), `rotate(${R} ${I} ${q})`;
  }).text((t) => t.label);
  const bt = P.append("g").attr("class", "wardley-trends"), vt = f.trends.map((t) => {
    const o = c.get(t.nodeId);
    if (!o)
      return null;
    const r = N(t.targetX), s = T(t.targetY), l = r - o.x, d = s - o.y, g = Math.sqrt(l * l + d * d), x = i.nodeRadius + 2, y = g > x ? r - l / g * x : r, v = g > x ? s - d / g * x : s;
    return {
      origin: o,
      targetX: r,
      targetY: s,
      adjustedX2: y,
      adjustedY2: v
    };
  }).filter((t) => t !== null);
  bt.selectAll("line").data(vt).enter().append("line").attr("class", "wardley-trend").attr("x1", (t) => t.origin.x).attr("y1", (t) => t.origin.y).attr("x2", (t) => t.adjustedX2).attr("y2", (t) => t.adjustedY2).attr("stroke", p.evolutionStroke).attr("stroke-width", 1).attr("stroke-dasharray", "4 4").attr("marker-end", `url(#arrow-${n})`);
  const A = P.append("g").attr("class", "wardley-nodes").selectAll("g").data(f.nodes).enter().append("g").attr(
    "class",
    (t) => ["wardley-node", t.className ? `wardley-node--${t.className}` : ""].filter(Boolean).join(" ")
  );
  A.filter((t) => t.sourceStrategy === "outsource").append("circle").attr("class", "wardley-outsource-overlay").attr("cx", (t) => c.get(t.id).x).attr("cy", (t) => c.get(t.id).y).attr("r", i.nodeRadius * 2).attr("fill", "#666").attr("stroke", p.componentStroke).attr("stroke-width", 1), A.filter((t) => t.sourceStrategy === "buy").append("circle").attr("class", "wardley-buy-overlay").attr("cx", (t) => c.get(t.id).x).attr("cy", (t) => c.get(t.id).y).attr("r", i.nodeRadius * 2).attr("fill", "#ccc").attr("stroke", p.componentStroke).attr("stroke-width", 1), A.filter((t) => t.sourceStrategy === "build").append("circle").attr("class", "wardley-build-overlay").attr("cx", (t) => c.get(t.id).x).attr("cy", (t) => c.get(t.id).y).attr("r", i.nodeRadius * 2).attr("fill", "#eee").attr("stroke", "#000").attr("stroke-width", 1);
  const E = A.filter((t) => t.sourceStrategy === "market");
  E.append("circle").attr("class", "wardley-market-overlay").attr("cx", (t) => c.get(t.id).x).attr("cy", (t) => c.get(t.id).y).attr("r", i.nodeRadius * 2).attr("fill", "white").attr("stroke", p.componentStroke).attr("stroke-width", 1), A.filter(
    (t) => !t.isPipelineParent && t.sourceStrategy !== "market" && t.className !== "anchor"
  ).append("circle").attr("cx", (t) => c.get(t.id).x).attr("cy", (t) => c.get(t.id).y).attr("r", i.nodeRadius).attr("fill", p.componentFill).attr("stroke", p.componentStroke).attr("stroke-width", 1);
  const V = i.nodeRadius * 0.7, M = i.nodeRadius * 1.2;
  if (E.append("line").attr("class", "wardley-market-line").attr("x1", (t) => c.get(t.id).x).attr("y1", (t) => c.get(t.id).y - M).attr("x2", (t) => c.get(t.id).x - M * Math.cos(Math.PI / 6)).attr("y2", (t) => c.get(t.id).y + M * Math.sin(Math.PI / 6)).attr("stroke", p.componentStroke).attr("stroke-width", 1), E.append("line").attr("class", "wardley-market-line").attr("x1", (t) => c.get(t.id).x - M * Math.cos(Math.PI / 6)).attr("y1", (t) => c.get(t.id).y + M * Math.sin(Math.PI / 6)).attr("x2", (t) => c.get(t.id).x + M * Math.cos(Math.PI / 6)).attr("y2", (t) => c.get(t.id).y + M * Math.sin(Math.PI / 6)).attr("stroke", p.componentStroke).attr("stroke-width", 1), E.append("line").attr("class", "wardley-market-line").attr("x1", (t) => c.get(t.id).x + M * Math.cos(Math.PI / 6)).attr("y1", (t) => c.get(t.id).y + M * Math.sin(Math.PI / 6)).attr("x2", (t) => c.get(t.id).x).attr("y2", (t) => c.get(t.id).y - M).attr("stroke", p.componentStroke).attr("stroke-width", 1), E.append("circle").attr("class", "wardley-market-dot").attr("cx", (t) => c.get(t.id).x).attr("cy", (t) => c.get(t.id).y - M).attr("r", V).attr("fill", "white").attr("stroke", p.componentStroke).attr("stroke-width", 2), E.append("circle").attr("class", "wardley-market-dot").attr("cx", (t) => c.get(t.id).x - M * Math.cos(Math.PI / 6)).attr("cy", (t) => c.get(t.id).y + M * Math.sin(Math.PI / 6)).attr("r", V).attr("fill", "white").attr("stroke", p.componentStroke).attr("stroke-width", 2), E.append("circle").attr("class", "wardley-market-dot").attr("cx", (t) => c.get(t.id).x + M * Math.cos(Math.PI / 6)).attr("cy", (t) => c.get(t.id).y + M * Math.sin(Math.PI / 6)).attr("r", V).attr("fill", "white").attr("stroke", p.componentStroke).attr("stroke-width", 2), A.filter((t) => t.isPipelineParent === !0).append("rect").attr("x", (t) => c.get(t.id).x - b / 2).attr("y", (t) => c.get(t.id).y - b / 2).attr("width", b).attr("height", b).attr("fill", p.componentFill).attr("stroke", p.componentStroke).attr("stroke-width", 1), A.filter((t) => t.inertia === !0).append("line").attr("class", "wardley-inertia").attr("x1", (t) => {
    const o = c.get(t.id);
    let r = t.isPipelineParent ? b / 2 + 15 : i.nodeRadius + 15;
    return t.sourceStrategy && (r += i.nodeRadius + 10), o.x + r;
  }).attr("y1", (t) => {
    const o = c.get(t.id), r = t.isPipelineParent ? b : i.nodeRadius * 2;
    return o.y - r / 2;
  }).attr("x2", (t) => {
    const o = c.get(t.id);
    let r = t.isPipelineParent ? b / 2 + 15 : i.nodeRadius + 15;
    return t.sourceStrategy && (r += i.nodeRadius + 10), o.x + r;
  }).attr("y2", (t) => {
    const o = c.get(t.id), r = t.isPipelineParent ? b : i.nodeRadius * 2;
    return o.y + r / 2;
  }).attr("stroke", p.componentStroke).attr("stroke-width", 6), A.append("text").attr("x", (t) => {
    const o = c.get(t.id);
    if (t.className === "anchor")
      return t.labelOffsetX !== void 0 ? o.x + t.labelOffsetX : o.x;
    let r = i.nodeLabelOffset;
    t.sourceStrategy && t.labelOffsetX === void 0 && (r += 10);
    const s = t.labelOffsetX ?? r;
    return o.x + s;
  }).attr("y", (t) => {
    const o = c.get(t.id);
    if (t.className === "anchor")
      return t.labelOffsetY !== void 0 ? o.y + t.labelOffsetY : o.y - 3;
    let r = -i.nodeLabelOffset;
    t.sourceStrategy && t.labelOffsetY === void 0 && (r -= 10);
    const s = t.labelOffsetY ?? r;
    return o.y + s;
  }).attr("class", "wardley-node-label").attr("fill", (t) => t.className === "evolved" ? p.evolutionStroke : t.className === "anchor" ? "#000" : p.componentLabelColor).attr("font-size", i.labelFontSize).attr("font-weight", (t) => t.className === "anchor" ? "bold" : "normal").attr("text-anchor", (t) => t.className === "anchor" ? "middle" : "start").attr("dominant-baseline", (t) => t.className === "anchor" ? "middle" : "auto").text((t) => t.label), f.annotations.length > 0) {
    const t = P.append("g").attr("class", "wardley-annotations");
    if (f.annotations.forEach((o) => {
      const r = o.coordinates.map((s) => ({
        x: N(s.x),
        y: T(s.y)
      }));
      if (r.length > 1)
        for (let s = 0; s < r.length - 1; s++)
          t.append("line").attr("class", "wardley-annotation-line").attr("x1", r[s].x).attr("y1", r[s].y).attr("x2", r[s + 1].x).attr("y2", r[s + 1].y).attr("stroke", p.axisColor).attr("stroke-width", 1.5).attr("stroke-dasharray", "4 4");
      r.forEach((s) => {
        const l = t.append("g").attr("class", "wardley-annotation");
        l.append("circle").attr("cx", s.x).attr("cy", s.y).attr("r", 10).attr("fill", "white").attr("stroke", p.axisColor).attr("stroke-width", 1.5), l.append("text").attr("x", s.x).attr("y", s.y).attr("text-anchor", "middle").attr("dominant-baseline", "central").attr("font-size", 10).attr("fill", p.axisTextColor).attr("font-weight", "bold").text(o.number);
      });
    }), f.annotationsBox) {
      let o = N(f.annotationsBox.x), r = T(f.annotationsBox.y);
      const s = 10, l = 16, d = 11, g = t.append("g").attr("class", "wardley-annotations-box"), x = [...f.annotations].filter((v) => v.text).sort((v, C) => v.number - C.number), y = [];
      if (x.forEach((v, C) => {
        const I = g.append("text").attr("x", o + s).attr("y", r + s + (C + 1) * l).attr("font-size", d).attr("fill", p.axisTextColor).attr("text-anchor", "start").attr("dominant-baseline", "middle").text(`${v.number}. ${v.text}`);
        y.push(I);
      }), y.length > 0) {
        let v = 0, C = 0;
        y.forEach((Z) => {
          const j = Z.node(), Pt = j.getComputedTextLength();
          v = Math.max(v, Pt);
          const Ct = j.getBBox();
          C = Math.max(C, Ct.height);
        });
        const I = v + s * 2 + 105, q = x.length * l + s * 2 + C / 2, R = i.padding, $t = w - i.padding - I, Mt = i.padding, St = $ - i.padding - q;
        o = Math.max(R, Math.min(o, $t)), r = Math.max(Mt, Math.min(r, St)), y.forEach((Z, j) => {
          Z.attr("x", o + s).attr("y", r + s + (j + 1) * l);
        }), g.insert("rect", "text").attr("x", o).attr("y", r).attr("width", I).attr("height", q).attr("fill", "white").attr("stroke", p.axisColor).attr("stroke-width", 1.5).attr("rx", 4).attr("ry", 4);
      }
    }
  }
  if (f.notes.length > 0) {
    const t = P.append("g").attr("class", "wardley-notes");
    f.notes.forEach((o) => {
      const r = N(o.x), s = T(o.y);
      t.append("text").attr("x", r).attr("y", s).attr("text-anchor", "start").attr("font-size", 11).attr("fill", p.axisTextColor).attr("font-weight", "bold").text(o.text);
    });
  }
  if (f.accelerators.length > 0) {
    const t = P.append("g").attr("class", "wardley-accelerators");
    f.accelerators.forEach((o) => {
      const r = N(o.x), s = T(o.y), l = 60, d = 30, g = 20, x = `
        M ${r} ${s - d / 2}
        L ${r + l - g} ${s - d / 2}
        L ${r + l - g} ${s - d / 2 - 8}
        L ${r + l} ${s}
        L ${r + l - g} ${s + d / 2 + 8}
        L ${r + l - g} ${s + d / 2}
        L ${r} ${s + d / 2}
        Z
      `;
      t.append("path").attr("d", x).attr("fill", "white").attr("stroke", p.componentStroke).attr("stroke-width", 1), t.append("text").attr("x", r + l / 2).attr("y", s + d / 2 + 15).attr("text-anchor", "middle").attr("font-size", 10).attr("fill", p.axisTextColor).attr("font-weight", "bold").text(o.name);
    });
  }
  if (f.deaccelerators.length > 0) {
    const t = P.append("g").attr("class", "wardley-deaccelerators");
    f.deaccelerators.forEach((o) => {
      const r = N(o.x), s = T(o.y), l = 60, d = 30, g = 20, x = `
        M ${r + l} ${s - d / 2}
        L ${r + g} ${s - d / 2}
        L ${r + g} ${s - d / 2 - 8}
        L ${r} ${s}
        L ${r + g} ${s + d / 2 + 8}
        L ${r + g} ${s + d / 2}
        L ${r + l} ${s + d / 2}
        Z
      `;
      t.append("path").attr("d", x).attr("fill", "white").attr("stroke", p.componentStroke).attr("stroke-width", 1), t.append("text").attr("x", r + l / 2).attr("y", s + d / 2 + 15).attr("text-anchor", "middle").attr("font-size", 10).attr("fill", p.axisTextColor).attr("font-weight", "bold").text(o.name);
    });
  }
}, "draw"), Vt = {
  draw: Ht
}, Jt = {
  parser: at,
  db: qt,
  renderer: Vt,
  styles: /* @__PURE__ */ u(() => "", "styles")
};
export {
  Jt as diagram
};
