text: "use strict";
  var __assign = (this && this.__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;
  };
  Object.defineProperty(exports, "__esModule", { value: true });
  var utils_1 = require("./utils");
  var mitt_1 = require("./mitt");
  /**
   * Provenance Graph implementation
   *
   * @param version The version of the software to track the provenance of
   *
   */
  var ProvenanceGraph = /** @class */ (function () {
      function ProvenanceGraph(application, userid, rootNode) {
          if (userid === void 0) { userid = 'Unknown'; }
          this._nodes = {};
          this._mitt = mitt_1.default();
          this.application = application;
          if (rootNode) {
              this.root = rootNode;
          }
          else {
              this.root = {
                  id: utils_1.generateUUID(),
                  label: 'Root',
                  metadata: {
                      createdBy: userid,
                      createdOn: utils_1.generateTimestamp()
                  },
                  children: [],
                  artifacts: {}
              };
          }
          this.addNode(this.root);
          this._current = this.root;
      }
      ProvenanceGraph.prototype.addNode = function (node) {
          if (this._nodes[node.id]) {
              throw new Error('Node already added');
          }
          this._nodes[node.id] = node;
          this._mitt.emit('nodeAdded', node);
      };
      ProvenanceGraph.prototype.getNode = function (id) {
          var result = this._nodes[id];
          if (!result) {
              throw new Error('Node id not found');
          }
          return this._nodes[id];
      };
      Object.defineProperty(ProvenanceGraph.prototype, "current", {
          get: function () {
              return this._current;
          },
          set: function (node) {
              if (!this._nodes[node.id]) {
                  throw new Error('Node id not found');
              }
              this._current = node;
              this._mitt.emit('currentChanged', node);
          },
          enumerable: true,
          configurable: true
      });
      Object.defineProperty(ProvenanceGraph.prototype, "nodes", {
          get: function () {
              return this._nodes;
          },
          enumerable: true,
          configurable: true
      });
      ProvenanceGraph.prototype.emitNodeChangedEvent = function (node) {
          /* istanbul ignore if */
          if (!this._nodes[node.id]) {
              throw new Error('Node id not found');
          }
          this._mitt.emit('nodeChanged', node);
      };
      ProvenanceGraph.prototype.on = function (type, handler) {
          this._mitt.on(type, handler);
      };
      ProvenanceGraph.prototype.off = function (type, handler) {
          this._mitt.off(type, handler);
      };
      return ProvenanceGraph;
  }());
  exports.ProvenanceGraph = ProvenanceGraph;
  /* Beware that deeply nested properties in serializedProvenanceGraph is mutated in the process */
  function restoreProvenanceGraph(serializedProvenanceGraph) {
      var nodes = {};
      // restore nodes as key value
      for (var _i = 0, _a = serializedProvenanceGraph.nodes; _i < _a.length; _i++) {
          var node = _a[_i];
          nodes[node.id] = __assign({}, node);
      }
      // restore parent/children relations
      for (var _b = 0, _c = Object.keys(nodes); _b < _c.length; _b++) {
          var nodeId = _c[_b];
          var node = nodes[nodeId];
          node.children = node.children.map(function (id) { return nodes[id]; });
          if ('parent' in node) {
              node.parent = nodes[node.parent];
          }
      }
      var graph = new ProvenanceGraph(serializedProvenanceGraph.application);
      graph._nodes = nodes;
      graph._current = nodes[serializedProvenanceGraph.current];
      graph.root = nodes[serializedProvenanceGraph.root];
      return graph;
  }
  exports.restoreProvenanceGraph = restoreProvenanceGraph;
  function serializeProvenanceGraph(graph) {
      var nodes = Object.keys(graph.nodes).map(function (nodeId) {
          var node = graph.getNode(nodeId);
          var serializedNode = __assign({}, node);
          if (utils_1.isStateNode(node)) {
              serializedNode.parent = node.parent.id;
          }
          serializedNode.children = node.children.map(function (child) { return child.id; });
          return serializedNode;
      });
      return {
          nodes: nodes,
          root: graph.root.id,
          application: graph.application,
          current: graph.current.id
      };
  }
  exports.serializeProvenanceGraph = serializeProvenanceGraph;
  //# sourceMappingURL=ProvenanceGraph.js.map