import StringHelper from "./stringHelper";

export default class FullNominatimResponse {
  lat;
  lon;
  boundingBox;
  city;
  street;
  building_number;
  type;
  wikipedia;
  name;
  name_suffix;
  processed_name;

  constructor(init) {
    if (init !== undefined) {
      this.lat = Number(init.lat);
      this.lon = Number(init.lon);
      this.boundingBox = new L.LatLngBounds(
        [parseFloat(init.boundingbox[0]), parseFloat(init.boundingbox[2])],
        [parseFloat(init.boundingbox[1]), parseFloat(init.boundingbox[3])],
      );

      this.wikipedia = "";

      if ("city" in init.address) this.city = init.address.city;
      else if ("town" in init.address) this.city = init.address.town;
      else this.city = "";

      if ("street" in init.address) this.street = init.address.street;
      else if ("road" in init.address) this.street = init.address.road;
      else this.street = "";

      if ("house_number" in init.address)
        this.building_number = init.address.house_number;
      else this.building_number = "";

      this.type = init.type;

      if (init.type === "living_street") this.name = init.address.road;
      else if (init.type === "residential") {
        this.name = init.address.road;
        this.type = "living_street";
      } else if (
        (init.type === "primary" ||
          init.type === "secondary" ||
          init.type === "tertiary") &&
        init.class === "highway"
      ) {
        this.name = init.address.road;
        this.type = "living_street";
      } else if ("city" in init.address) this.name = init.address.city;
      else if ("town" in init.address) this.name = init.address.town;
      else if ("hamlet" in init.address) this.name = init.address.hamlet;
      else if ("village" in init.address) this.name = init.address.village;
      else if ("administrative" in init.address)
        this.name = init.address.administrative;
      else this.name = "";

      if ("municipality" in init.address) {
        if (this.name === "") {
          this.name = init.address.municipality;
          this.name_suffix = "";
        } else {
          this.name_suffix = init.address.municipality;
        }
      } else if ("suburb" in init.address)
        this.name_suffix = init.address.suburb;
      else if ("county" in init.address) this.name_suffix = init.address.county;
      else this.name_suffix = "";

      this.processed_name = StringHelper.createAutocompleteName(this);
    }
  }
}
