"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = require("react");
require("semantic-ui-css/semantic.min.css");
var provenance_lib_core_1 = require("@visdesignlab/provenance-lib-core");
var d3_1 = require("d3");
var TreeLayout_1 = require("../Utils/TreeLayout");
var translate_1 = require("../Utils/translate");
var react_move_1 = require("react-move");
var BackboneNode_1 = require("./BackboneNode");
var Link_1 = require("./Link");
var Styles_1 = require("./Styles");
var NodeTransitions_1 = require("./NodeTransitions");
var LinkTransitions_1 = require("./LinkTransitions");
var BundleTransitions_1 = require("./BundleTransitions");
var typestyle_1 = require("typestyle");
var semantic_ui_react_1 = require("semantic-ui-react");
function ProvVis(_a) {
    var nodeMap = _a.nodeMap, _b = _a.width, width = _b === void 0 ? 1500 : _b, _c = _a.height, height = _c === void 0 ? 2000 : _c, root = _a.root, current = _a.current, changeCurrent = _a.changeCurrent, _d = _a.gutter, gutter = _d === void 0 ? 15 : _d, _e = _a.backboneGutter, backboneGutter = _e === void 0 ? 20 : _e, _f = _a.verticalSpace, verticalSpace = _f === void 0 ? 50 : _f, _g = _a.annotationHeight, annotationHeight = _g === void 0 ? 100 : _g, _h = _a.clusterVerticalSpace, clusterVerticalSpace = _h === void 0 ? 30 : _h, _j = _a.regularCircleRadius, regularCircleRadius = _j === void 0 ? 4 : _j, _k = _a.backboneCircleRadius, backboneCircleRadius = _k === void 0 ? 5 : _k, _l = _a.regularCircleStroke, regularCircleStroke = _l === void 0 ? 3 : _l, _m = _a.backboneCircleStroke, backboneCircleStroke = _m === void 0 ? 3 : _m, _o = _a.sideOffset, sideOffset = _o === void 0 ? 200 : _o, _p = _a.topOffset, topOffset = _p === void 0 ? 30 : _p, _q = _a.textSize, textSize = _q === void 0 ? 15 : _q, _r = _a.linkWidth, linkWidth = _r === void 0 ? 4 : _r, _s = _a.duration, duration = _s === void 0 ? 600 : _s, _t = _a.clusterLabels, clusterLabels = _t === void 0 ? true : _t, bundleMap = _a.bundleMap, eventConfig = _a.eventConfig, popupContent = _a.popupContent, annotationContent = _a.annotationContent;
    var _u = react_1.useState(true), first = _u[0], setFirst = _u[1];
    var _v = react_1.useState(-1), annotationOpen = _v[0], setAnnotationOpen = _v[1];
    react_1.useEffect(function () {
        setFirst(false);
    }, []);
    var nodeList = Object.values(nodeMap).filter(function (d) { return d.metadata.createdOn >= nodeMap[root].metadata.createdOn; });
    var strat = d3_1.stratify()
        .id(function (d) { return d.id; })
        .parentId(function (d) {
        if (d.id === root)
            return null;
        if (provenance_lib_core_1.isStateNode(d)) {
            return d.parent;
        }
        else {
            return null;
        }
    });
    var keys = bundleMap ? Object.keys(bundleMap) : [];
    var stratifiedTree = strat(nodeList);
    var stratifiedList = stratifiedTree.descendants();
    var stratifiedMap = {};
    // Find a list of all nodes included in a bundle.
    var bundledNodes = [];
    if (bundleMap) {
        for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
            var key = keys_1[_i];
            bundledNodes = bundledNodes.concat(bundleMap[key].bunchedNodes);
            bundledNodes.push(key);
        }
    }
    stratifiedList.forEach(function (c) { return (stratifiedMap[c.id] = c); });
    TreeLayout_1.treeLayout(stratifiedMap, current, root);
    var links = stratifiedTree.links();
    var xOffset = gutter;
    var yOffset = verticalSpace;
    function regularGlyph(node) {
        if (eventConfig) {
            var eventType = node.metadata.type;
            if (eventType && eventType in eventConfig && eventType !== 'Root') {
                return eventConfig[eventType].regularGlyph;
            }
        }
        return (react_1.default.createElement("circle", { r: regularCircleRadius, strokeWidth: regularCircleStroke, className: Styles_1.treeColor(false) }));
    }
    return (react_1.default.createElement("div", { className: container, id: "prov-vis" },
        react_1.default.createElement("svg", { height: height, width: width },
            react_1.default.createElement("rect", { height: height, width: width, fill: "none", stroke: "black" }),
            react_1.default.createElement("g", { transform: translate_1.default(width - sideOffset, topOffset) },
                react_1.default.createElement(react_move_1.NodeGroup, __assign({ data: keys, keyAccessor: function (key) { return "" + key; } }, BundleTransitions_1.default(xOffset, verticalSpace, clusterVerticalSpace, backboneGutter - gutter, duration, stratifiedMap, stratifiedList, annotationOpen, annotationHeight, bundleMap)), function (bundle) { return (react_1.default.createElement(react_1.default.Fragment, null, bundle.map(function (b) {
                    var key = b.key, state = b.state;
                    if (bundleMap === undefined ||
                        stratifiedMap[b.key].width !== 0 ||
                        state.validity === false) {
                        return;
                    }
                    return (react_1.default.createElement("g", { key: key, transform: translate_1.default(state.x - gutter, state.y - clusterVerticalSpace / 2) },
                        react_1.default.createElement("rect", { width: sideOffset - gutter, height: state.height, rx: "10", ry: "10", fill: "#F0F0F0", stroke: "none" })));
                }))); }),
                react_1.default.createElement(react_move_1.NodeGroup, __assign({ data: links, keyAccessor: function (link) { return "" + link.source.id + link.target.id; } }, LinkTransitions_1.default(xOffset, yOffset, clusterVerticalSpace, backboneGutter - gutter, duration, stratifiedList, stratifiedMap, annotationOpen, annotationHeight, bundleMap)), function (linkArr) { return (react_1.default.createElement(react_1.default.Fragment, null, linkArr.map(function (link) {
                    var key = link.key, state = link.state;
                    return (react_1.default.createElement("g", { key: key },
                        react_1.default.createElement(Link_1.default, __assign({}, state, { className: Styles_1.treeColor(true), strokeWidth: linkWidth }))));
                }))); }),
                react_1.default.createElement(react_move_1.NodeGroup, __assign({ data: stratifiedList, keyAccessor: function (d) { return d.id; } }, NodeTransitions_1.default(xOffset, yOffset, clusterVerticalSpace, backboneGutter - gutter, duration, stratifiedList, stratifiedMap, annotationOpen, annotationHeight, bundleMap)), function (nodes) {
                    return (react_1.default.createElement(react_1.default.Fragment, null, nodes.map(function (node) {
                        var d = node.data, key = node.key, state = node.state;
                        var popupTrigger = (react_1.default.createElement("g", { key: key, onClick: function () {
                                if (changeCurrent) {
                                    changeCurrent(d.id);
                                }
                            }, transform: d.width === 0 ? translate_1.default(state.x, state.y) : translate_1.default(state.x, state.y) }, d.width === 0 ? (react_1.default.createElement(BackboneNode_1.default, { textSize: textSize, radius: backboneCircleRadius, strokeWidth: backboneCircleStroke, duration: duration, first: first, current: current === d.id, node: d.data, bundleMap: bundleMap, nodeMap: stratifiedMap, clusterLabels: clusterLabels, annotationOpen: annotationOpen, setAnnotationOpen: setAnnotationOpen, eventConfig: eventConfig, annotationContent: annotationContent, popupContent: popupContent })) : popupContent !== undefined ? (react_1.default.createElement(semantic_ui_react_1.Popup, { content: popupContent(d), trigger: react_1.default.createElement("g", { onClick: function () { return setAnnotationOpen(-1); } }, regularGlyph(d.data)) })) : (react_1.default.createElement("g", { onClick: function () { return setAnnotationOpen(-1); } }, regularGlyph(d.data)))));
                        return popupTrigger;
                    })));
                })))));
}
exports.default = ProvVis;
var container = typestyle_1.style({
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center',
    overflow: 'auto'
});
//# sourceMappingURL=ProvVis.js.map