import moment from 'moment';
import { checkProtocol } from './util.js';

/*
 * Philip Crotwell
 * University of South Carolina, 2019
 * http://www.seis.sc.edu
 */
//import type { moment as momentType } from 'moment';

const DS = "fdsnws-dataselect";
const EV = "fdsn-event";
const ST = "fdsn-station";
const RS = "ringserver";
const BestDCIdMap = new Map();

function initBestDCIdMap() {
  BestDCIdMap.set(knownDataCenters.DS, "IRIS");
  BestDCIdMap.set(ST, "IRIS");
  BestDCIdMap.set(RS, "IRIS");
  BestDCIdMap.set(EV, "USGS");
}

let knownDCs = null;
const knownDataCenters = {
  knownDataCentersJsonURL: checkProtocol() + "//www.seis.sc.edu/fdsnServiceCheck/fdsnDataCenters.json",
  DS: DS,
  EV: EV,
  ST: ST,
  RS: RS,
  allDCTypes: [DS, EV, ST, RS],

  getDefaultDC(type) {
    return this.getDataCenter(BestDCIdMap.get(type));
  },

  /** Loads all known data centers. JSON top level has:
   *  created - modification date,
   *  datacenters - array of objects, each with
   *  id, name, website, email, host, region and
   *  and supports that is an array of type and version.
   *   Returns a Promise that resolves to the returned json. */
  getKnownDataCenters() {
    if (!knownDCs) {
      knownDCs = fetch(this.knownDataCentersJsonURL).then(response => {
        return response.json();
      }).then(function (json) {
        json.accesstime = moment.utc();
        return json;
      });
    }

    return knownDCs;
  },

  /** Forces a reload of the remote json. In general, because the data
   *  is updated infrequently, this is unlikely to be needed. However,
   *  a very long running instance may wish to update this periodically,
   *  likely at most daily. Returns a Promise. */
  updateKnownDataCenters() {
    knownDCs = null;
    return this.getKnownDataCenters();
  },

  /** Gets the data center associated with the id.
   *   Returns a Promise. */
  getDataCenter(id) {
    return this.getKnownDataCenters().then(kdcs => {
      for (const dc of kdcs.datacenters) {
        if (dc.id === id) {
          return dc;
        }
      }

      return null;
    });
  },

  /** returns true is the dc datacenter supports type web service,
   *false otherwise.
   */
  doesSupport(dc, type) {
    let out = dc.supports.find(function (s) {
      return s.type === type;
    });
    return typeof out !== 'undefined';
  },

  /** returns the hostname for type web service. In many cases this
   *  is the same as the host for the overall datacenter, but sometimes
   *  not all web services are hosted on the same machine. For example
   *  all fdsn web services at IRIS are hosted at service.iris.edu
   *  but the ringserver is hosted at rtserve.iris.edu
   */
  serviceHost(dc, type) {
    let does = this.doesSupport(dc, type);

    if (does) {
      return does.host ? does.host : dc.host;
    }

    throw new Error(dc.id + " does not support " + type);
  },

  /** returns the port for type web service. In many cases this
   *  is 80, but sometimes web services are hosted on alternative
   *  ports.
   */
  servicePort(dc, type) {
    let does = this.doesSupport(dc, type);

    if (does) {
      return does.port ? does.port : 80;
    }

    throw new Error(dc.id + " does not support " + type);
  }

};
initBestDCIdMap();

export { knownDataCenters };
//# sourceMappingURL=knowndatacenters.js.map
