var _a;
import "./chunk-TCCFYFTB-d3HOqL2I.js";
import "./main-CThhXnXU.js";
import "./purify.es-DT70lfR0.js";
import { i as cleanAndMerge } from "./chunk-S3R3BYOJ-mQeCz5CE.js";
import { i as log, r as __name } from "./src-Bf2iLOlr.js";
import { B as setAccTitle, C as getDiagramTitle, U as setDiagramTitle, _ as getAccDescription, a as clear, c as configureSvgSize, d as defaultConfig_default, v as getAccTitle, y as getConfig, z as setAccDescription } from "./chunk-ABZYJK2D-D0cLy8Bb.js";
import { t as selectSvgElement } from "./chunk-EXTU4WIE-BKNXdLmD.js";
import "./dist-BGdYVvOu.js";
import "./chunk-UMXZTB3W-97iS1iEl.js";
import "./chunk-4F5CHEZ2-BZq7Kom7.js";
import "./chunk-FRFDVMJY-BSBUAX7r.js";
import "./chunk-SJTYNZTY-CEG4F0pB.js";
import "./chunk-PL6DKKU2-B0MTXvyc.js";
import "./chunk-TQ3KTPDO-DiCtqVSi.js";
import "./chunk-B2363JML-D9-XOau1.js";
import { t as populateCommonDb } from "./chunk-4BX2VUAB-CzXltWHN.js";
import { t as parse } from "./mermaid-parser.core-DreccfmS.js";
var DEFAULT_PACKET_CONFIG = defaultConfig_default.packet, PacketDB = (_a = class {
  constructor() {
    this.packet = [], this.setAccTitle = setAccTitle, this.getAccTitle = getAccTitle, this.setDiagramTitle = setDiagramTitle, this.getDiagramTitle = getDiagramTitle, this.getAccDescription = getAccDescription, this.setAccDescription = setAccDescription;
  }
  getConfig() {
    let d = cleanAndMerge({
      ...DEFAULT_PACKET_CONFIG,
      ...getConfig().packet
    });
    return d.showBits && (d.paddingY += 10), d;
  }
  getPacket() {
    return this.packet;
  }
  pushWord(e) {
    e.length > 0 && this.packet.push(e);
  }
  clear() {
    clear(), this.packet = [];
  }
}, __name(_a, "PacketDB"), _a), maxPacketSize = 1e4, populate = /* @__PURE__ */ __name((e, f) => {
  populateCommonDb(e, f);
  let p = -1, m = [], h = 1, { bitsPerRow: g } = f.getConfig();
  for (let { start: _, end: v, bits: y, label: b } of e.blocks) {
    if (_ !== void 0 && v !== void 0 && v < _) throw Error(`Packet block ${_} - ${v} is invalid. End must be greater than start.`);
    if (_ ?? (_ = p + 1), _ !== p + 1) throw Error(`Packet block ${_} - ${v ?? _} is not contiguous. It should start from ${p + 1}.`);
    if (y === 0) throw Error(`Packet block ${_} is invalid. Cannot have a zero bit field.`);
    for (v ?? (v = _ + (y ?? 1) - 1), y ?? (y = v - _ + 1), p = v, log.debug(`Packet block ${_} - ${p} with label ${b}`); m.length <= g + 1 && f.getPacket().length < maxPacketSize; ) {
      let [e2, d] = getNextFittingBlock({
        start: _,
        end: v,
        bits: y,
        label: b
      }, h, g);
      if (m.push(e2), e2.end + 1 === h * g && (f.pushWord(m), m = [], h++), !d) break;
      ({ start: _, end: v, bits: y, label: b } = d);
    }
  }
  f.pushWord(m);
}, "populate"), getNextFittingBlock = /* @__PURE__ */ __name((e, d, f) => {
  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 <= d * f) return [e, void 0];
  let p = d * f - 1, m = d * f;
  return [{
    start: e.start,
    end: p,
    label: e.label,
    bits: p - e.start
  }, {
    start: m,
    end: e.end,
    label: e.label,
    bits: e.end - m
  }];
}, "getNextFittingBlock"), parser = {
  parser: { yy: void 0 },
  parse: /* @__PURE__ */ __name(async (e) => {
    var _a2;
    let f = await parse("packet", e), p = (_a2 = parser.parser) == null ? void 0 : _a2.yy;
    if (!(p instanceof PacketDB)) 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.");
    log.debug(f), populate(f, p);
  }, "parse")
}, draw = /* @__PURE__ */ __name((e, d, f, p) => {
  let m = p.db, h = m.getConfig(), { rowHeight: g, paddingY: _, bitWidth: y, bitsPerRow: b } = h, x = m.getPacket(), S = m.getDiagramTitle(), C = g + _, w = C * (x.length + 1) - (S ? 0 : g), T = y * b + 2, E = selectSvgElement(d);
  E.attr("viewbox", `0 0 ${T} ${w}`), configureSvgSize(E, w, T, h.useMaxWidth);
  for (let [e2, d2] of x.entries()) drawWord(E, d2, e2, h);
  E.append("text").text(S).attr("x", T / 2).attr("y", w - C / 2).attr("dominant-baseline", "middle").attr("text-anchor", "middle").attr("class", "packetTitle");
}, "draw"), drawWord = /* @__PURE__ */ __name((e, d, f, { rowHeight: p, paddingX: m, paddingY: h, bitWidth: g, bitsPerRow: _, showBits: v }) => {
  let y = e.append("g"), b = f * (p + h) + h;
  for (let e2 of d) {
    let d2 = e2.start % _ * g + 1, f2 = (e2.end - e2.start + 1) * g - m;
    if (y.append("rect").attr("x", d2).attr("y", b).attr("width", f2).attr("height", p).attr("class", "packetBlock"), y.append("text").attr("x", d2 + f2 / 2).attr("y", b + p / 2).attr("class", "packetLabel").attr("dominant-baseline", "middle").attr("text-anchor", "middle").text(e2.label), !v) continue;
    let h2 = e2.end === e2.start, x = b - 2;
    y.append("text").attr("x", d2 + (h2 ? f2 / 2 : 0)).attr("y", x).attr("class", "packetByte start").attr("dominant-baseline", "auto").attr("text-anchor", h2 ? "middle" : "start").text(e2.start), h2 || y.append("text").attr("x", d2 + f2).attr("y", x).attr("class", "packetByte end").attr("dominant-baseline", "auto").attr("text-anchor", "end").text(e2.end);
  }
}, "drawWord"), renderer = { draw }, defaultPacketStyleOptions = {
  byteFontSize: "10px",
  startByteColor: "black",
  endByteColor: "black",
  labelColor: "black",
  labelFontSize: "12px",
  titleColor: "black",
  titleFontSize: "14px",
  blockStrokeColor: "black",
  blockStrokeWidth: "1",
  blockFillColor: "#efefef"
}, diagram = {
  parser,
  get db() {
    return new PacketDB();
  },
  renderer,
  styles: /* @__PURE__ */ __name(({ packet: d } = {}) => {
    let f = cleanAndMerge(defaultPacketStyleOptions, d);
    return `
	.packetByte {
		font-size: ${f.byteFontSize};
	}
	.packetByte.start {
		fill: ${f.startByteColor};
	}
	.packetByte.end {
		fill: ${f.endByteColor};
	}
	.packetLabel {
		fill: ${f.labelColor};
		font-size: ${f.labelFontSize};
	}
	.packetTitle {
		fill: ${f.titleColor};
		font-size: ${f.titleFontSize};
	}
	.packetBlock {
		stroke: ${f.blockStrokeColor};
		stroke-width: ${f.blockStrokeWidth};
		fill: ${f.blockFillColor};
	}
	`;
  }, "styles")
};
export {
  diagram
};
