"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.toRadians = exports.toDegrees = exports.hypotenuse = exports.getOpposingSide = exports.edges = exports.diffEdge = exports.angle = exports.acuteYAngle = exports.acuteXAngle = void 0;

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

var _pointGeometry = _interopRequireDefault(require("@mapbox/point-geometry"));

var _debug = _interopRequireDefault(require("debug"));

var log = (0, _debug["default"])('pie-lib:plot:trig');

var toDegrees = function toDegrees(radians) {
  return radians * (180 / Math.PI);
};

exports.toDegrees = toDegrees;

var toRadians = function toRadians(degrees) {
  return degrees * (Math.PI / 180);
};
/**
 * return angle in radians between 2 points using counting degrees counter clockwise
 *
 * 0,0 + 1,1 = 45 in radians
 * 1,1 + 0,0 = 45?
 * @param {Point} a
 * @param {Point} b
 */


exports.toRadians = toRadians;

var angle = function angle(a, b) {
  var vd = b.y - a.y;
  var hd = b.x - a.x;
  log(a, b, 'vd: ', vd, 'hd: ', hd);
  var radians = Math.atan2(vd, hd);
  return radians < 0 ? radians + Math.PI * 2 : radians;
};

exports.angle = angle;
var NINETY = Math.PI / 2;
var ONE_EIGHTY = Math.PI;
var TWO_SEVENTY = ONE_EIGHTY + NINETY;

var acuteXAngle = function acuteXAngle(a) {
  log(toDegrees(a));

  if (a < NINETY) {
    return a;
  }

  if (a < ONE_EIGHTY) {
    return Math.abs(ONE_EIGHTY - a);
  }

  if (a < TWO_SEVENTY) {
    return Math.abs(ONE_EIGHTY - a);
  }

  return Math.abs(Math.PI * 2 - a);
};

exports.acuteXAngle = acuteXAngle;

var acuteYAngle = function acuteYAngle(a) {
  return NINETY - acuteXAngle(a);
};

exports.acuteYAngle = acuteYAngle;

var hypotenuse = function hypotenuse(a, alpha) {
  var out = Math.abs(a / Math.sin(alpha));
  return out;
};
/**
 * return 2 edge points for a,b within domain + range.
 * - one edge is from following a -> b to the bounds
 * - one edge is from following b -> a to the bounds
 * @param {{min: number, max: number}} domain
 * @param {{min: number, max: number}} range
 * @param {{x: number, y: number}} a
 * @param {{x: number, y: number}} b
 * @returns [{x: number, y: number}, {x: number, y: number}]
 */


exports.hypotenuse = hypotenuse;

var edges = function edges(domain, range) {
  return function (a, b) {
    // const xDest =
    var destX = a.x < b.x ? domain.max : domain.min;
    var destY = a.y < b.y ? range.max : range.min;
    var aToB = diffEdge((0, _utils.xy)(destX, destY), a, b);
    var dX = b.x < a.x ? domain.max : domain.min;
    var dY = b.y < a.y ? range.max : range.min;
    var bToA = diffEdge((0, _utils.xy)(dX, dY), b, a);
    return [aToB, bToA];
  };
};
/** get length of side A of a triangle from H and angle Alpha */


exports.edges = edges;

var getOpposingSide = function getOpposingSide(hyp, angle) {
  log('[getOpposingSide] hyp: ', hyp, 'angle:', angle);
  return Math.abs(hyp * Math.sin(angle));
};

exports.getOpposingSide = getOpposingSide;

var getShortestSide = function getShortestSide(xh, yh) {
  if (Number.isFinite(xh) && Number.isFinite(yh)) {
    if (xh === 0 && yh > 0) {
      return 'y';
    }

    if (yh === 0 && xh > 0) {
      return 'x';
    }

    return xh < yh ? 'x' : 'y';
  }

  if (isNaN(xh) && !isNaN(yh)) {
    return 'y';
  }

  if (!isNaN(xh) && isNaN(yh)) {
    return 'x';
  }

  if (xh === Infinity) {
    return 'y';
  }

  if (yh === Infinity) {
    return 'x';
  } // eslint-disable-next-line no-console


  console.warn('hypotenuse - which is shorter? x:', xh, 'y:', yh);
};
/**
 * return the difference between bounds and a as a Point
 * @param {*} bounds
 */


var diffEdge = function diffEdge(bounds, a, b) {
  var l = log.enabled ? log.bind(log, "diffEdge: [".concat(a.x, ",").concat(a.y, " -> ").concat(b.x, ",").concat(b.y, "]")) : function () {};
  var xRadians = angle(a, b);
  l('x angle', toDegrees(xRadians));
  var yRadians = Math.abs(xRadians - toRadians(90));
  l('y angle', toDegrees(yRadians));
  var xSide = Math.abs(a.x - bounds.x);
  /**
   * Draw 2 triangles:
   * 1 with a horizontal line from a to the graph x edge
   * 1 with a vertical line from a to the graph y edge
   * Calculate the hypotenuse for both, whichever is shorter
   * indicates that we should use that triangle to get the edge point.
   */

  var xH = hypotenuse(xSide, yRadians);
  var ySide = Math.abs(a.y - bounds.y);
  var yH = hypotenuse(ySide, xRadians);
  l('x: side', xSide, 'h:', xH);
  l('y: side', ySide, 'h:', yH);
  var side = getShortestSide(xH, yH);

  if (side !== 'x' && side !== 'y') {
    throw new Error('Cant decide which hypotenuse to use');
  }

  var point = side === 'x' ? new _pointGeometry["default"](xSide, getOpposingSide(xH, xRadians)) : new _pointGeometry["default"](getOpposingSide(yH, yRadians), ySide);
  l('point:', point);
  var multiplier = new _pointGeometry["default"](b.x < a.x ? -1 : 1, b.y < a.y ? -1 : 1);
  l('multiplier:', multiplier);
  var out = point.multByPoint(multiplier);
  l('out:', out);
  var normalized = out.add(new _pointGeometry["default"](a.x, a.y));
  l('normalized:', normalized);
  return normalized;
};

exports.diffEdge = diffEdge;
//# sourceMappingURL=trig.js.map