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