import { i as oe, f as le, G as se, K as de, W as me, U as xe, p as o, g, Z as R, q as ce, r as ue, H as fe, s as ge, o as B, F as he, c as y, D as P, b9 as be } from "./index-CNUFa5l3.js";
import { r as pe, U as we } from "./wardley-L42UT6IY-BjAkE_cs-li1xYed9.js";
import { t as ve } from "./chunk-4BX2VUAB-C_NnimAy-9zhkU6MG.js";
var H = "position frame", O = "frame positioned", M = "position relation", T = "relation positioned", ye = /* @__PURE__ */ o(function(e) {
  g.debug("options str", e);
}, "setOptions"), Pe = /* @__PURE__ */ o(function() {
  return {};
}, "getOptions"), Me = /* @__PURE__ */ o(function() {
  U(), fe();
}, "clear");
function U() {
  F = {};
}
o(U, "reset");
var ke = he.eventmodeling, Se = /* @__PURE__ */ o(() => ge({
  ...ke,
  ...B().eventmodeling
}), "getConfig"), F = {};
function D() {
  let e = Be, { ast: a } = F, t = $();
  if (!a) throw Error("No data for EventModel");
  return a.frames.forEach((i, n) => {
    let r = X(i, a.dataEntities, t);
    e = v(e, {
      $kind: H,
      index: n,
      frame: i,
      textProps: r
    });
    let s;
    Z(i) ? (g.debug("source frame", i.sourceFrames), s = a.frames.filter((d) => i.sourceFrames.some((m) => m.$refText === d.name)), s.forEach((d) => {
      e = v(e, {
        $kind: M,
        index: n,
        frame: i,
        sourceFrame: d
      });
    })) : e = v(e, {
      $kind: M,
      index: n,
      frame: i
    });
  }), e = {
    ...e,
    sortedSwimlanesArray: A(e.swimlanes)
  }, e;
}
o(D, "getState");
function I(e) {
  F.ast = e;
}
o(I, "setAst");
var l = {
  swimlaneMinHeight: 70,
  swimlanePadding: 15,
  swimlaneGap: 10,
  boxPadding: 10,
  boxOverlap: 90,
  boxDefaultY: 0,
  boxMinWidth: 80,
  boxMaxWidth: 450,
  boxMinHeight: 80,
  boxMaxHeight: 750,
  contentStartX: 250,
  textMaxWidth: 430,
  boxTextFontWeight: "bold",
  boxTextPadding: 10,
  swimlaneTextFontWeight: "bold",
  labelUiAutomation: "UI/Automation",
  labelUiAutomationPrefix: "UI/A: ",
  labelCommandReadModel: "Command/Read Model",
  labelCommandReadModelPrefix: "C/RM: ",
  labelEvents: "Events",
  labelEventsPrefix: "Stream: "
};
function $() {
  return l;
}
o($, "getDiagramProps");
var Be = {
  boxes: [],
  swimlanes: {},
  relations: [],
  maxR: 0,
  sortedSwimlanesArray: []
};
function C(e) {
  let a = e.split(".");
  if (a.length === 2) return a[0];
}
o(C, "extractNamespace");
function N(e) {
  let a = e.split(".");
  return a.length === 2 ? a[1] : e;
}
o(N, "extractName");
function j(e, a) {
  if (!(!a || a.length === 0)) return Object.values(e).find((t) => t.namespace === a);
}
o(j, "findSwimlaneByNamespace");
function w(e, a, t) {
  return Math.max(a, ...Object.keys(e).filter((i) => {
    let n = Number.parseInt(i);
    return n > a && n < t;
  }).map((i) => Number.parseInt(i))) + 1;
}
o(w, "findNextAvailableIndex");
function G(e, a) {
  let t = C(e.entityIdentifier), i = j(a, t);
  switch (e.modelEntityType) {
    case "ui":
    case "pcr":
    case "processor":
      return i ? {
        index: i.index,
        label: i.namespace || l.labelUiAutomation
      } : t ? {
        index: w(a, 0, 100),
        label: l.labelUiAutomationPrefix + t
      } : {
        index: 0,
        label: l.labelUiAutomation
      };
    case "rmo":
    case "readmodel":
    case "cmd":
    case "command":
      return i ? {
        index: i.index,
        label: i.namespace || l.labelCommandReadModel
      } : t ? {
        index: w(a, 100, 200),
        label: l.labelCommandReadModelPrefix + t
      } : {
        index: 100,
        label: l.labelCommandReadModel
      };
    default:
      return i ? {
        index: i.index,
        label: i.namespace || l.labelEvents
      } : t ? {
        index: w(a, 200, 300),
        label: l.labelEventsPrefix + t
      } : {
        index: 200,
        label: l.labelEvents
      };
  }
}
o(G, "calculateSwimlaneProps");
function V(e) {
  let { themeVariables: a } = B();
  switch (e.modelEntityType) {
    case "ui":
      return {
        fill: a.emUiFill ?? "white",
        stroke: a.emUiStroke ?? "#dbdada"
      };
    case "pcr":
    case "processor":
      return {
        fill: a.emProcessorFill ?? "#edb3f6",
        stroke: a.emProcessorStroke ?? "#b88cbf"
      };
    case "rmo":
    case "readmodel":
      return {
        fill: a.emReadModelFill ?? "#d3f1a2",
        stroke: a.emReadModelStroke ?? "#a3b732"
      };
    case "cmd":
    case "command":
      return {
        fill: a.emCommandFill ?? "#bcd6fe",
        stroke: a.emCommandStroke ?? "#679ac3"
      };
    case "evt":
    case "event":
      return {
        fill: a.emEventFill ?? "#ffb778",
        stroke: a.emEventStroke ?? "#c19a0f"
      };
    default:
      return {
        fill: "red",
        stroke: "black"
      };
  }
}
o(V, "calculateEntityVisualProps");
function X(e, a, t) {
  let i = B(), n = y(N(e.entityIdentifier) ?? "", i), r, s = {
    fontSize: 16,
    fontWeight: 700,
    fontFamily: '"trebuchet ms", verdana, arial, sans-serif',
    joinWith: "<br/>"
  }, d = `<b>${P(n, t.textMaxWidth, s)}</b>`;
  if (e.dataInlineValue && (r = e.dataInlineValue, r = r.substring(r.indexOf("{") + 1), r = r.substring(0, r.lastIndexOf("}") - 1), r = y(r, i), r = P(r, t.textMaxWidth, s), r = r.replaceAll(" ", "&nbsp;")), e.dataReference) {
    let f = a.find((b) => {
      var p;
      return b.name === ((p = e.dataReference) == null ? void 0 : p.$refText);
    });
    f && (r = f.dataBlockValue, r = r.substring(r.indexOf(`{
`) + 2), r = r.substring(0, r.lastIndexOf("}") - 1), r = y(r, i), r = P(r, t.textMaxWidth, s), r = r.replaceAll(" ", "&nbsp;"), r += "<br/>");
  }
  let m = r !== void 0;
  m && (d += `<br/><br/><code style="text-align: left; display: block;max-width:${t.textMaxWidth}px">${r}</code>`);
  let c = {
    fontSize: s.fontSize,
    fontWeight: s.fontWeight,
    fontFamily: s.fontFamily
  }, x = be(d, c), u = m ? x.width / 3 : x.width, h = {
    content: d,
    width: u,
    height: x.height
  };
  return g.debug(`[${e.name}] ${e.entityIdentifier} text`, h), h;
}
o(X, "calculateTextProps");
function q(e, a) {
  let t = a, i = V(t.frame), n = {
    width: t.textProps.width + 2 * l.boxTextPadding,
    height: t.textProps.height + 2 * l.boxTextPadding
  };
  return [{
    $kind: O,
    frame: t.frame,
    index: t.index,
    visual: i,
    dimension: n,
    textProps: t.textProps
  }];
}
o(q, "decidePositionFrame");
function z(e, a, t) {
  return a === void 0 ? l.contentStartX : a.index === e.index && e.r ? e.r + l.boxPadding : t === void 0 ? l.contentStartX : t.r - l.boxOverlap + l.boxPadding;
}
o(z, "calculateX");
function Y(e, a) {
  let t = [...e.map((i) => i.r), a];
  return Math.max(...t);
}
o(Y, "calculateMaxRight");
function A(e) {
  return Object.values(e).sort((a, t) => a.index - t.index);
}
o(A, "sortedSwimlanesArray");
function K(e, a) {
  let t = a, i = G(t.frame, e.swimlanes), n;
  n = i.index in e.swimlanes ? e.swimlanes[i.index] : {
    index: i.index,
    label: i.label,
    r: 0,
    y: i.index * l.swimlaneMinHeight + l.swimlaneGap,
    height: l.swimlaneMinHeight,
    maxHeight: l.swimlaneMinHeight
  };
  let r = e.boxes.length > 0 ? e.boxes[e.boxes.length - 1] : void 0, s = e.previousSwimlaneNumber === void 0 ? void 0 : e.swimlanes[e.previousSwimlaneNumber], d = {
    width: Math.max(l.boxMinWidth, Math.min(l.boxMaxWidth, t.dimension.width)) + 2 * l.boxPadding,
    height: Math.max(l.boxMinHeight, Math.min(l.boxMaxHeight, t.dimension.height)) + 2 * l.boxPadding
  }, m = z(n, s, r), c = m + d.width + l.boxPadding, x = Y(Object.values(e.swimlanes), c);
  n.r = m + d.width, n.maxHeight = Math.max(n.maxHeight, d.height), n.height = Math.max(l.swimlaneMinHeight, n.maxHeight) + 2 * l.swimlanePadding;
  let u = {
    x: m,
    y: l.swimlanePadding + n.y,
    r: c,
    dimension: d,
    leftSibling: !1,
    swimlane: n,
    visual: t.visual,
    text: t.textProps.content,
    frame: t.frame,
    index: t.index
  }, h = {
    ...e,
    boxes: [...e.boxes, u],
    swimlanes: {
      ...e.swimlanes,
      [`${n.index}`]: n
    },
    previousSwimlaneNumber: i.index,
    previousFrame: t.frame,
    maxR: x
  }, f = A(h.swimlanes);
  f.length > 0 && (f[0].y = 0);
  for (let b = 1; b < f.length; b++) {
    let p = f[b], E = f[b - 1];
    p.y = E.y + E.height + l.swimlaneGap;
  }
  return h;
}
o(K, "evolveFramePositioned");
function L(e, a) {
  return e === 0 && a.sourceFrames.length === 0;
}
o(L, "isFirstFrame");
function Z(e) {
  return e.sourceFrames !== void 0 && e.sourceFrames !== null && e.sourceFrames.length > 0;
}
o(Z, "hasSourceFrame");
function k(e, a) {
  if (a != null) return e.find((t) => t.frame.name === a.name);
}
o(k, "findBoxByFrame");
function J(e, a, t) {
  if (!(t < 0)) for (let i = t; i >= 0; i--) {
    let n = e[i];
    if (n.swimlane.index !== a) return n;
  }
}
o(J, "findBoxByLineIndex");
function Q(e, a) {
  let t = a;
  if (we(t.frame) || L(t.index, t.frame)) return [];
  let i = k(e.boxes, t.frame);
  if (i === void 0) throw Error(`Target box not found for frame ${t.frame.name}`);
  let n;
  return n = t.sourceFrame ? k(e.boxes, t.sourceFrame) : J(e.boxes, i.swimlane.index, t.index - 1), n === void 0 ? [] : [{
    $kind: T,
    frame: t.frame,
    index: t.index,
    sourceBox: n,
    targetBox: i
  }];
}
o(Q, "decidePositionRelation");
function _(e, a) {
  let t = a, i = {
    visual: {
      fill: "none",
      stroke: "#000"
    },
    source: {
      x: t.sourceBox.x,
      y: t.sourceBox.y
    },
    target: {
      x: t.targetBox.x,
      y: t.targetBox.y
    },
    sourceBox: t.sourceBox,
    targetBox: t.targetBox
  };
  return {
    ...e,
    relations: [...e.relations, i]
  };
}
o(_, "evolveRelationPositioned");
var Fe = {
  [H]: q,
  [M]: Q
}, $e = {
  [O]: K,
  [T]: _
};
function ee(e, a) {
  let t = Fe[a.$kind];
  if (t == null) return [];
  let i = t(e, a);
  return g.debug("decided events", i), i;
}
o(ee, "decide");
function te(e, a) {
  let t = a.reduce((i, n) => {
    let r = $e[n.$kind];
    return r == null ? i : r(i, n);
  }, e);
  return g.debug("evolve events", {
    state: e,
    newState: t,
    events: a
  }), t;
}
o(te, "evolve");
function v(e, a) {
  return te(e, ee(e, a));
}
o(v, "dispatch");
var S = {
  getConfig: Se,
  setOptions: ye,
  getOptions: Pe,
  clear: Me,
  setAccTitle: xe,
  getAccTitle: me,
  getAccDescription: de,
  setAccDescription: se,
  setDiagramTitle: le,
  getDiagramTitle: oe,
  setAst: I,
  getDiagramProps: $,
  getState: D
}, Ae = { parse: /* @__PURE__ */ o(async (e) => {
  let a = await pe("eventmodeling", e);
  g.debug(a), S.setAst(a), ve(a, S);
}, "parse") }, W, Ee = (W = R()) == null ? void 0 : W.eventmodeling;
function ae(e, a) {
  return (t) => {
    let i = t.swimlane.y + a.swimlanePadding, n = e.append("g").attr("class", "em-box");
    n.append("rect").attr("x", t.x).attr("y", i).attr("rx", "3").attr("width", t.dimension.width).attr("height", t.dimension.height).attr("stroke", t.visual.stroke).attr("fill", t.visual.fill), n.append("foreignObject").attr("x", t.x + a.boxPadding).attr("y", i + 10).attr("width", t.dimension.width - 2 * a.boxPadding).attr("height", t.dimension.height - 2 * a.boxPadding).append("xhtml:div").style("display", "table").style("height", "100%").style("width", "100%").append("span").style("display", "table-cell").style("text-align", "center").style("vertical-align", "middle").html(t.text);
  };
}
o(ae, "renderD3Box");
function ie(e, a) {
  return e > a;
}
o(ie, "dirUpwards");
function ne(e, a, t, i) {
  return (n) => {
    let r = n.sourceBox.swimlane.y + a.swimlanePadding, s = n.targetBox.swimlane.y + a.swimlanePadding, d = ie(r, s), m = n.sourceBox.x + n.sourceBox.dimension.width * 2 / 3, c = n.targetBox.x + n.targetBox.dimension.width / 3, x, u;
    g.debug(`rendering relation up=${d} for `, {
      sourceBox: n.sourceBox,
      targetBox: n.targetBox
    }), d ? (x = r, u = s + n.targetBox.dimension.height) : (x = r + n.sourceBox.dimension.height, u = s);
    let h = i.emRelationStroke ?? n.visual.stroke;
    e.append("path").attr("class", "em-relation").attr("fill", n.visual.fill).attr("stroke", h).attr("stroke-width", "1").attr("marker-end", `url(#${t})`).attr("d", `M${m} ${x} L${c} ${u}`);
  };
}
o(ne, "renderD3Relation");
function re(e, a, t, i) {
  return (n) => {
    let r = e.append("g").attr("class", "em-swimlane"), s = i.emSwimlaneBackgroundOdd ?? "rgb(250,250,250)", d = i.emSwimlaneBackgroundStroke ?? "rgb(240,240,240)";
    r.append("rect").attr("x", 0).attr("y", n.y).attr("rx", "3").attr("width", a + t.swimlanePadding).attr("height", n.height).attr("fill", s).attr("stroke", d), r.append("text").attr("font-weight", t.swimlaneTextFontWeight).attr("x", 30).attr("y", n.y + 30).text(n.label);
  };
}
o(re, "renderD3Swimlane");
var Oe = {
  parser: Ae,
  db: S,
  renderer: { draw: /* @__PURE__ */ o(function(e, a, t, i) {
    if (g.debug("in eventmodeling renderer", e + `
`, "id:", a, t), !Ee) throw Error("EventModeling config not found");
    let n = i.db, { themeVariables: r, eventmodeling: s } = R(), d = ce(`[id="${a}"]`), m = n.getDiagramProps(), c = n.getState(), x = `em-arrowhead-${a}`, u = r.emArrowhead ?? "#000000";
    c.sortedSwimlanesArray.forEach(re(d, c.maxR, m, r)), c.boxes.forEach(ae(d, m)), c.relations.forEach(ne(d, m, x, r)), d.append("defs").append("marker").attr("id", x).attr("markerWidth", "10").attr("markerHeight", "7").attr("refX", "10").attr("refY", "3.5").attr("orient", "auto").append("polygon").attr("points", "0 0, 10 3.5, 0 7").attr("fill", u), ue(void 0, d, (s == null ? void 0 : s.padding) ?? 30, s == null ? void 0 : s.useMaxWidth);
  }, "draw") },
  styles: /* @__PURE__ */ o((e) => "", "getStyles")
};
export {
  Oe as diagram
};
