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