"use strict";

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

var _utils = require("../utils/utils");

var _reactEmotion = require("react-emotion");

var _d = require("d3");

var _propTypes = _interopRequireDefault(require("prop-types"));

var _constants = require("../utils/constants");

var _react = _interopRequireWildcard(require("react"));

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// make links css rules
var defaultLinksCssClass =
/*#__PURE__*/
(0, _reactEmotion.css)("position:relative;fill:none;stroke:", _constants.DEFAULT_COLOR_EDGES, ";");
var selectedLinksCssClass =
/*#__PURE__*/
(0, _reactEmotion.css)("position:relative;fill:none;stroke:", _constants.SELECTED_COLOR_EDGES, ";");

function diagonal(source, target) {
  return "M".concat(source.x, ",").concat(source.y, "C").concat(source.x, ",").concat((source.y + target.y) / 2, " ").concat(target.x, ",").concat((source.y + target.y) / 2, " ").concat(target.x, ",").concat(target.y);
}

function drawNodes(domNode, _ref) {
  var descendants = _ref.descendants;
  (0, _d.select)(domNode).selectAll('g.node').data(descendants, function (d) {
    return d.id;
  }).join(function (enter) {
    return enter.append('g').attr('transform', function (d) {
      if (d.parent) {
        return "translate(".concat(d.parent.x, ",").concat(d.parent.y, ")");
      }

      return "translate(".concat(d.x, ",").concat(d.y, ")");
    });
  }, function (update) {
    return update.transition().attr('transform', function (d) {
      return "translate(".concat(d.x, ",").concat(d.y, ")");
    });
  }, function (exit) {
    return exit.remove();
  }).classed('node', true).append('rect').attr('x', -_constants.NODE_WIDTH / 2).attr('y', -_constants.NODE_HEIGHT / 2).attr('width', _constants.NODE_WIDTH).attr('height', _constants.NODE_HEIGHT).attr('fill', 'none');
}

function drawLinks(domNode, _ref2) {
  var descendants = _ref2.descendants,
      links = _ref2.links,
      selectedNodeIdPath = _ref2.selectedNodeIdPath,
      edgeType = _ref2.edgeType;
  (0, _d.select)(domNode).selectAll('path').data(links, function (d) {
    d.linkClass = defaultLinksCssClass;

    if (selectedNodeIdPath.indexOf(d.target.id) !== -1) {
      d.linkClass = selectedLinksCssClass;
    }

    return d.target.id;
  }).join(function (enter) {
    return enter.append('path');
  }, function (update) {
    return update;
  }, function (exit) {
    return exit.remove();
  }).attr('stroke-width', function (d) {
    if (edgeType == 'constant') {
      return d.linkClass == selectedLinksCssClass ? _constants.DEFAULT_MINIMUM_STROKE_WIDTH + _constants.ADDITIONAL_SELECTED_STROKE_WIDTH : _constants.DEFAULT_MINIMUM_STROKE_WIDTH;
    } else if (edgeType == 'absolute' || edgeType == 'relative') {
      var branchRatio;

      if (edgeType == 'absolute') {
        branchRatio = (0, _utils.computeSamplesCount)(d.target.data) / (0, _utils.computeSamplesCount)(descendants[0].data);
      } else {
        branchRatio = (0, _utils.computeSamplesCount)(d.target.data) / (0, _utils.computeSamplesCount)(d.source.data);
      }

      var strokeWidth = _constants.DEFAULT_STROKE_WIDTH_RATIO * branchRatio < _constants.DEFAULT_MINIMUM_STROKE_WIDTH ? _constants.DEFAULT_MINIMUM_STROKE_WIDTH : _constants.DEFAULT_STROKE_WIDTH_RATIO * branchRatio;

      if (d.linkClass == selectedLinksCssClass) {
        return strokeWidth + _constants.ADDITIONAL_SELECTED_STROKE_WIDTH;
      }

      return strokeWidth;
    } else {
      throw new Error('Unexpected edgeType variable, only \'constant\', \'relative\' or \'absolute\' values are accepted.');
    }
  }).attr('d', function (d) {
    var source = {
      x: d.source.x,
      y: d.source.y + _constants.NODE_HEIGHT / 2
    };
    var target = {
      x: d.target.x,
      y: d.target.y - _constants.NODE_HEIGHT / 3
    };
    return diagonal(source, target);
  }).attr('class', function (d) {
    return "".concat(d.linkClass, " ").concat(d.linkClass == selectedLinksCssClass ? 'selected-link' : '');
  });
}

var Edges = function Edges(_ref3) {
  var selectedNodeIdPath = _ref3.selectedNodeIdPath,
      dt = _ref3.dt,
      hierarchy = _ref3.hierarchy,
      width = _ref3.width,
      height = _ref3.height,
      edgeType = _ref3.edgeType;
  var canvas = (0, _react.useRef)();
  (0, _react.useEffect)(function () {
    var descendants = hierarchy.descendants();
    var links = hierarchy.links(); // ------------ NODES ------------

    drawNodes(canvas.current, {
      descendants: descendants
    }); // ------------ LINKS ------------

    drawLinks(canvas.current, {
      links: links,
      selectedNodeIdPath: selectedNodeIdPath,
      edgeType: edgeType,
      descendants: descendants
    });
  }, [canvas, hierarchy, selectedNodeIdPath, edgeType]);
  return _react.default.createElement("svg", {
    ref: canvas,
    width: width,
    height: height
  });
};

Edges.propTypes = {
  selectedNodeIdPath: _propTypes.default.array.isRequired,
  dt: _propTypes.default.object.isRequired,
  hierarchy: _propTypes.default.object.isRequired,
  width: _propTypes.default.number,
  height: _propTypes.default.number,
  edgeType: _propTypes.default.string
};
var _default = Edges;
exports.default = _default;