"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var POSE_LANDMARKS = {
  NOSE: 0,
  LEFT_EYE_INNER: 1,
  LEFT_EYE: 2,
  LEFT_EYE_OUTER: 3,
  RIGHT_EYE_INNER: 4,
  RIGHT_EYE: 5,
  RIGHT_EYE_OUTER: 6,
  LEFT_EAR: 7,
  RIGHT_EAR: 8,
  LEFT_RIGHT: 9,
  RIGHT_LEFT: 10,
  LEFT_SHOULDER: 11,
  RIGHT_SHOULDER: 12,
  LEFT_ELBOW: 13,
  RIGHT_ELBOW: 14,
  LEFT_WRIST: 15,
  RIGHT_WRIST: 16,
  LEFT_PINKY: 17,
  RIGHT_PINKY: 18,
  LEFT_INDEX: 19,
  RIGHT_INDEX: 20,
  LEFT_THUMB: 21,
  RIGHT_THUMB: 22,
  LEFT_HIP: 23,
  RIGHT_HIP: 24,
  LEFT_KNEE: 25,
  RIGHT_KNEE: 26,
  LEFT_ANKLE: 27,
  RIGHT_ANKLE: 28,
  LEFT_HEEL: 29,
  RIGHT_HEEL: 30,
  LEFT_FOOT_INDEX: 31,
  RIGHT_FOOT_INDEX: 32
};

var getValueByPart = function getValueByPart(result, key) {
  switch (key) {
    case 'rShoulder':
      if (result.poseLandmarks[14] && result.poseLandmarks[12] && result.poseLandmarks[24]) {
        return [result.poseLandmarks[14], result.poseLandmarks[12], result.poseLandmarks[24]];
      }

      break;

    case 'lShoulder':
      if (result.poseLandmarks[13] && result.poseLandmarks[11] && result.poseLandmarks[23]) {
        return [result.poseLandmarks[13], result.poseLandmarks[11], result.poseLandmarks[23]];
      }

      break;

    case 'rElbow':
      if (result.rightHandLandmarks && result.poseLandmarks[12] && result.poseLandmarks[14] && result.rightHandLandmarks[0]) {
        return [result.poseLandmarks[12], result.poseLandmarks[14], result.rightHandLandmarks[0]];
      }

      break;

    case 'lElbow':
      if (result.leftHandLandmarks && result.poseLandmarks[11] && result.poseLandmarks[13] && result.leftHandLandmarks[0]) {
        return [result.poseLandmarks[11], result.poseLandmarks[13], result.leftHandLandmarks[0]];
      }

      break;

    case 'rHip':
      if (result.poseLandmarks[26] && result.poseLandmarks[24] && result.poseLandmarks[12]) {
        return [result.poseLandmarks[26], result.poseLandmarks[24], result.poseLandmarks[12]];
      }

      break;

    case 'lHip':
      if (result.poseLandmarks[25] && result.poseLandmarks[23] && result.poseLandmarks[11]) {
        return [result.poseLandmarks[25], result.poseLandmarks[23], result.poseLandmarks[11]];
      }

      break;

    case 'rKnee':
      if (result.poseLandmarks[28] && result.poseLandmarks[26] && result.poseLandmarks[24]) {
        return [result.poseLandmarks[28], result.poseLandmarks[26], result.poseLandmarks[24]];
      }

      break;

    case 'lKnee':
      if (result.poseLandmarks[27] && result.poseLandmarks[25] && result.poseLandmarks[23]) {
        return [result.poseLandmarks[27], result.poseLandmarks[25], result.poseLandmarks[23]];
      }

      break;

    case 'rSide':
      if (result.poseLandmarks[28] && result.poseLandmarks[24] && result.poseLandmarks[12]) {
        return [result.poseLandmarks[27], result.poseLandmarks[23], result.poseLandmarks[11]];
      }

      break;

    case 'lSide':
      if (result.poseLandmarks[27] && result.poseLandmarks[23] && result.poseLandmarks[11]) {
        return [result.poseLandmarks[27], result.poseLandmarks[23], result.poseLandmarks[11]];
      }

      break;

    case 'legs':
      if (result.poseLandmarks[28] && result.poseLandmarks[28].visibility > 0.8 && result.poseLandmarks[26] && result.poseLandmarks[26].visibility > 0.8 && result.poseLandmarks[24] && result.poseLandmarks[24].visibility > 0.8) {
        return [result.poseLandmarks[28], result.poseLandmarks[26], result.poseLandmarks[24]];
      } else if (result.poseLandmarks[27] && result.poseLandmarks[27].visibility > 0.8 && result.poseLandmarks[25] && result.poseLandmarks[25].visibility > 0.8 && result.poseLandmarks[23] && result.poseLandmarks[23].visibility > 0.8) {
        return [result.poseLandmarks[27], result.poseLandmarks[25], result.poseLandmarks[23]];
      }

      break;

    default:
      throw new Error("Unknowing Key " + key);
  }
};

var _calculateAngle = function _calculateAngle(p0, p1, p2) {
  var a = Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2);
  var b = Math.pow(p1.x - p0.x, 2) + Math.pow(p1.y - p0.y, 2);
  var c = Math.pow(p2.x - p0.x, 2) + Math.pow(p2.y - p0.y, 2);
  var angle = Math.acos((a + b - c) / Math.sqrt(4 * a * b)) * (180 / Math.PI);
  return Math.round(angle * 100) / 100;
};

var calculate = function calculate(key, result) {
  try {
    var position = getValueByPart(result, key);
    var values = 0;

    if (position && position.length > 0) {
      values = _calculateAngle(position[0], position[1], position[2]);
    }

    if (key === 'rElbow' || key === 'lElbow') return 180 - values;
    return values;
  } catch (error) {
    console.error('ERRO AQUI ', error);
    throw new Error("error = " + error);
  }
};

var _default = {
  calculate: calculate
};
exports["default"] = _default;
module.exports = exports.default;