import { Y as Yt, K as Kt, z as zt } from './styles-11e81fdd.esm.js';
import { V as Vn, a as L$1, b as bt, O as Ol, P as Pf, g as ei, C as C1 } from './index.esm2.js';
import { Z as Ze } from './graph-0ee63739.esm.js';
import { Y as Yt$1 } from './layout-fd473db2.esm.js';
import { P } from './line-24d93f1b.esm.js';
import './array-2ff2c7a6.esm.js';
import './path-428ebac9.esm.js';

var _ = 0;
var $ = function $(i, a, t, o, p) {
    var g = function g(e) {
        switch(e){
            case p.db.relationType.AGGREGATION:
                return "aggregation";
            case p.db.relationType.EXTENSION:
                return "extension";
            case p.db.relationType.COMPOSITION:
                return "composition";
            case p.db.relationType.DEPENDENCY:
                return "dependency";
            case p.db.relationType.LOLLIPOP:
                return "lollipop";
        }
    };
    a.points = a.points.filter(function(e) {
        return !Number.isNaN(e.y);
    });
    var s = a.points, c = P().x(function(e) {
        return e.x;
    }).y(function(e) {
        return e.y;
    }).curve(Pf), n = i.append("path").attr("d", c(s)).attr("id", "edge" + _).attr("class", "relation");
    var r = "";
    o.arrowMarkerAbsolute && (r = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search, r = r.replace(/\(/g, "\\("), r = r.replace(/\)/g, "\\)")), t.relation.lineType == 1 && n.attr("class", "relation dashed-line"), t.relation.lineType == 10 && n.attr("class", "relation dotted-line"), t.relation.type1 !== "none" && n.attr("marker-start", "url(" + r + "#" + g(t.relation.type1) + "Start)"), t.relation.type2 !== "none" && n.attr("marker-end", "url(" + r + "#" + g(t.relation.type2) + "End)");
    var f, h;
    var x = a.points.length;
    var k = ei.calcLabelPosition(a.points);
    f = k.x, h = k.y;
    var y, m, w, b;
    if (x % 2 !== 0 && x > 1) {
        var e = ei.calcCardinalityPosition(t.relation.type1 !== "none", a.points, a.points[0]), d = ei.calcCardinalityPosition(t.relation.type2 !== "none", a.points, a.points[x - 1]);
        L$1.debug("cardinality_1_point " + JSON.stringify(e)), L$1.debug("cardinality_2_point " + JSON.stringify(d)), y = e.x, m = e.y, w = d.x, b = d.y;
    }
    if (t.title !== void 0) {
        var e1 = i.append("g").attr("class", "classLabel"), d1 = e1.append("text").attr("class", "label").attr("x", f).attr("y", h).attr("fill", "red").attr("text-anchor", "middle").text(t.title);
        window.label = d1;
        var l = d1.node().getBBox();
        e1.insert("rect", ":first-child").attr("class", "box").attr("x", l.x - o.padding / 2).attr("y", l.y - o.padding / 2).attr("width", l.width + o.padding).attr("height", l.height + o.padding);
    }
    L$1.info("Rendering relation " + JSON.stringify(t)), t.relationTitle1 !== void 0 && t.relationTitle1 !== "none" && i.append("g").attr("class", "cardinality").append("text").attr("class", "type1").attr("x", y).attr("y", m).attr("fill", "black").attr("font-size", "6").text(t.relationTitle1), t.relationTitle2 !== void 0 && t.relationTitle2 !== "none" && i.append("g").attr("class", "cardinality").append("text").attr("class", "type2").attr("x", w).attr("y", b).attr("fill", "black").attr("font-size", "6").text(t.relationTitle2), _++;
}, J = function J(i, a, t, o) {
    L$1.debug("Rendering class ", a, t);
    var p = a.id, g = {
        id: p,
        label: a.id,
        width: 0,
        height: 0
    }, s = i.append("g").attr("id", o.db.lookUpDomId(p)).attr("class", "classGroup");
    var c;
    a.link ? c = s.append("svg:a").attr("xlink:href", a.link).attr("target", a.linkTarget).append("text").attr("y", t.textHeight + t.padding).attr("x", 0) : c = s.append("text").attr("y", t.textHeight + t.padding).attr("x", 0);
    var n = true;
    a.annotations.forEach(function(d) {
        var l = c.append("tspan").text("«" + d + "»");
        n || l.attr("dy", t.textHeight), n = false;
    });
    var r = C(a);
    var f = c.append("tspan").text(r).attr("class", "title");
    n || f.attr("dy", t.textHeight);
    var h = c.node().getBBox().height;
    var x, k, y;
    if (a.members.length > 0) {
        x = s.append("line").attr("x1", 0).attr("y1", t.padding + h + t.dividerMargin / 2).attr("y2", t.padding + h + t.dividerMargin / 2);
        var d = s.append("text").attr("x", t.padding).attr("y", h + t.dividerMargin + t.textHeight).attr("fill", "white").attr("class", "classText");
        n = true, a.members.forEach(function(l) {
            v(d, l, n, t), n = false;
        }), k = d.node().getBBox();
    }
    if (a.methods.length > 0) {
        y = s.append("line").attr("x1", 0).attr("y1", t.padding + h + t.dividerMargin + k.height).attr("y2", t.padding + h + t.dividerMargin + k.height);
        var d1 = s.append("text").attr("x", t.padding).attr("y", h + 2 * t.dividerMargin + k.height + t.textHeight).attr("fill", "white").attr("class", "classText");
        n = true, a.methods.forEach(function(l) {
            v(d1, l, n, t), n = false;
        });
    }
    var m = s.node().getBBox();
    var w = " ";
    a.cssClasses.length > 0 && (w = w + a.cssClasses.join(" "));
    var e = s.insert("rect", ":first-child").attr("x", 0).attr("y", 0).attr("width", m.width + 2 * t.padding).attr("height", m.height + t.padding + 0.5 * t.dividerMargin).attr("class", w).node().getBBox().width;
    return c.node().childNodes.forEach(function(d) {
        d.setAttribute("x", (e - d.getBBox().width) / 2);
    }), a.tooltip && c.insert("title").text(a.tooltip), x && x.attr("x2", e), y && y.attr("x2", e), g.width = e, g.height = m.height + t.padding + 0.5 * t.dividerMargin, g;
}, C = function C(i) {
    var a = i.id;
    return i.type && (a += "<" + C1(i.type) + ">"), a;
}, Z = function Z(i, a, t, o) {
    L$1.debug("Rendering note ", a, t);
    var p = a.id, g = {
        id: p,
        text: a.text,
        width: 0,
        height: 0
    }, s = i.append("g").attr("id", p).attr("class", "classGroup");
    var c = s.append("text").attr("y", t.textHeight + t.padding).attr("x", 0);
    var n = JSON.parse('"'.concat(a.text, '"')).split("\n");
    n.forEach(function(x) {
        L$1.debug("Adding line: ".concat(x)), c.append("tspan").text(x).attr("class", "title").attr("dy", t.textHeight);
    });
    var r = s.node().getBBox(), h = s.insert("rect", ":first-child").attr("x", 0).attr("y", 0).attr("width", r.width + 2 * t.padding).attr("height", r.height + n.length * t.textHeight + t.padding + 0.5 * t.dividerMargin).node().getBBox().width;
    return c.node().childNodes.forEach(function(x) {
        x.setAttribute("x", (h - x.getBBox().width) / 2);
    }), g.width = h, g.height = r.height + n.length * t.textHeight + t.padding + 0.5 * t.dividerMargin, g;
}, v = function v(i, a, t, o) {
    var _a_getDisplayDetails = a.getDisplayDetails(), p = _a_getDisplayDetails.displayText, g = _a_getDisplayDetails.cssStyle, s = i.append("tspan").attr("x", o.padding).text(p);
    g !== "" && s.attr("style", a.cssStyle), t || s.attr("dy", o.textHeight);
}, N = {
    getClassTitleString: C,
    drawClass: J,
    drawEdge: $,
    drawNote: Z
};
var T = {};
var E = 20, L = function L(i) {
    var a = Object.entries(T).find(function(t) {
        return t[1].label === i;
    });
    if (a) return a[0];
}, R = function R(i) {
    i.append("defs").append("marker").attr("id", "extensionStart").attr("class", "extension").attr("refX", 0).attr("refY", 7).attr("markerWidth", 190).attr("markerHeight", 240).attr("orient", "auto").append("path").attr("d", "M 1,7 L18,13 V 1 Z"), i.append("defs").append("marker").attr("id", "extensionEnd").attr("refX", 19).attr("refY", 7).attr("markerWidth", 20).attr("markerHeight", 28).attr("orient", "auto").append("path").attr("d", "M 1,1 V 13 L18,7 Z"), i.append("defs").append("marker").attr("id", "compositionStart").attr("class", "extension").attr("refX", 0).attr("refY", 7).attr("markerWidth", 190).attr("markerHeight", 240).attr("orient", "auto").append("path").attr("d", "M 18,7 L9,13 L1,7 L9,1 Z"), i.append("defs").append("marker").attr("id", "compositionEnd").attr("refX", 19).attr("refY", 7).attr("markerWidth", 20).attr("markerHeight", 28).attr("orient", "auto").append("path").attr("d", "M 18,7 L9,13 L1,7 L9,1 Z"), i.append("defs").append("marker").attr("id", "aggregationStart").attr("class", "extension").attr("refX", 0).attr("refY", 7).attr("markerWidth", 190).attr("markerHeight", 240).attr("orient", "auto").append("path").attr("d", "M 18,7 L9,13 L1,7 L9,1 Z"), i.append("defs").append("marker").attr("id", "aggregationEnd").attr("refX", 19).attr("refY", 7).attr("markerWidth", 20).attr("markerHeight", 28).attr("orient", "auto").append("path").attr("d", "M 18,7 L9,13 L1,7 L9,1 Z"), i.append("defs").append("marker").attr("id", "dependencyStart").attr("class", "extension").attr("refX", 0).attr("refY", 7).attr("markerWidth", 190).attr("markerHeight", 240).attr("orient", "auto").append("path").attr("d", "M 5,7 L9,13 L1,7 L9,1 Z"), i.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 18,7 L9,13 L14,7 L9,1 Z");
}, F = function F(i, a, t, o) {
    var p = Vn().class;
    T = {}, L$1.info("Rendering diagram " + i);
    var g = Vn().securityLevel;
    var s;
    g === "sandbox" && (s = bt("#i" + a));
    var c = g === "sandbox" ? bt(s.nodes()[0].contentDocument.body) : bt("body"), n = c.select("[id='".concat(a, "']"));
    R(n);
    var r = new Ze({
        multigraph: true
    });
    r.setGraph({
        isMultiGraph: true
    }), r.setDefaultEdgeLabel(function() {
        return {};
    });
    var f = o.db.getClasses(), h = Object.keys(f);
    var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
    try {
        for(var _iterator = h[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
            var e = _step.value;
            var d = f[e], l = N.drawClass(n, d, p, o);
            T[l.id] = l, r.setNode(l.id, l), L$1.info("Org height: " + l.height);
        }
    } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
    } finally{
        try {
            if (!_iteratorNormalCompletion && _iterator.return != null) {
                _iterator.return();
            }
        } finally{
            if (_didIteratorError) {
                throw _iteratorError;
            }
        }
    }
    o.db.getRelations().forEach(function(e) {
        L$1.info(// cspell:ignore tjoho
        "tjoho" + L(e.id1) + L(e.id2) + JSON.stringify(e)), r.setEdge(L(e.id1), L(e.id2), {
            relation: e
        }, e.title || "DEFAULT");
    }), o.db.getNotes().forEach(function(e) {
        L$1.debug("Adding note: ".concat(JSON.stringify(e)));
        var d = N.drawNote(n, e, p, o);
        T[d.id] = d, r.setNode(d.id, d), e.class && e.class in f && r.setEdge(e.id, L(e.class), {
            relation: {
                id1: e.id,
                id2: e.class,
                relation: {
                    type1: "none",
                    type2: "none",
                    lineType: 10
                }
            }
        }, "DEFAULT");
    }), Yt$1(r), r.nodes().forEach(function(e) {
        e !== void 0 && r.node(e) !== void 0 && (L$1.debug("Node " + e + ": " + JSON.stringify(r.node(e))), c.select("#" + (o.db.lookUpDomId(e) || e)).attr("transform", "translate(" + (r.node(e).x - r.node(e).width / 2) + "," + (r.node(e).y - r.node(e).height / 2) + " )"));
    }), r.edges().forEach(function(e) {
        e !== void 0 && r.edge(e) !== void 0 && (L$1.debug("Edge " + e.v + " -> " + e.w + ": " + JSON.stringify(r.edge(e))), N.drawEdge(n, r.edge(e), r.edge(e).relation, p, o));
    });
    var y = n.node().getBBox(), m = y.width + E * 2, w = y.height + E * 2;
    Ol(n, w, m, p.useMaxWidth);
    var b = "".concat(y.x - E, " ").concat(y.y - E, " ").concat(m, " ").concat(w);
    L$1.debug("viewBox ".concat(b)), n.attr("viewBox", b);
}, U = {
    draw: F
}, tt = {
    parser: zt,
    db: Kt,
    renderer: U,
    styles: Yt,
    init: function(i) {
        i.class || (i.class = {}), i.class.arrowMarkerAbsolute = i.arrowMarkerAbsolute, Kt.clear();
    }
};

export { tt as diagram };
