text: "use strict";
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  
  var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  
  var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  
  var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  
  var _class, _temp, _initialiseProps;
  // modules
  
  //import { load } from 'opentype.js'
  
  
  var _react = require("react");
  
  var _react2 = _interopRequireDefault(_react);
  
  var _annotationHandling = require("./annotationLayerBehavior/annotationHandling");
  
  var _propTypes = require("prop-types");
  
  var _propTypes2 = _interopRequireDefault(_propTypes);
  
  var _Legend = require("./Legend");
  
  var _Legend2 = _interopRequireDefault(_Legend);
  
  var _Annotation = require("./Annotation");
  
  var _Annotation2 = _interopRequireDefault(_Annotation);
  
  var _labella = require("labella");
  
  var _labella2 = _interopRequireDefault(_labella);
  
  var _SpanOrDiv = require("./SpanOrDiv");
  
  var _SpanOrDiv2 = _interopRequireDefault(_SpanOrDiv);
  
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  
  function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
  
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  
  function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  
  function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  
  function marginOffsetFn(orient, axisSettings, marginOffset) {
    if (typeof marginOffset === "number") {
      return marginOffset;
    }
    if (axisSettings && axisSettings.find(function (d) {
      return d.props.orient === orient;
    })) {
      return 50;
    }
    return 10;
  }
  
  function adjustedAnnotationKeyMapper(d) {
    return d.props.noteData.id || d.props.noteData.x + "-" + d.props.noteData.y;
  }
  
  function noteDataWidth(noteData) {
    var charWidth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 8;
  
    var wrap = noteData.note && noteData.note.wrap || 120;
    var noteText = noteData.note.label || noteData.note.label || "";
    return Math.min(wrap, noteText.length * charWidth);
  }
  
  function noteDataHeight(noteData) {
    var charWidth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 8;
    var lineHeight = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 20;
  
    var wrap = noteData.note && noteData.note.wrap || 120;
    var text = noteData.note.label || noteData.note.title || "";
    return Math.ceil(text.length * charWidth / wrap) * lineHeight + (noteData.note.label && noteData.note.title ? lineHeight : 0);
  }
  
  var AnnotationLayer = (_temp = _class = function (_React$Component) {
    _inherits(AnnotationLayer, _React$Component);
  
    function AnnotationLayer(props) {
      _classCallCheck(this, AnnotationLayer);
  
      var _this = _possibleConstructorReturn(this, (AnnotationLayer.__proto__ || Object.getPrototypeOf(AnnotationLayer)).call(this, props));
  
      _initialiseProps.call(_this);
  
      _this.state = {
        font: undefined,
        svgAnnotations: [],
        htmlAnnotations: [],
        adjustedAnnotations: [],
        adjustedAnnotationsKey: "",
        adjustedAnnotationsDataVersion: ""
      };
      return _this;
    }
  
    /*    componentWillMount() {
        const fontLocation = this.props.fontLocation
         if (fontLocation) {
          load(fontLocation, function(err, font) {
              if (err) {
                  return null
              } else {
                  this.setState({ font });
              }
          });
        }
      } */
  
    _createClass(AnnotationLayer, [{
      key: "componentWillMount",
      value: function componentWillMount() {
        this.createAnnotations(this.props);
      }
    }, {
      key: "componentWillReceiveProps",
      value: function componentWillReceiveProps(nextProps) {
        this.createAnnotations(nextProps);
      }
    }, {
      key: "render",
      value: function render() {
        var _state = this.state,
            svgAnnotations = _state.svgAnnotations,
            htmlAnnotations = _state.htmlAnnotations;
        var _props = this.props,
            useSpans = _props.useSpans,
            legendSettings = _props.legendSettings,
            margin = _props.margin,
            size = _props.size;
  
  
        var renderedLegend = void 0;
        if (legendSettings) {
          var positionHash = {
            left: [15, 15],
            right: [size[0] + 15, 15]
          };
  
          var _legendSettings$posit = legendSettings.position,
              _position = _legendSettings$posit === undefined ? "right" : _legendSettings$posit,
              _legendSettings$title = legendSettings.title,
              _title = _legendSettings$title === undefined ? "Legend" : _legendSettings$title;
  
          var legendPosition = positionHash[_position];
          renderedLegend = _react2.default.createElement(
            "g",
            { transform: "translate(" + legendPosition.join(",") + ")" },
            _react2.default.createElement(_Legend2.default, _extends({}, legendSettings, { title: _title, position: _position }))
          );
        }
        var svgStyle = {
          background: "none",
          pointerEvents: "none",
          position: "absolute",
          left: margin.left + "px",
          top: margin.top + "px",
          overflow: "visible"
        };
  
        return _react2.default.createElement(
          _SpanOrDiv2.default,
          {
            span: useSpans,
            className: "annotation-layer",
            style: {
              position: "absolute",
              pointerEvents: "none",
              background: "none"
            }
          },
          _react2.default.createElement(
            "svg",
            {
              className: "annotation-layer-svg",
              height: size[1],
              width: size[0],
              style: svgStyle
            },
            _react2.default.createElement(
              "g",
              null,
              renderedLegend,
              svgAnnotations
            )
          ),
          _react2.default.createElement(
            _SpanOrDiv2.default,
            {
              span: useSpans,
              className: "annotation-layer-html",
              style: {
                background: "none",
                pointerEvents: "none",
                position: "absolute",
                height: size[1] + "px",
                width: size[0] + "px",
                left: margin.left + "px",
                top: margin.top + "px"
              }
            },
            htmlAnnotations
          )
        );
      }
    }]);
  
    return AnnotationLayer;
  }(_react2.default.Component), _initialiseProps = function _initialiseProps() {
    var _this2 = this;
  
    this.generateSVGAnnotations = function (props, annotations) {
      var renderedAnnotations = annotations.map(function (d, i) {
        return props.svgAnnotationRule(d, i, props);
      }).filter(function (d) {
        return d !== null && d !== undefined;
      });
  
      return renderedAnnotations;
    };
  
    this.generateHTMLAnnotations = function (props, annotations) {
      var renderedAnnotations = annotations.map(function (d, i) {
        return props.htmlAnnotationRule(d, i, props);
      }).filter(function (d) {
        return d !== null && d !== undefined;
      });
  
      return renderedAnnotations;
    };
  
    this.processAnnotations = function (adjustableAnnotations, annotationProcessor, props) {
      var _annotationProcessor$ = annotationProcessor.layout,
          layout = _annotationProcessor$ === undefined ? { type: false } : _annotationProcessor$;
  
  
      if (layout.type === false) {
        return adjustableAnnotations;
      }
  
      var _props$margin = props.margin,
          margin = _props$margin === undefined ? { top: 0, bottom: 0, left: 0, right: 0 } : _props$margin;
      var size = props.size,
          _props$axes = props.axes,
          axes = _props$axes === undefined ? [] : _props$axes;
  
  
      margin = typeof margin === "number" ? { top: margin, left: margin, right: margin, bottom: margin } : margin;
  
      if (layout.type === "bump") {
        var _adjustedAnnotations = (0, _annotationHandling.bumpAnnotations)(adjustableAnnotations, layout, size, props.pointSizeFunction, props.labelSizeFunction);
        return _adjustedAnnotations;
      } else if (layout.type === "marginalia") {
        var marginOffset = layout.marginOffset,
            _layout$orient = layout.orient,
            orient = _layout$orient === undefined ? "nearest" : _layout$orient,
            _layout$characterWidt = layout.characterWidth,
            characterWidth = _layout$characterWidt === undefined ? 8 : _layout$characterWidt,
            _layout$lineHeight = layout.lineHeight,
            lineHeight = _layout$lineHeight === undefined ? 20 : _layout$lineHeight,
            _layout$padding = layout.padding,
            padding = _layout$padding === undefined ? 2 : _layout$padding;
  
        var finalOrientation = orient === "nearest" ? ["left", "right", "top", "bottom"] : Array.isArray(orient) ? orient : [orient];
  
        var leftOn = finalOrientation.find(function (d) {
          return d === "left";
        });
        var rightOn = finalOrientation.find(function (d) {
          return d === "right";
        });
        var topOn = finalOrientation.find(function (d) {
          return d === "top";
        });
        var bottomOn = finalOrientation.find(function (d) {
          return d === "bottom";
        });
  
        var leftNodes = [];
        var rightNodes = [];
        var topNodes = [];
        var bottomNodes = [];
  
        adjustableAnnotations.forEach(function (aNote) {
          var noteData = aNote.props.noteData;
          var noteX = noteData.x[0] || noteData.x;
          var noteY = noteData.y[0] || noteData.y;
  
          var leftDist = leftOn ? noteX : Infinity;
          var rightDist = rightOn ? size[0] - noteX : Infinity;
          var topDist = topOn ? noteY : Infinity;
          var bottomDist = bottomOn ? size[1] - noteY : Infinity;
  
          var minDist = Math.min(leftDist, rightDist, topDist, bottomDist);
  
          if (leftDist === minDist) {
            leftNodes.push(aNote);
          } else if (rightDist === minDist) {
            rightNodes.push(aNote);
          } else if (topDist === minDist) {
            topNodes.push(aNote);
          } else {
            bottomNodes.push(aNote);
          }
        });
  
        //Adjust the margins based on which regions are active
  
        var leftForce = new _labella2.default.Force({
          minPos: 0 - margin.top,
          maxPos: bottomOn ? size[1] : size[1] + margin.bottom
        }).nodes(leftNodes.map(function (d) {
          var noteY = d.props.noteData.y[0] || d.props.noteData.y;
          return new _labella2.default.Node(noteY, noteDataHeight(d.props.noteData, characterWidth, lineHeight) + padding);
        })).compute();
  
        var rightForce = new _labella2.default.Force({
          minPos: topOn ? 0 : 0 - margin.top,
          maxPos: size[1] + margin.bottom
        }).nodes(rightNodes.map(function (d) {
          var noteY = d.props.noteData.y[0] || d.props.noteData.y;
          return new _labella2.default.Node(noteY, noteDataHeight(d.props.noteData, characterWidth, lineHeight) + padding);
        })).compute();
  
        var topForce = new _labella2.default.Force({
          minPos: leftOn ? 0 : 0 - margin.left,
          maxPos: size[0] + margin.right
        }).nodes(topNodes.map(function (d) {
          var noteX = d.props.noteData.x[0] || d.props.noteData.x;
          return new _labella2.default.Node(noteX, noteDataWidth(d.props.noteData, characterWidth) + padding);
        })).compute();
  
        var bottomForce = new _labella2.default.Force({
          minPos: 0 - margin.left,
          maxPos: rightOn ? size[0] : size[0] + margin.right
        }).nodes(bottomNodes.map(function (d) {
          var noteX = d.props.noteData.x[0] || d.props.noteData.x;
          return new _labella2.default.Node(noteX, noteDataWidth(d.props.noteData, characterWidth) + padding);
        })).compute();
  
        var bottomOffset = Math.max.apply(Math, _toConsumableArray(bottomNodes.map(function (d) {
          return noteDataHeight(d.props.noteData, characterWidth, lineHeight) + padding;
        })));
        var topOffset = Math.max.apply(Math, _toConsumableArray(topNodes.map(function (d) {
          return noteDataHeight(d.props.noteData, characterWidth, lineHeight) + padding;
        })));
        var leftOffset = Math.max.apply(Math, _toConsumableArray(leftNodes.map(function (d) {
          return noteDataWidth(d.props.noteData, characterWidth) + padding;
        })));
        var rightOffset = Math.max.apply(Math, _toConsumableArray(rightNodes.map(function (d) {
          return noteDataWidth(d.props.noteData, characterWidth) + padding;
        })));
  
        //      const nodeOffsetHeight = Math.max()
  
        var leftSortedNodes = leftForce.nodes();
        var rightSortedNodes = rightForce.nodes();
        var topSortedNodes = topForce.nodes();
        var bottomSortedNodes = bottomForce.nodes();
  
        leftNodes.forEach(function (note, i) {
          note.props.noteData.ny = leftSortedNodes[i].currentPos;
          note.props.noteData.nx = 0 - leftSortedNodes[i].layerIndex * leftOffset - marginOffsetFn("left", axes, marginOffset);
          if (note.props.noteData.note) {
            note.props.noteData.note.orientation = note.props.noteData.note.orientation || "leftRight";
            note.props.noteData.note.align = note.props.noteData.note.align || "right";
          }
        });
  
        rightNodes.forEach(function (note, i) {
          note.props.noteData.ny = rightSortedNodes[i].currentPos;
          note.props.noteData.nx = size[0] + rightSortedNodes[i].layerIndex * rightOffset + marginOffsetFn("right", axes, marginOffset);
          if (note.props.noteData.note) {
            note.props.noteData.note.orientation = note.props.noteData.note.orientation || "leftRight";
            note.props.noteData.note.align = note.props.noteData.note.align || "left";
          }
        });
  
        topNodes.forEach(function (note, i) {
          note.props.noteData.nx = topSortedNodes[i].currentPos;
          note.props.noteData.ny = 0 - topSortedNodes[i].layerIndex * topOffset - marginOffsetFn("top", axes, marginOffset);
        });
  
        bottomNodes.forEach(function (note, i) {
          note.props.noteData.nx = bottomSortedNodes[i].currentPos;
          note.props.noteData.ny = size[1] + bottomSortedNodes[i].layerIndex * bottomOffset + marginOffsetFn("bottom", axes, marginOffset);
        });
        return adjustableAnnotations;
      }
      return adjustableAnnotations;
    };
  
    this.createAnnotations = function (props) {
      var renderedSVGAnnotations = _this2.state.svgAnnotations,
          renderedHTMLAnnotations = [],
          adjustedAnnotations = _this2.state.adjustedAnnotations,
          adjustableAnnotationsKey = _this2.state.adjustedAnnotationsKey;
  
      var adjustedAnnotationsKey = _this2.state.adjustedAnnotationsKey,
          adjustedAnnotationsDataVersion = _this2.state.adjustedAnnotationsDataVersion;
  
      var annotations = props.annotations,
          _props$annotationHand = props.annotationHandling,
          annotationHandling = _props$annotationHand === undefined ? false : _props$annotationHand,
          size = props.size,
          svgAnnotationRule = props.svgAnnotationRule,
          htmlAnnotationRule = props.htmlAnnotationRule;
  
  
      var annotationProcessor = (typeof annotationHandling === "undefined" ? "undefined" : _typeof(annotationHandling)) === "object" ? annotationHandling : { layout: { type: annotationHandling }, dataVersion: "" };
  
      var _annotationProcessor$2 = annotationProcessor.dataVersion,
          dataVersion = _annotationProcessor$2 === undefined ? "" : _annotationProcessor$2;
  
  
      if (svgAnnotationRule) {
        var initialSVGAnnotations = _this2.generateSVGAnnotations(props, annotations);
        var adjustableAnnotations = initialSVGAnnotations.filter(function (d) {
          return d.props && d.props.noteData && !d.props.noteData.fixedPosition;
        });
        var fixedAnnotations = initialSVGAnnotations.filter(function (d) {
          return !d.props || !d.props.noteData || d.props.noteData.fixedPosition;
        });
        adjustableAnnotationsKey = "" + adjustableAnnotations.map(adjustedAnnotationKeyMapper).join(",") + JSON.stringify(annotationProcessor) + size.join(",");
  
        if (annotationHandling === false) {
          adjustedAnnotations = adjustableAnnotations;
        }
  
        if (adjustedAnnotations.length !== adjustableAnnotations.length || adjustedAnnotationsKey !== adjustableAnnotationsKey || adjustedAnnotationsDataVersion !== dataVersion) {
          adjustedAnnotations = _this2.processAnnotations(adjustableAnnotations, annotationProcessor, props);
        } else {
          //Handle when style or other attributes change
          adjustedAnnotations = adjustedAnnotations.map(function (d, i) {
            var newNoteData = _extends(adjustableAnnotations[i].props.noteData, {
              nx: d.props.noteData.nx,
              ny: d.props.noteData.ny,
              note: d.props.noteData.note
            });
            return _react2.default.createElement(_Annotation2.default, { key: d.key, noteData: newNoteData });
          });
        }
  
        renderedSVGAnnotations = [].concat(_toConsumableArray(adjustedAnnotations), _toConsumableArray(fixedAnnotations));
      }
  
      if (htmlAnnotationRule) {
        renderedHTMLAnnotations = _this2.generateHTMLAnnotations(props, annotations);
      }
  
      _this2.setState({
        svgAnnotations: renderedSVGAnnotations,
        htmlAnnotations: renderedHTMLAnnotations,
        adjustedAnnotations: adjustedAnnotations,
        adjustedAnnotationsKey: adjustableAnnotationsKey,
        adjustedAnnotationsDataVersion: dataVersion
      });
    };
  }, _temp);
  
  
  AnnotationLayer.propTypes = {
    useSpans: _propTypes2.default.bool,
    legendSettings: _propTypes2.default.object,
    margin: _propTypes2.default.object,
    size: _propTypes2.default.array,
    annotations: _propTypes2.default.array,
    annotationHandling: _propTypes2.default.object,
    pointSizeFunction: _propTypes2.default.func,
    labelSizeFunction: _propTypes2.default.func,
    svgAnnotationRule: _propTypes2.default.func,
    htmlAnnotationRule: _propTypes2.default.func,
    axes: _propTypes2.default.array
  };
  
  exports.default = AnnotationLayer;
  module.exports = exports['default'];