import { p as f, s as m, a0 as C, A as v, g as w, U as P, W as F, f as S, i as W, K as z, G as E, o as T, F as D, H as A } from "./index-CNUFa5l3.js";
import { t as H } from "./chunk-4BX2VUAB-C_NnimAy-9zhkU6MG.js";
import { r as G } from "./wardley-L42UT6IY-BjAkE_cs-li1xYed9.js";
var R = D.packet, u, y = (u = class {
  constructor() {
    this.packet = [], this.setAccTitle = P, this.getAccTitle = F, this.setDiagramTitle = S, this.getDiagramTitle = W, this.getAccDescription = z, this.setAccDescription = E;
  }
  getConfig() {
    let t = m({
      ...R,
      ...T().packet
    });
    return t.showBits && (t.paddingY += 10), t;
  }
  getPacket() {
    return this.packet;
  }
  pushWord(t) {
    t.length > 0 && this.packet.push(t);
  }
  clear() {
    A(), this.packet = [];
  }
}, f(u, "PacketDB"), u), Y = 1e4, K = /* @__PURE__ */ f((e, t) => {
  H(e, t);
  let i = -1, r = [], l = 1, { bitsPerRow: n } = t.getConfig();
  for (let { start: a, end: o, bits: c, label: d } of e.blocks) {
    if (a !== void 0 && o !== void 0 && o < a) throw Error(`Packet block ${a} - ${o} is invalid. End must be greater than start.`);
    if (a ?? (a = i + 1), a !== i + 1) throw Error(`Packet block ${a} - ${o ?? a} is not contiguous. It should start from ${i + 1}.`);
    if (c === 0) throw 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 && t.getPacket().length < Y; ) {
      let [p, s] = L({
        start: a,
        end: o,
        bits: c,
        label: d
      }, l, n);
      if (r.push(p), p.end + 1 === l * n && (t.pushWord(r), r = [], l++), !s) break;
      ({ start: a, end: o, bits: c, label: d } = s);
    }
  }
  t.pushWord(r);
}, "populate"), L = /* @__PURE__ */ f((e, t, i) => {
  if (e.start === void 0) throw Error("start should have been set during first phase");
  if (e.end === void 0) throw Error("end should have been set during first phase");
  if (e.start > e.end) throw Error(`Block start ${e.start} is greater than block end ${e.end}.`);
  if (e.end + 1 <= t * i) return [e, void 0];
  let r = t * i - 1, l = t * i;
  return [{
    start: e.start,
    end: r,
    label: e.label,
    bits: r - e.start
  }, {
    start: l,
    end: e.end,
    label: e.label,
    bits: e.end - l
  }];
}, "getNextFittingBlock"), x = {
  parser: { yy: void 0 },
  parse: /* @__PURE__ */ f(async (e) => {
    var r;
    let t = await G("packet", e), i = (r = x.parser) == null ? void 0 : r.yy;
    if (!(i instanceof y)) throw 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(t), K(t, i);
  }, "parse")
}, M = /* @__PURE__ */ f((e, t, i, r) => {
  let 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 = C(t);
  g.attr("viewBox", `0 0 ${k} ${h}`), v(g, h, k, n.useMaxWidth);
  for (let [$, B] of p.entries()) U(g, B, $, 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"), U = /* @__PURE__ */ f((e, t, i, { rowHeight: r, paddingX: l, paddingY: n, bitWidth: a, bitsPerRow: o, showBits: c }) => {
  let d = e.append("g"), p = i * (r + n) + n;
  for (let s of t) {
    let 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;
    let 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"), _ = { draw: M }, j = {
  byteFontSize: "10px",
  startByteColor: "black",
  endByteColor: "black",
  labelColor: "black",
  labelFontSize: "12px",
  titleColor: "black",
  titleFontSize: "14px",
  blockStrokeColor: "black",
  blockStrokeWidth: "1",
  blockFillColor: "#efefef"
}, N = {
  parser: x,
  get db() {
    return new y();
  },
  renderer: _,
  styles: /* @__PURE__ */ f(({ packet: e } = {}) => {
    let t = m(j, e);
    return `
	.packetByte {
		font-size: ${t.byteFontSize};
	}
	.packetByte.start {
		fill: ${t.startByteColor};
	}
	.packetByte.end {
		fill: ${t.endByteColor};
	}
	.packetLabel {
		fill: ${t.labelColor};
		font-size: ${t.labelFontSize};
	}
	.packetTitle {
		fill: ${t.titleColor};
		font-size: ${t.titleFontSize};
	}
	.packetBlock {
		stroke: ${t.blockStrokeColor};
		stroke-width: ${t.blockStrokeWidth};
		fill: ${t.blockFillColor};
	}
	`;
  }, "styles")
};
export {
  N as diagram
};
