"use strict";

exports.__esModule = true;
exports["default"] = drawDirectedPiePiece;
var _path = _interopRequireDefault(require("paths-js/path"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function polarToSpecialCartesian(radius, angleInRadians) {
  //the 0 angle returns the 0,1 point on the unit circle instead of the 1,0 point like normal
  return {
    x: radius * Math.cos(angleInRadians - Math.PI / 2),
    y: radius * Math.sin(angleInRadians - Math.PI / 2)
  };
}
var stickOutThisMuch = 0.03;

// draws a directed piece of the pie with an arrowhead, starts at 0 angle, only draws in one direction (use transforms to move it around the )
function drawDirectedPiePiece(_ref) {
  var _ref$tailThickness = _ref.tailThickness,
    tailThickness = _ref$tailThickness === void 0 ? 0.6 : _ref$tailThickness,
    radius = _ref.radius,
    arrowheadType = _ref.arrowheadType,
    overlapsSelf = _ref.overlapsSelf,
    annotationHeight = _ref.annotationHeight,
    totalAngle = _ref.totalAngle,
    hasLabel = _ref.hasLabel,
    labelNeedsFlip = _ref.labelNeedsFlip,
    returnTextPath = _ref.returnTextPath;
  var tailHeight = annotationHeight * tailThickness;
  var arrowheadOuterRadius = radius + annotationHeight / 2;
  var arrowheadInnerRadius = radius - annotationHeight / 2;
  var tailOuterRadius = radius + tailHeight / 2;
  var tailInnerRadius = radius - tailHeight / 2;

  // var arrowheadAngle = totalAngle / 2
  var arrowheadAngle = 80 / radius / (Math.PI * 2);
  if (totalAngle < arrowheadAngle) {
    //set arrowhead length to the angle in radians length
    arrowheadAngle = totalAngle;
  }
  if (arrowheadType === "NONE") arrowheadAngle = 0;
  var arcAngle = totalAngle - arrowheadAngle;

  //the main points we need to draw the arrow and in the order we draw them in:
  var arrowheadPoint = polarToSpecialCartesian(radius, 0);
  var arrowheadBottom = polarToSpecialCartesian(arrowheadInnerRadius, arrowheadAngle);
  var arcLeftBottom = polarToSpecialCartesian(tailInnerRadius, arrowheadAngle);
  var arcRightBottom = polarToSpecialCartesian(tailInnerRadius, totalAngle);
  var arcRightTop = polarToSpecialCartesian(tailOuterRadius, totalAngle);
  var arcRightMiddle = polarToSpecialCartesian(radius, totalAngle);
  var arcRightMiddleOuter = polarToSpecialCartesian(radius, totalAngle + stickOutThisMuch);
  var arcLeftTop = polarToSpecialCartesian(tailOuterRadius, arrowheadAngle);
  var arrowheadTop = polarToSpecialCartesian(arrowheadOuterRadius, arrowheadAngle);
  var largeArcFlag = arcAngle > Math.PI ? 1 : 0;
  var path = (0, _path["default"])().moveto(arrowheadPoint.x, arrowheadPoint.y);
  if (overlapsSelf) {
    var arrowheadPointInner = polarToSpecialCartesian(radius, -stickOutThisMuch);
    path = path.lineto(arrowheadPointInner.x, arrowheadPointInner.y).lineto(arrowheadPoint.x, arrowheadPoint.y);
  }
  path = path.lineto(arrowheadBottom.x, arrowheadBottom.y).lineto(arcLeftBottom.x, arcLeftBottom.y).arc({
    rx: tailInnerRadius,
    ry: tailInnerRadius,
    xrot: 0,
    largeArcFlag: largeArcFlag,
    sweepFlag: 1,
    x: arcRightBottom.x,
    y: arcRightBottom.y
  });
  if (overlapsSelf) {
    path = path.lineto(arcRightMiddle.x, arcRightMiddle.y).lineto(arcRightMiddleOuter.x, arcRightMiddleOuter.y).lineto(arcRightMiddle.x, arcRightMiddle.y);
  }
  var textPath;
  if (returnTextPath && hasLabel) {
    if (labelNeedsFlip) {
      var _arcLeftTop = polarToSpecialCartesian(tailOuterRadius, 0);
      textPath = (0, _path["default"])().moveto(arcRightTop.x, arcRightTop.y).arc({
        rx: tailOuterRadius,
        ry: tailOuterRadius,
        xrot: 0,
        largeArcFlag: largeArcFlag,
        sweepFlag: 0,
        x: _arcLeftTop.x,
        y: _arcLeftTop.y
      });
    } else {
      var _arcLeftBottom = polarToSpecialCartesian(tailInnerRadius, 0);
      textPath = (0, _path["default"])().moveto(_arcLeftBottom.x, _arcLeftBottom.y).arc({
        rx: tailInnerRadius,
        ry: tailInnerRadius,
        xrot: 0,
        largeArcFlag: largeArcFlag,
        sweepFlag: 1,
        x: arcRightBottom.x,
        y: arcRightBottom.y
      });
    }
  }
  path = path.lineto(arcRightTop.x, arcRightTop.y).arc({
    rx: tailOuterRadius,
    ry: tailOuterRadius,
    xrot: 0,
    largeArcFlag: largeArcFlag,
    sweepFlag: 0,
    x: arcLeftTop.x,
    y: arcLeftTop.y
  }).lineto(arrowheadTop.x, arrowheadTop.y).closepath();
  if (returnTextPath) {
    return [path, textPath];
  }
  return path;
}
module.exports = exports.default;