import { L as Le, A as Ae, D as De, C as Ce, E as Et$1, t as te, I as It, _ as _t$1 } from './styles-cacbff60.esm.js';
import { Z as Ze } from './graph-0ee63739.esm.js';
import { g as ei, O as Ol, a as L, V as Vn, b as bt$1, $ as $n } from './index.esm2.js';
import { b as bt$2 } from './index-bb6d8841.esm.js';
import './layout-fd473db2.esm.js';
import './clone-afc2f047.esm.js';
import './edges-16357fde.esm.js';
import './createText-03b82060.esm.js';
import './line-24d93f1b.esm.js';
import './array-2ff2c7a6.esm.js';
import './path-428ebac9.esm.js';

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
    try {
        var info = gen[key](arg);
        var value = info.value;
    } catch (error) {
        reject(error);
        return;
    }
    if (info.done) {
        resolve(value);
    } else {
        Promise.resolve(value).then(_next, _throw);
    }
}
function _async_to_generator(fn) {
    return function() {
        var self = this, args = arguments;
        return new Promise(function(resolve, reject) {
            var gen = fn.apply(self, args);
            function _next(value) {
                asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
            }
            function _throw(err) {
                asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
            }
            _next(undefined);
        });
    };
}
function _ts_generator(thisArg, body) {
    var f, y, t, g, _ = {
        label: 0,
        sent: function() {
            if (t[0] & 1) throw t[1];
            return t[1];
        },
        trys: [],
        ops: []
    };
    return g = {
        next: verb(0),
        "throw": verb(1),
        "return": verb(2)
    }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
        return this;
    }), g;
    function verb(n) {
        return function(v) {
            return step([
                n,
                v
            ]);
        };
    }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while(_)try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [
                op[0] & 2,
                t.value
            ];
            switch(op[0]){
                case 0:
                case 1:
                    t = op;
                    break;
                case 4:
                    _.label++;
                    return {
                        value: op[1],
                        done: false
                    };
                case 5:
                    _.label++;
                    y = op[1];
                    op = [
                        0
                    ];
                    continue;
                case 7:
                    op = _.ops.pop();
                    _.trys.pop();
                    continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
                        _ = 0;
                        continue;
                    }
                    if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
                        _.label = op[1];
                        break;
                    }
                    if (op[0] === 6 && _.label < t[1]) {
                        _.label = t[1];
                        t = op;
                        break;
                    }
                    if (t && _.label < t[2]) {
                        _.label = t[2];
                        _.ops.push(op);
                        break;
                    }
                    if (t[2]) _.ops.pop();
                    _.trys.pop();
                    continue;
            }
            op = body.call(thisArg, _);
        } catch (e) {
            op = [
                6,
                e
            ];
            y = 0;
        } finally{
            f = t = 0;
        }
        if (op[0] & 5) throw op[1];
        return {
            value: op[0] ? op[1] : void 0,
            done: true
        };
    }
}
var h = "rect", C = "rectWithTitle", nt = "start", ct = "end", it = "divider", rt = "roundedWithTitle", lt = "note", at = "noteGroup", _ = "statediagram", dt = "state", Et = "".concat(_, "-").concat(dt), U = "transition", St = "note", Tt = "note-edge", pt = "".concat(U, " ").concat(Tt), _t = "".concat(_, "-").concat(St), ut = "cluster", ft = "".concat(_, "-").concat(ut), Dt = "cluster-alt", bt = "".concat(_, "-").concat(Dt), V = "parent", Y = "note", At = "state", N = "----", ht = "".concat(N).concat(Y), v = "".concat(N).concat(V), W = "fill:none", m = "fill: #333", z = "c", j = "text", q = "normal";
var y = {}, d = 0;
var yt = function yt(t) {
    var n = Object.keys(t);
    var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
    try {
        for(var _iterator = n[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
            var e = _step.value;
            t[e];
        }
    } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
    } finally{
        try {
            if (!_iteratorNormalCompletion && _iterator.return != null) {
                _iterator.return();
            }
        } finally{
            if (_didIteratorError) {
                throw _iteratorError;
            }
        }
    }
}, gt = function gt(t, n) {
    return n.db.extract(n.db.getRootDocV2()), n.db.getClasses();
};
function xt(t) {
    return t == null ? "" : t.classes ? t.classes.join(" ") : "";
}
function R() {
    var t = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : "", n = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 0, e = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : "", c = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : N;
    var i = e !== null && e.length > 0 ? "".concat(c).concat(e) : "";
    return "".concat(At, "-").concat(t).concat(i, "-").concat(n);
}
var A = function(t, n, e, c, i, r) {
    var o = e.id, u = xt(c[o]);
    if (o !== "root") {
        var T = h;
        e.start === true && (T = nt), e.start === false && (T = ct), e.type !== Et$1 && (T = e.type), y[o] || (y[o] = {
            id: o,
            shape: T,
            description: $n.sanitizeText(o, Vn()),
            classes: "".concat(u, " ").concat(Et)
        });
        var s = y[o];
        e.description && (Array.isArray(s.description) ? (s.shape = C, s.description.push(e.description)) : s.description.length > 0 ? (s.shape = C, s.description === o ? s.description = [
            e.description
        ] : s.description = [
            s.description,
            e.description
        ]) : (s.shape = h, s.description = e.description), s.description = $n.sanitizeTextOrArray(s.description, Vn())), s.description.length === 1 && s.shape === C && (s.shape = h), !s.type && e.doc && (L.info("Setting cluster for ", o, G(e)), s.type = "group", s.dir = G(e), s.shape = e.type === te ? it : rt, s.classes = s.classes + " " + ft + " " + (r ? bt : ""));
        var p = {
            labelStyle: "",
            shape: s.shape,
            labelText: s.description,
            // typeof newNode.description === 'object'
            //   ? newNode.description[0]
            //   : newNode.description,
            classes: s.classes,
            style: "",
            //styles.style,
            id: o,
            dir: s.dir,
            domId: R(o, d),
            type: s.type,
            padding: 15
        };
        if (p.centerLabel = true, e.note) {
            var l = {
                labelStyle: "",
                shape: lt,
                labelText: e.note.text,
                classes: _t,
                // useHtmlLabels: false,
                style: "",
                // styles.style,
                id: o + ht + "-" + d,
                domId: R(o, d, Y),
                type: s.type,
                padding: 15
            }, a = {
                labelStyle: "",
                shape: at,
                labelText: e.note.text,
                classes: s.classes,
                style: "",
                // styles.style,
                id: o + v,
                domId: R(o, d, V),
                type: "group",
                padding: 0
            };
            d++;
            var f = o + v;
            t.setNode(f, a), t.setNode(l.id, l), t.setNode(o, p), t.setParent(o, f), t.setParent(l.id, f);
            var S = o, D = l.id;
            e.note.position === "left of" && (S = l.id, D = o), t.setEdge(S, D, {
                arrowhead: "none",
                arrowType: "",
                style: W,
                labelStyle: "",
                classes: pt,
                arrowheadStyle: m,
                labelpos: z,
                labelType: j,
                thickness: q
            });
        } else t.setNode(o, p);
    }
    n && n.id !== "root" && (L.trace("Setting node ", o, " to be child of its parent ", n.id), t.setParent(o, n.id)), e.doc && (L.trace("Adding nodes children "), $t(t, e, e.doc, c, i, !r));
}, $t = function(t, n, e, c, i, r) {
    L.trace("items", e), e.forEach(function(o) {
        switch(o.stmt){
            case _t$1:
                A(t, n, o, c, i, r);
                break;
            case Et$1:
                A(t, n, o, c, i, r);
                break;
            case It:
                {
                    A(t, n, o.state1, c, i, r), A(t, n, o.state2, c, i, r);
                    var u = {
                        id: "edge" + d,
                        arrowhead: "normal",
                        arrowTypeEnd: "arrow_barb",
                        style: W,
                        labelStyle: "",
                        label: $n.sanitizeText(o.description, Vn()),
                        arrowheadStyle: m,
                        labelpos: z,
                        labelType: j,
                        thickness: q,
                        classes: U
                    };
                    t.setEdge(o.state1.id, o.state2.id, u, d), d++;
                }
                break;
        }
    });
}, G = function(t) {
    var n = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : Ce;
    var e = n;
    if (t.doc) for(var c = 0; c < t.doc.length; c++){
        var i = t.doc[c];
        i.stmt === "dir" && (e = i.value);
    }
    return e;
}, Ct = function() {
    var _ref = _async_to_generator(function(t, n, e, c) {
        var _g, i, r, o, u, T, s, p, l, a, f, S, D, L$1, P, O, k, K, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, x, B, b;
        return _ts_generator(this, function(_state) {
            switch(_state.label){
                case 0:
                    L.info("Drawing state diagram (v2)", n), y = {}, c.db.getDirection();
                    _g = Vn(), i = _g.securityLevel, r = _g.state, o = r.nodeSpacing || 50, u = r.rankSpacing || 50;
                    L.info(c.db.getRootDocV2()), c.db.extract(c.db.getRootDocV2()), L.info(c.db.getRootDocV2());
                    T = c.db.getStates(), s = new Ze({
                        multigraph: true,
                        compound: true
                    }).setGraph({
                        rankdir: G(c.db.getRootDocV2()),
                        nodesep: o,
                        ranksep: u,
                        marginx: 8,
                        marginy: 8
                    }).setDefaultEdgeLabel(function() {
                        return {};
                    });
                    A(s, void 0, c.db.getRootDocV2(), T, c.db, true);
                    i === "sandbox" && (p = bt$1("#i" + n));
                    l = i === "sandbox" ? bt$1(p.nodes()[0].contentDocument.body) : bt$1("body"), a = l.select('[id="'.concat(n, '"]')), f = l.select("#" + n + " g");
                    return [
                        4,
                        bt$2(f, s, [
                            "barb"
                        ], _, n)
                    ];
                case 1:
                    _state.sent();
                    S = 8;
                    ei.insertTitle(a, "statediagramTitleText", r.titleTopMargin, c.db.getDiagramTitle());
                    D = a.node().getBBox(), L$1 = D.width + S * 2, P = D.height + S * 2;
                    a.attr("class", _);
                    O = a.node().getBBox();
                    Ol(a, P, L$1, r.useMaxWidth);
                    k = "".concat(O.x - S, " ").concat(O.y - S, " ").concat(L$1, " ").concat(P);
                    L.debug("viewBox ".concat(k)), a.attr("viewBox", k);
                    K = document.querySelectorAll('[id="' + n + '"] .edgeLabel .label');
                    _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
                    try {
                        for(_iterator = K[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
                            x = _step.value;
                            B = x.getBBox(), b = document.createElementNS("http://www.w3.org/2000/svg", h);
                            b.setAttribute("rx", 0), b.setAttribute("ry", 0), b.setAttribute("width", B.width), b.setAttribute("height", B.height), x.insertBefore(b, x.firstChild);
                        }
                    } catch (err) {
                        _didIteratorError = true;
                        _iteratorError = err;
                    } finally{
                        try {
                            if (!_iteratorNormalCompletion && _iterator.return != null) {
                                _iterator.return();
                            }
                        } finally{
                            if (_didIteratorError) {
                                throw _iteratorError;
                            }
                        }
                    }
                    return [
                        2
                    ];
            }
        });
    });
    return function Ct(t, n, e, c) {
        return _ref.apply(this, arguments);
    };
}(), Rt = {
    setConf: yt,
    getClasses: gt,
    draw: Ct
}, Ut = {
    parser: De,
    db: Ae,
    renderer: Rt,
    styles: Le,
    init: function(t) {
        t.state || (t.state = {}), t.state.arrowMarkerAbsolute = t.arrowMarkerAbsolute, Ae.clear();
    }
};

export { Ut as diagram };
