
const dms = require('./dms');

function geometryAsString (item, opts = {}) {
  const key = "key" in opts ? opts.key : "geometry";
  const formatDMS = opts.dms;

  let str = "";

  if (key in item) {
    const geometry = item[key];
    if (geometry && "coordinates" in geometry) {
      if (geometry.type == "Point") {
        if (formatDMS) {
          str = dms(geometry.coordinates[1], geometry.coordinates[0]);
        } else {
          str = `${geometry.coordinates[1].toFixed(6)}, ${geometry.coordinates[0].toFixed(6)}`;
        }
      }

      if (str) {
        if (opts.url) {
          if (typeof opts.url === 'string') {
            str = `[${str}](${opts.url.replace("$x", geometry.coordinates[0]).replace("$y", geometry.coordinates[1])})`;
          } else {
             str = `[${str}](geo:${geometry.coordinates[0]},${geometry.coordinates[1]})`;
          }
        }
      }
    }
  }

  return str;
}

module.exports = geometryAsString;
