const log = require('./logger');

const localOrder = {
  City: 5,
  Hamlet: 2,
  'Other Settlement': 1,
  'Suburban Area': 0,
  Town: 4,
  Village: 3,
};

function getOrder(type) {
  const order = localOrder[type];
  if (order === undefined) {
    log.error(`unknown local_type '${type}'`);
    return -1;
  }
  return order;
}

function compare(first, second) {
  if (first < second) {
    return -1;
  }
  if (first > second) {
    return 1;
  }
  return 0;
}

function sortPlace(places) {
  return places.sort((a, b) => {
    const first = getOrder(a.local_type);
    const second = getOrder(b.local_type);
    return compare(second, first) ||
           compare(a.name_1, b.name_1) ||
           compare(a.county_unitary || a.region, b.county_unitary || b.region) ||
           compare(a.outcode, b.outcode);
  });
}

function getCountries(places) {
  return [...new Set(places.map(place => place.country))].sort(compare);
}

module.exports = {
  getCountries,
  sortPlace,
};

@@ -27,16 +27,47 @@
 ger');%0A%0A
+/* eslint-disable sort-keys */%0A
 const lo
@@ -90,16 +90,41 @@
 ity: 5,%0A
+  Town: 4,%0A  Village: 3,%0A
   Hamlet
@@ -179,35 +179,40 @@
  0,%0A
-  Town: 4,%0A  Village: 3,%0A%7D;
+%7D;%0A/* eslint-enable sort-keys */
 %0A%0Afu

Order based on importance