import { m as v } from "./chunk-ANTBXLJU-CgMY7K4E-BDFVHt4k.js";
import { _ as f, aA as m, aU as B, aa as S, l as w, a8 as P, a7 as T, an as z, ao as D, a6 as A, a5 as E, aB as F, aC as W, at as M } from "./index-DeBlJN4f.js";
import { N as Y } from "./mermaid-parser.core-DRkTy7co-D3k_lOFL.js";
var _ = F.packet, u, y = (u = class {
  constructor() {
    this.packet = [], this.setAccTitle = P, this.getAccTitle = T, this.setDiagramTitle = z, this.getDiagramTitle = D, this.getAccDescription = A, this.setAccDescription = E;
  }
  getConfig() {
    const t = m({
      ..._,
      ...W().packet
    });
    return t.showBits && (t.paddingY += 10), t;
  }
  getPacket() {
    return this.packet;
  }
  pushWord(t) {
    t.length > 0 && this.packet.push(t);
  }
  clear() {
    M(), this.packet = [];
  }
}, f(u, "PacketDB"), u), H = 1e4, L = /* @__PURE__ */ f((t, e) => {
  v(t, e);
  let i = -1, r = [], l = 1;
  const { bitsPerRow: n } = e.getConfig();
  for (let { start: a, end: o, bits: c, label: d } of t.blocks) {
    if (a !== void 0 && o !== void 0 && o < a)
      throw new Error(`Packet block ${a} - ${o} is invalid. End must be greater than start.`);
    if (a ?? (a = i + 1), a !== i + 1)
      throw new Error(
        `Packet block ${a} - ${o ?? a} is not contiguous. It should start from ${i + 1}.`
      );
    if (c === 0)
      throw new Error(`Packet block ${a} is invalid. Cannot have a zero bit field.`);
    for (o ?? (o = a + (c ?? 1) - 1), c ?? (c = o - a + 1), i = o, w.debug(`Packet block ${a} - ${i} with label ${d}`); r.length <= n + 1 && e.getPacket().length < H; ) {
      const [p, s] = N({ start: a, end: o, bits: c, label: d }, l, n);
      if (r.push(p), p.end + 1 === l * n && (e.pushWord(r), r = [], l++), !s)
        break;
      ({ start: a, end: o, bits: c, label: d } = s);
    }
  }
  e.pushWord(r);
}, "populate"), N = /* @__PURE__ */ f((t, e, i) => {
  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 * i)
    return [t, void 0];
  const r = e * i - 1, l = e * i;
  return [
    {
      start: t.start,
      end: r,
      label: t.label,
      bits: r - t.start
    },
    {
      start: l,
      end: t.end,
      label: t.label,
      bits: t.end - l
    }
  ];
}, "getNextFittingBlock"), x = {
  // @ts-expect-error - PacketDB is not assignable to DiagramDB
  parser: { yy: void 0 },
  parse: /* @__PURE__ */ f(async (t) => {
    var e;
    const i = await Y("packet", t), r = (e = x.parser) == null ? void 0 : e.yy;
    if (!(r instanceof y))
      throw new Error(
        "parser.parser?.yy was not a PacketDB. This is due to a bug within Mermaid, please report this issue at https://github.com/mermaid-js/mermaid/issues."
      );
    w.debug(i), L(i, r);
  }, "parse")
}, R = /* @__PURE__ */ f((t, e, i, r) => {
  const l = r.db, n = l.getConfig(), { rowHeight: a, paddingY: o, bitWidth: c, bitsPerRow: d } = n, p = l.getPacket(), s = l.getDiagramTitle(), b = a + o, h = b * (p.length + 1) - (s ? 0 : a), k = c * d + 2, g = B(e);
  g.attr("viewbox", `0 0 ${k} ${h}`), S(g, h, k, n.useMaxWidth);
  for (const [$, C] of p.entries())
    j(g, C, $, n);
  g.append("text").text(s).attr("x", k / 2).attr("y", h - b / 2).attr("dominant-baseline", "middle").attr("text-anchor", "middle").attr("class", "packetTitle");
}, "draw"), j = /* @__PURE__ */ f((t, e, i, { rowHeight: r, paddingX: l, paddingY: n, bitWidth: a, bitsPerRow: o, showBits: c }) => {
  const d = t.append("g"), p = i * (r + n) + n;
  for (const s of e) {
    const b = s.start % o * a + 1, h = (s.end - s.start + 1) * a - l;
    if (d.append("rect").attr("x", b).attr("y", p).attr("width", h).attr("height", r).attr("class", "packetBlock"), d.append("text").attr("x", b + h / 2).attr("y", p + r / 2).attr("class", "packetLabel").attr("dominant-baseline", "middle").attr("text-anchor", "middle").text(s.label), !c)
      continue;
    const k = s.end === s.start, g = p - 2;
    d.append("text").attr("x", b + (k ? h / 2 : 0)).attr("y", g).attr("class", "packetByte start").attr("dominant-baseline", "auto").attr("text-anchor", k ? "middle" : "start").text(s.start), k || d.append("text").attr("x", b + h).attr("y", g).attr("class", "packetByte end").attr("dominant-baseline", "auto").attr("text-anchor", "end").text(s.end);
  }
}, "drawWord"), I = { draw: R }, X = {
  byteFontSize: "10px",
  startByteColor: "black",
  endByteColor: "black",
  labelColor: "black",
  labelFontSize: "12px",
  titleColor: "black",
  titleFontSize: "14px",
  blockStrokeColor: "black",
  blockStrokeWidth: "1",
  blockFillColor: "#efefef"
}, q = /* @__PURE__ */ f(({ packet: t } = {}) => {
  const e = m(X, 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"), J = {
  parser: x,
  get db() {
    return new y();
  },
  renderer: I,
  styles: q
};
export {
  J as diagram
};
