"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _util = require("@antv/util");

var _default = {
  getDefaultCfg: function getDefaultCfg() {
    return {
      multiple: true,
      selectedState: 'selected'
    };
  },
  getEvents: function getEvents() {
    var self = this;
    return {
      'node:tap': 'onClick',
      'combo:tap': 'onClick',
      'canvas:tap': 'onCanvasClick'
    };
  },
  onClick: function onClick(evt) {
    var self = this;
    var item = evt.item;

    if (!item || item.destroyed) {
      return;
    }

    var type = item.getType();
    var graph = self.graph,
        multiple = self.multiple,
        shouldUpdate = self.shouldUpdate,
        shouldBegin = self.shouldBegin;

    if (!shouldBegin.call(self, evt)) {
      return;
    } // allow to select multiple nodes but did not press a key || do not allow the select multiple nodes


    if (!multiple) {
      var selected = graph.findAllByState(type, self.selectedState);
      (0, _util.each)(selected, function (combo) {
        if (combo !== item) {
          graph.setItemState(combo, self.selectedState, false);
        }
      });
    }

    if (item.hasState(self.selectedState)) {
      if (shouldUpdate.call(self, evt)) {
        graph.setItemState(item, self.selectedState, false);
      }

      var selectedNodes = graph.findAllByState('node', self.selectedState);
      var selectedCombos = graph.findAllByState('combo', self.selectedState);
      graph.emit('nodeselectchange', {
        target: item,
        selectedItems: {
          nodes: selectedNodes,
          combos: selectedCombos
        },
        select: false
      });
    } else {
      if (shouldUpdate.call(self, evt)) {
        graph.setItemState(item, self.selectedState, true);
      }

      var selectedNodes = graph.findAllByState('node', self.selectedState);
      var selectedCombos = graph.findAllByState('combo', self.selectedState);
      graph.emit('nodeselectchange', {
        target: item,
        selectedItems: {
          nodes: selectedNodes,
          combos: selectedCombos
        },
        select: true
      });
    }
  },
  onCanvasClick: function onCanvasClick() {
    var _this = this;

    var graph = this.graph;
    var selected = graph.findAllByState('node', this.selectedState);
    (0, _util.each)(selected, function (node) {
      graph.setItemState(node, _this.selectedState, false);
    });
    var selectedCombos = graph.findAllByState('combo', this.selectedState);
    (0, _util.each)(selectedCombos, function (combo) {
      graph.setItemState(combo, _this.selectedState, false);
    });
    graph.emit('nodeselectchange', {
      selectedItems: {
        nodes: [],
        edges: [],
        combos: []
      },
      select: false
    });
  }
};
exports.default = _default;