import { magvar } from 'magvar';

Number.prototype.toRadians = function () {
  return ((this as number) * Math.PI) / 180;
};
Number.prototype.toDegrees = function () {
  return ((this as number) * 180) / Math.PI;
};
Number.prototype.reciprocalCourse = function () {
  let inv = (this as number) + 180;
  inv = inv >= 360 ? inv - 360 : inv;
  return inv;
};
Number.prototype.normaliseDegrees = function () {
  return (this as number) >= 360
    ? (this as number) - 360
    : (this as number) < 0
      ? (this as number) + 360
      : (this as number);
};
Number.prototype.toTrue = function (fix) {
  const _magvar = magvar(fix.latitude, fix.longitude); //Magvar is returned + for East
  return ((this as number) + _magvar).normaliseDegrees();
};
Number.prototype.toMetre = function () {
  return (this as number) * 1852.0;
};
Number.prototype.equal = function (other: number) {
  return Math.abs((this as number) - other) < 0.1;
};

String.prototype.parseAltitude = function () {
  switch (this.length) {
    case 5:
      return Number.parseInt(this as string);
    case 6:
      return Number.parseInt(this.substring(0, 5));
    case 12: {
      const upper = Number.parseInt(this.substring(0, 5));
      const lower = Number.parseInt(this.substring(6, 12));
      return lower + (upper - lower) / 2;
    }
    default:
      return -1;
  }
};
