"use strict";

var _interopRequireWildcard = require("@babel/runtime-corejs3/helpers/interopRequireWildcard");

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

var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");

require("core-js/modules/es.number.to-fixed");

require("core-js/modules/es.regexp.exec");

require("core-js/modules/es.string.replace");

_Object$defineProperty(exports, "__esModule", {
  value: true
});

exports.default = void 0;

var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));

var _react = require("react");

var _sexagesimal = _interopRequireDefault(require("@mapbox/sexagesimal"));

var _utm = require("utm");

var coordFormats = _interopRequireWildcard(require("../constants/coord_formats"));

var _Context = require("./Context");

var _helpers = require("../utils/helpers");

// @flow
var FormattedLocation = function FormattedLocation(_ref) {
  var latitude = _ref.latitude,
      longitude = _ref.longitude;

  var _useContext = (0, _react.useContext)(_Context.SettingsContext),
      coordFormat = _useContext.coordFormat;

  switch (coordFormat) {
    case coordFormats.DEC_DEG:
      return formatDecDeg({
        latitude: latitude,
        longitude: longitude
      });

    case coordFormats.DEG_MIN_SEC:
      return _sexagesimal.default.formatPair({
        lon: longitude,
        lat: latitude
      }).replace(/'/g, '’').replace(/"/g, '”');

    case coordFormats.UTM:
      return formatUtm({
        latitude: latitude,
        longitude: longitude
      });
  }

  return null;
};

var _default = FormattedLocation;
exports.default = _default;

function formatUtm(_ref2) {
  var latitude = _ref2.latitude,
      longitude = _ref2.longitude;

  try {
    var _context, _context2, _context3;

    var _fromLatLon = (0, _utm.fromLatLon)(latitude, longitude),
        easting = _fromLatLon.easting,
        northing = _fromLatLon.northing,
        zoneNum = _fromLatLon.zoneNum,
        zoneLetter = _fromLatLon.zoneLetter;

    easting = (0, _helpers.leftPad)(easting.toFixed(), 6, '0');
    northing = (0, _helpers.leftPad)(northing.toFixed(), 6, '0');
    return (0, _concat.default)(_context = (0, _concat.default)(_context2 = (0, _concat.default)(_context3 = "UTM ".concat(zoneNum)).call(_context3, zoneLetter, " ")).call(_context2, easting, " ")).call(_context, northing);
  } catch (e) {
    // Some coordinates (e.g. < 80S or 84N) cannot be formatted as UTM
    return formatDecDeg({
      latitude: latitude,
      longitude: longitude
    });
  }
}

function formatDecDeg(_ref3) {
  var _context4, _context5, _context6;

  var latitude = _ref3.latitude,
      longitude = _ref3.longitude;
  return (0, _concat.default)(_context4 = (0, _concat.default)(_context5 = (0, _concat.default)(_context6 = "".concat(latitude >= 0 ? '+' : '')).call(_context6, latitude.toFixed(6), "\xB0, ")).call(_context5, longitude >= 0 ? '+' : '')).call(_context4, longitude.toFixed(6), "\xB0");
}
//# sourceMappingURL=FormattedLocation.js.map