import { L as Le, A as Ae, D as De } from './styles-cacbff60.esm.js';
import { V as Vn, b as bt, a as L, O as Ol, $ as $n, P as Pf, g as ei } from './index.esm2.js';
import { Z as Ze } from './graph-0ee63739.esm.js';
import { Y as Yt } from './layout-fd473db2.esm.js';
import { P } from './line-24d93f1b.esm.js';
import './array-2ff2c7a6.esm.js';
import './path-428ebac9.esm.js';

var j = function(e) {
    return e.append("circle").attr("class", "start-state").attr("r", Vn().state.sizeUnit).attr("cx", Vn().state.padding + Vn().state.sizeUnit).attr("cy", Vn().state.padding + Vn().state.sizeUnit);
}, q = function(e) {
    return e.append("line").style("stroke", "grey").style("stroke-dasharray", "3").attr("x1", Vn().state.textHeight).attr("class", "divider").attr("x2", Vn().state.textHeight * 2).attr("y1", 0).attr("y2", 0);
}, Z = function(e, i) {
    var o = e.append("text").attr("x", 2 * Vn().state.padding).attr("y", Vn().state.textHeight + 2 * Vn().state.padding).attr("font-size", Vn().state.fontSize).attr("class", "state-title").text(i.id), d = o.node().getBBox();
    return e.insert("rect", ":first-child").attr("x", Vn().state.padding).attr("y", Vn().state.padding).attr("width", d.width + 2 * Vn().state.padding).attr("height", d.height + 2 * Vn().state.padding).attr("rx", Vn().state.radius), o;
}, K = function(e, i) {
    var o = function o(l, m, w) {
        var k = l.append("tspan").attr("x", 2 * Vn().state.padding).text(m);
        w || k.attr("dy", Vn().state.textHeight);
    }, s = e.append("text").attr("x", 2 * Vn().state.padding).attr("y", Vn().state.textHeight + 1.3 * Vn().state.padding).attr("font-size", Vn().state.fontSize).attr("class", "state-title").text(i.descriptions[0]).node().getBBox(), g = s.height, p = e.append("text").attr("x", Vn().state.padding).attr("y", g + Vn().state.padding * 0.4 + Vn().state.dividerMargin + Vn().state.textHeight).attr("class", "state-description");
    var a = true, r = true;
    i.descriptions.forEach(function(l) {
        a || (o(p, l, r), r = false), a = false;
    });
    var y = e.append("line").attr("x1", Vn().state.padding).attr("y1", Vn().state.padding + g + Vn().state.dividerMargin / 2).attr("y2", Vn().state.padding + g + Vn().state.dividerMargin / 2).attr("class", "descr-divider"), x = p.node().getBBox(), c = Math.max(x.width, s.width);
    return y.attr("x2", c + 3 * Vn().state.padding), e.insert("rect", ":first-child").attr("x", Vn().state.padding).attr("y", Vn().state.padding).attr("width", c + 2 * Vn().state.padding).attr("height", x.height + g + 2 * Vn().state.padding).attr("rx", Vn().state.radius), e;
}, Q = function(e, i, o) {
    var d = Vn().state.padding, s = 2 * Vn().state.padding, g = e.node().getBBox(), p = g.width, a = g.x, r = e.append("text").attr("x", 0).attr("y", Vn().state.titleShift).attr("font-size", Vn().state.fontSize).attr("class", "state-title").text(i.id), x = r.node().getBBox().width + s;
    var c = Math.max(x, p);
    c === p && (c = c + s);
    var l;
    var m = e.node().getBBox();
    i.doc, l = a - d, x > p && (l = (p - c) / 2 + d), Math.abs(a - m.x) < d && x > p && (l = a - (x - p) / 2);
    var w = 1 - Vn().state.textHeight;
    return e.insert("rect", ":first-child").attr("x", l).attr("y", w).attr("class", o ? "alt-composit" : "composit").attr("width", c).attr("height", m.height + Vn().state.textHeight + Vn().state.titleShift + 1).attr("rx", "0"), r.attr("x", l + d), x <= p && r.attr("x", a + (c - s) / 2 - x / 2 + d), e.insert("rect", ":first-child").attr("x", l).attr("y", Vn().state.titleShift - Vn().state.textHeight - Vn().state.padding).attr("width", c).attr("height", Vn().state.textHeight * 3).attr("rx", Vn().state.radius), e.insert("rect", ":first-child").attr("x", l).attr("y", Vn().state.titleShift - Vn().state.textHeight - Vn().state.padding).attr("width", c).attr("height", m.height + 3 + 2 * Vn().state.textHeight).attr("rx", Vn().state.radius), e;
}, V = function(e) {
    return e.append("circle").attr("class", "end-state-outer").attr("r", Vn().state.sizeUnit + Vn().state.miniPadding).attr("cx", Vn().state.padding + Vn().state.sizeUnit + Vn().state.miniPadding).attr("cy", Vn().state.padding + Vn().state.sizeUnit + Vn().state.miniPadding), e.append("circle").attr("class", "end-state-inner").attr("r", Vn().state.sizeUnit).attr("cx", Vn().state.padding + Vn().state.sizeUnit + 2).attr("cy", Vn().state.padding + Vn().state.sizeUnit + 2);
}, D = function(e, i) {
    var o = Vn().state.forkWidth, d = Vn().state.forkHeight;
    if (i.parentId) {
        var s = o;
        o = d, d = s;
    }
    return e.append("rect").style("stroke", "black").style("fill", "black").attr("width", o).attr("height", d).attr("x", Vn().state.padding).attr("y", Vn().state.padding);
}, tt = function(e, i, o, d) {
    var s = 0;
    var g = d.append("text");
    g.style("text-anchor", "start"), g.attr("class", "noteText");
    var p = e.replace(/\r\n/g, "<br/>");
    p = p.replace(/\n/g, "<br/>");
    var a = p.split($n.lineBreakRegex);
    var r = 1.25 * Vn().state.noteMargin;
    var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
    try {
        for(var _iterator = a[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
            var y = _step.value;
            var x = y.trim();
            if (x.length > 0) {
                var c = g.append("tspan");
                if (c.text(x), r === 0) {
                    var l = c.node().getBBox();
                    r += l.height;
                }
                s += r, c.attr("x", i + Vn().state.noteMargin), c.attr("y", o + s + 1.25 * Vn().state.noteMargin);
            }
        }
    } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
    } finally{
        try {
            if (!_iteratorNormalCompletion && _iterator.return != null) {
                _iterator.return();
            }
        } finally{
            if (_didIteratorError) {
                throw _iteratorError;
            }
        }
    }
    return {
        textWidth: g.node().getBBox().width,
        textHeight: s
    };
}, et = function(e, i) {
    i.attr("class", "state-note");
    var o = i.append("rect").attr("x", 0).attr("y", Vn().state.padding), d = i.append("g"), _tt = tt(e, 0, 0, d), s = _tt.textWidth, g = _tt.textHeight;
    return o.attr("height", g + 2 * Vn().state.noteMargin), o.attr("width", s + Vn().state.noteMargin * 2), o;
}, A = function A(e, i) {
    var o = i.id, d = {
        id: o,
        label: i.id,
        width: 0,
        height: 0
    }, s = e.append("g").attr("id", o).attr("class", "stateGroup");
    i.type === "start" && j(s), i.type === "end" && V(s), (i.type === "fork" || i.type === "join") && D(s, i), i.type === "note" && et(i.note.text, s), i.type === "divider" && q(s), i.type === "default" && i.descriptions.length === 0 && Z(s, i), i.type === "default" && i.descriptions.length > 0 && K(s, i);
    var g = s.node().getBBox();
    return d.width = g.width + 2 * Vn().state.padding, d.height = g.height + 2 * Vn().state.padding, d;
};
var G = 0;
var at = function at(e, i, o) {
    var d = function d(r) {
        switch(r){
            case Ae.relationType.AGGREGATION:
                return "aggregation";
            case Ae.relationType.EXTENSION:
                return "extension";
            case Ae.relationType.COMPOSITION:
                return "composition";
            case Ae.relationType.DEPENDENCY:
                return "dependency";
        }
    };
    i.points = i.points.filter(function(r) {
        return !Number.isNaN(r.y);
    });
    var s = i.points, g = P().x(function(r) {
        return r.x;
    }).y(function(r) {
        return r.y;
    }).curve(Pf), p = e.append("path").attr("d", g(s)).attr("id", "edge" + G).attr("class", "transition");
    var a = "";
    if (Vn().state.arrowMarkerAbsolute && (a = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search, a = a.replace(/\(/g, "\\("), a = a.replace(/\)/g, "\\)")), p.attr("marker-end", "url(" + a + "#" + d(Ae.relationType.DEPENDENCY) + "End)"), o.title !== void 0) {
        var r = e.append("g").attr("class", "stateLabel"), _F_calcLabelPosition = ei.calcLabelPosition(i.points), y = _F_calcLabelPosition.x, x = _F_calcLabelPosition.y, c = $n.getRows(o.title);
        var l = 0;
        var m = [];
        var w = 0, k = 0;
        for(var u = 0; u <= c.length; u++){
            var h = r.append("text").attr("text-anchor", "middle").text(c[u]).attr("x", y).attr("y", x + l), f = h.node().getBBox();
            w = Math.max(w, f.width), k = Math.min(k, f.x), L.info(f.x, y, x + l), l === 0 && (l = h.node().getBBox().height, L.info("Title height", l, x)), m.push(h);
        }
        var E = l * c.length;
        if (c.length > 1) {
            var u1 = (c.length - 1) * l * 0.5;
            m.forEach(function(h, f) {
                return h.attr("y", x + f * l - u1);
            }), E = l * c.length;
        }
        var n = r.node().getBBox();
        r.insert("rect", ":first-child").attr("class", "box").attr("x", y - w / 2 - Vn().state.padding / 2).attr("y", x - E / 2 - Vn().state.padding / 2 - 3.5).attr("width", w + Vn().state.padding).attr("height", E + Vn().state.padding), L.info(n);
    }
    G++;
};
var B;
var z = {}, it = function it() {}, nt = function nt(e) {
    e.append("defs").append("marker").attr("id", "dependencyEnd").attr("refX", 19).attr("refY", 7).attr("markerWidth", 20).attr("markerHeight", 28).attr("orient", "auto").append("path").attr("d", "M 19,7 L9,13 L14,7 L9,1 Z");
}, st = function st(e, i, o, d) {
    B = Vn().state;
    var s = Vn().securityLevel;
    var g;
    s === "sandbox" && (g = bt("#i" + i));
    var p = s === "sandbox" ? bt(g.nodes()[0].contentDocument.body) : bt("body"), a = s === "sandbox" ? g.nodes()[0].contentDocument : document;
    L.debug("Rendering diagram " + e);
    var r = p.select("[id='".concat(i, "']"));
    nt(r);
    var y = d.db.getRootDoc();
    W(y, r, void 0, false, p, a, d);
    var x = B.padding, c = r.node().getBBox(), l = c.width + x * 2, m = c.height + x * 2, w = l * 1.75;
    Ol(r, m, w, B.useMaxWidth), r.attr("viewBox", "".concat(c.x - B.padding, "  ").concat(c.y - B.padding, " ") + l + " " + m);
}, rt = function(e) {
    return e ? e.length * B.fontSizeFactor : 1;
}, W = function(e, i, o, d, s, g, p) {
    var a = new Ze({
        compound: true,
        multigraph: true
    });
    var r, y = true;
    for(r = 0; r < e.length; r++)if (e[r].stmt === "relation") {
        y = false;
        break;
    }
    o ? a.setGraph({
        rankdir: "LR",
        multigraph: true,
        compound: true,
        // acyclicer: 'greedy',
        ranker: "tight-tree",
        ranksep: y ? 1 : B.edgeLengthFactor,
        nodeSep: y ? 1 : 50,
        isMultiGraph: true
    }) : a.setGraph({
        rankdir: "TB",
        multigraph: true,
        compound: true,
        // isCompound: true,
        // acyclicer: 'greedy',
        // ranker: 'longest-path'
        ranksep: y ? 1 : B.edgeLengthFactor,
        nodeSep: y ? 1 : 50,
        ranker: "tight-tree",
        // ranker: 'network-simplex'
        isMultiGraph: true
    }), a.setDefaultEdgeLabel(function() {
        return {};
    }), p.db.extract(e);
    var x = p.db.getStates(), c = p.db.getRelations(), l = Object.keys(x);
    var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
    try {
        for(var _iterator = l[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
            var n = _step.value;
            var u = x[n];
            o && (u.parentId = o);
            var h = void 0;
            if (u.doc) {
                var f = i.append("g").attr("id", u.id).attr("class", "stateGroup");
                h = W(u.doc, f, u.id, !d, s, g, p);
                {
                    f = Q(f, u, d);
                    var S = f.node().getBBox();
                    h.width = S.width, h.height = S.height + B.padding / 2, z[u.id] = {
                        y: B.compositTitleSize
                    };
                }
            } else h = A(i, u);
            if (u.note) {
                var f1 = {
                    descriptions: [],
                    id: u.id + "-note",
                    note: u.note,
                    type: "note"
                }, S1 = A(i, f1);
                u.note.position === "left of" ? (a.setNode(h.id + "-note", S1), a.setNode(h.id, h)) : (a.setNode(h.id, h), a.setNode(h.id + "-note", S1)), a.setParent(h.id, h.id + "-group"), a.setParent(h.id + "-note", h.id + "-group");
            } else a.setNode(h.id, h);
        }
    } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
    } finally{
        try {
            if (!_iteratorNormalCompletion && _iterator.return != null) {
                _iterator.return();
            }
        } finally{
            if (_didIteratorError) {
                throw _iteratorError;
            }
        }
    }
    L.debug("Count=", a.nodeCount(), a);
    var m = 0;
    c.forEach(function(n) {
        m++, L.debug("Setting edge", n), a.setEdge(n.id1, n.id2, {
            relation: n,
            width: rt(n.title),
            height: B.labelHeight * $n.getRows(n.title).length,
            labelpos: "c"
        }, "id" + m);
    }), Yt(a), L.debug("Graph after layout", a.nodes());
    var w = i.node();
    a.nodes().forEach(function(n) {
        n !== void 0 && a.node(n) !== void 0 ? (L.warn("Node " + n + ": " + JSON.stringify(a.node(n))), s.select("#" + w.id + " #" + n).attr("transform", "translate(" + (a.node(n).x - a.node(n).width / 2) + "," + (a.node(n).y + (z[n] ? z[n].y : 0) - a.node(n).height / 2) + " )"), s.select("#" + w.id + " #" + n).attr("data-x-shift", a.node(n).x - a.node(n).width / 2), g.querySelectorAll("#" + w.id + " #" + n + " .divider").forEach(function(h) {
            var f = h.parentElement;
            var S = 0, M = 0;
            f && (f.parentElement && (S = f.parentElement.getBBox().width), M = parseInt(f.getAttribute("data-x-shift"), 10), Number.isNaN(M) && (M = 0)), h.setAttribute("x1", 0 - M + 8), h.setAttribute("x2", S - M - 8);
        })) : L.debug("No Node " + n + ": " + JSON.stringify(a.node(n)));
    });
    var k = w.getBBox();
    a.edges().forEach(function(n) {
        n !== void 0 && a.edge(n) !== void 0 && (L.debug("Edge " + n.v + " -> " + n.w + ": " + JSON.stringify(a.edge(n))), at(i, a.edge(n), a.edge(n).relation));
    }), k = w.getBBox();
    var E = {
        id: o || "root",
        label: o || "root",
        width: 0,
        height: 0
    };
    return E.width = k.width + 2 * B.padding, E.height = k.height + 2 * B.padding, L.debug("Doc rendered", E, a), E;
}, ot = {
    setConf: it,
    draw: st
}, ut = {
    parser: De,
    db: Ae,
    renderer: ot,
    styles: Le,
    init: function(e) {
        e.state || (e.state = {}), e.state.arrowMarkerAbsolute = e.arrowMarkerAbsolute, Ae.clear();
    }
};

export { ut as diagram };
