import { m as B } from "./chunk-TMUBEWPD-BRIzn5gH-alZ9Sg3f.js";
import { _ as i, a5 as S, a6 as D, aj as T, ak as W, a7 as z, a8 as P, aC as x, aW as A, aa as F, aq as v, aD as E, aE as R, l as $ } from "./index-CMZig9xg.js";
import { D as Y } from "./mermaid-parser.core-Ce96PX3Q-CV9ue_xr.js";
var C = {
  packet: []
}, m = structuredClone(C), _ = R.packet, H = /* @__PURE__ */ i(() => {
  const t = x({
    ..._,
    ...E().packet
  });
  return t.showBits && (t.paddingY += 10), t;
}, "getConfig"), L = /* @__PURE__ */ i(() => m.packet, "getPacket"), M = /* @__PURE__ */ i((t) => {
  t.length > 0 && m.packet.push(t);
}, "pushWord"), j = /* @__PURE__ */ i(() => {
  v(), m = structuredClone(C);
}, "clear"), u = {
  pushWord: M,
  getPacket: L,
  getConfig: H,
  clear: j,
  setAccTitle: P,
  getAccTitle: z,
  setDiagramTitle: W,
  getDiagramTitle: T,
  getAccDescription: D,
  setAccDescription: S
}, q = 1e4, I = /* @__PURE__ */ i((t) => {
  B(t, u);
  let e = -1, o = [], s = 1;
  const { bitsPerRow: n } = u.getConfig();
  for (let { start: a, end: r, label: p } of t.blocks) {
    if (r && r < a)
      throw new Error(`Packet block ${a} - ${r} is invalid. End must be greater than start.`);
    if (a !== e + 1)
      throw new Error(
        `Packet block ${a} - ${r ?? a} is not contiguous. It should start from ${e + 1}.`
      );
    for (e = r ?? a, $.debug(`Packet block ${a} - ${e} with label ${p}`); o.length <= n + 1 && u.getPacket().length < q; ) {
      const [f, c] = X({ start: a, end: r, label: p }, s, n);
      if (o.push(f), f.end + 1 === s * n && (u.pushWord(o), o = [], s++), !c)
        break;
      ({ start: a, end: r, label: p } = c);
    }
  }
  u.pushWord(o);
}, "populate"), X = /* @__PURE__ */ i((t, e, o) => {
  if (t.end === void 0 && (t.end = t.start), t.start > t.end)
    throw new Error(`Block start ${t.start} is greater than block end ${t.end}.`);
  return t.end + 1 <= e * o ? [t, void 0] : [
    {
      start: t.start,
      end: e * o - 1,
      label: t.label
    },
    {
      start: e * o,
      end: t.end,
      label: t.label
    }
  ];
}, "getNextFittingBlock"), G = {
  parse: /* @__PURE__ */ i(async (t) => {
    const e = await Y("packet", t);
    $.debug(e), I(e);
  }, "parse")
}, J = /* @__PURE__ */ i((t, e, o, s) => {
  const n = s.db, a = n.getConfig(), { rowHeight: r, paddingY: p, bitWidth: f, bitsPerRow: c } = a, h = n.getPacket(), l = n.getDiagramTitle(), g = r + p, d = g * (h.length + 1) - (l ? 0 : r), k = f * c + 2, b = A(e);
  b.attr("viewbox", `0 0 ${k} ${d}`), F(b, d, k, a.useMaxWidth);
  for (const [w, y] of h.entries())
    K(b, y, w, a);
  b.append("text").text(l).attr("x", k / 2).attr("y", d - g / 2).attr("dominant-baseline", "middle").attr("text-anchor", "middle").attr("class", "packetTitle");
}, "draw"), K = /* @__PURE__ */ i((t, e, o, { rowHeight: s, paddingX: n, paddingY: a, bitWidth: r, bitsPerRow: p, showBits: f }) => {
  const c = t.append("g"), h = o * (s + a) + a;
  for (const l of e) {
    const g = l.start % p * r + 1, d = (l.end - l.start + 1) * r - n;
    if (c.append("rect").attr("x", g).attr("y", h).attr("width", d).attr("height", s).attr("class", "packetBlock"), c.append("text").attr("x", g + d / 2).attr("y", h + s / 2).attr("class", "packetLabel").attr("dominant-baseline", "middle").attr("text-anchor", "middle").text(l.label), !f)
      continue;
    const k = l.end === l.start, b = h - 2;
    c.append("text").attr("x", g + (k ? d / 2 : 0)).attr("y", b).attr("class", "packetByte start").attr("dominant-baseline", "auto").attr("text-anchor", k ? "middle" : "start").text(l.start), k || c.append("text").attr("x", g + d).attr("y", b).attr("class", "packetByte end").attr("dominant-baseline", "auto").attr("text-anchor", "end").text(l.end);
  }
}, "drawWord"), N = { draw: J }, O = {
  byteFontSize: "10px",
  startByteColor: "black",
  endByteColor: "black",
  labelColor: "black",
  labelFontSize: "12px",
  titleColor: "black",
  titleFontSize: "14px",
  blockStrokeColor: "black",
  blockStrokeWidth: "1",
  blockFillColor: "#efefef"
}, U = /* @__PURE__ */ i(({ packet: t } = {}) => {
  const e = x(O, t);
  return `
	.packetByte {
		font-size: ${e.byteFontSize};
	}
	.packetByte.start {
		fill: ${e.startByteColor};
	}
	.packetByte.end {
		fill: ${e.endByteColor};
	}
	.packetLabel {
		fill: ${e.labelColor};
		font-size: ${e.labelFontSize};
	}
	.packetTitle {
		fill: ${e.titleColor};
		font-size: ${e.titleFontSize};
	}
	.packetBlock {
		stroke: ${e.blockStrokeColor};
		stroke-width: ${e.blockStrokeWidth};
		fill: ${e.blockFillColor};
	}
	`;
}, "styles"), tt = {
  parser: G,
  db: u,
  renderer: N,
  styles: U
};
export {
  tt as diagram
};
