"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _default = {
  getDefaultCfg: function getDefaultCfg() {
    return {
      activeState: 'active',
      inactiveState: 'inactive',
      resetSelected: false,
      shouldUpdate: function shouldUpdate() {
        return true;
      }
    };
  },
  getEvents: function getEvents() {
    return {
      'node:tap': 'setAllItemStates',
      'canvas:tap': 'clearAllItemStates'
    };
  },
  setAllItemStates: function setAllItemStates(e) {
    var self = this;
    var item = e.item;
    var graph = self.graph;
    self.item = item;

    if (!self.shouldUpdate(e.item, {
      event: e,
      action: 'activate'
    })) {
      return;
    }

    var activeState = self.activeState;
    var inactiveState = self.inactiveState;
    var nodes = graph.getNodes();
    var edges = graph.getEdges();
    var nodeLength = nodes.length;
    var edgeLength = edges.length;

    for (var i = 0; i < nodeLength; i++) {
      var node = nodes[i];
      var hasSelected = node.hasState('selected');

      if (self.resetSelected) {
        if (hasSelected) {
          graph.setItemState(node, 'selected', false);
        }
      }

      graph.setItemState(node, activeState, false);

      if (inactiveState) {
        graph.setItemState(node, inactiveState, true);
      }
    }

    for (var i = 0; i < edgeLength; i++) {
      var edge = edges[i];
      graph.setItemState(edge, activeState, false);

      if (inactiveState) {
        graph.setItemState(edge, inactiveState, true);
      }
    }

    if (inactiveState) {
      graph.setItemState(item, inactiveState, false);
    }

    graph.setItemState(item, activeState, true);
    var rEdges = item.getEdges();
    var rEdgeLegnth = rEdges.length;

    for (var i = 0; i < rEdgeLegnth; i++) {
      var edge = rEdges[i];
      var otherEnd = void 0;

      if (edge.getSource() === item) {
        otherEnd = edge.getTarget();
      } else {
        otherEnd = edge.getSource();
      }

      if (inactiveState) {
        graph.setItemState(otherEnd, inactiveState, false);
      }

      graph.setItemState(otherEnd, activeState, true);
      graph.setItemState(edge, inactiveState, false);
      graph.setItemState(edge, activeState, true);
      edge.toFront();
    }

    graph.emit('afteractivaterelations', {
      item: e.item,
      action: 'activate'
    });
  },
  clearActiveState: function clearActiveState(e) {
    var self = this;
    var graph = self.get('graph');

    if (!self.shouldUpdate(e.item, {
      event: e,
      action: 'deactivate'
    })) {
      return;
    }

    var activeState = this.activeState;
    var inactiveState = this.inactiveState;
    var autoPaint = graph.get('autoPaint');
    graph.setAutoPaint(false);
    var nodes = graph.getNodes();
    var edges = graph.getEdges();
    var nodeLength = nodes.length;
    var edgeLength = edges.length;

    for (var i = 0; i < nodeLength; i++) {
      var node = nodes[i];
      graph.clearItemStates(node, [activeState, inactiveState]);
    }

    for (var i = 0; i < edgeLength; i++) {
      var edge = edges[i];
      graph.clearItemStates(edge, [activeState, inactiveState, 'deactivate']);
    }

    graph.paint();
    graph.setAutoPaint(autoPaint);
    graph.emit('afteractivaterelations', {
      item: e.item || self.get('item'),
      action: 'deactivate'
    });
  },
  clearAllItemStates: function clearAllItemStates(e) {
    var self = this;
    var graph = self.graph;

    if (!self.shouldUpdate(e.item, {
      event: e,
      action: 'deactivate'
    })) {
      return;
    }

    var activeState = this.activeState;
    var inactiveState = this.inactiveState;
    var nodes = graph.getNodes();
    var edges = graph.getEdges();
    var nodeLength = nodes.length;
    var edgeLength = edges.length;

    for (var i = 0; i < nodeLength; i++) {
      var node = nodes[i];
      graph.clearItemStates(node, [activeState, inactiveState]);
    }

    for (var i = 0; i < edgeLength; i++) {
      var edge = edges[i];
      graph.clearItemStates(edge, [activeState, inactiveState, 'deactivate']);
    }

    graph.emit('afteractivaterelations', {
      item: e.item || self.get('item'),
      action: 'deactivate'
    });
  }
};
exports.default = _default;