// Generated by nautilus-codegen. Do not edit manually.

function _format{{ type_name }}Wkt(wkt, srid) {
  const raw = String(wkt);
  return srid == null ? raw : `SRID=${Number(srid)};${raw}`;
}

function _normalize{{ type_name }}Input(value) {
  if (value instanceof {{ type_name }}) return value.value;
  if (typeof value === 'string') return value;
  if (value == null) throw new TypeError('Unsupported {{ type_name }} input');
  if (typeof value === 'object' && !Array.isArray(value)) {
    if (Object.prototype.hasOwnProperty.call(value, 'value')) {
      return String(value.value);
    }
{%- if is_spatial %}
    if (Object.prototype.hasOwnProperty.call(value, 'wkt')) {
      return _format{{ type_name }}Wkt(value.wkt, value.srid);
    }
{%- if is_geometry %}
    if (
      Object.prototype.hasOwnProperty.call(value, 'x')
      && Object.prototype.hasOwnProperty.call(value, 'y')
    ) {
      return _format{{ type_name }}Wkt(`POINT(${Number(value.x)} ${Number(value.y)})`, value.srid);
    }
{%- endif %}
{%- if is_geography %}
    if (
      Object.prototype.hasOwnProperty.call(value, 'lon')
      && Object.prototype.hasOwnProperty.call(value, 'lat')
    ) {
      return _format{{ type_name }}Wkt(`POINT(${Number(value.lon)} ${Number(value.lat)})`, value.srid);
    }
{%- endif %}
{%- endif %}
  }
  if (typeof value === 'number' || typeof value === 'boolean') return String(value);
  throw new TypeError('Unsupported {{ type_name }} input');
}

export class {{ type_name }} {
  constructor(value) {
    this.value = _normalize{{ type_name }}Input(value);
  }

  static of(value) {
    return new {{ type_name }}(value);
  }

{%- if is_spatial %}
  static wkt(value, srid) {
    return new {{ type_name }}(_format{{ type_name }}Wkt(value, srid));
  }

{%- if is_geometry %}
  static point(x, y, srid) {
    return new {{ type_name }}(_format{{ type_name }}Wkt(`POINT(${Number(x)} ${Number(y)})`, srid));
  }

{%- endif %}
{%- if is_geography %}
  static point(lon, lat, srid) {
    return new {{ type_name }}(_format{{ type_name }}Wkt(`POINT(${Number(lon)} ${Number(lat)})`, srid));
  }

{%- endif %}
{%- endif %}
  static from(value) {
    return value instanceof {{ type_name }} ? value : new {{ type_name }}(value);
  }

  static toWireInput(value) {
    return _normalize{{ type_name }}Input(value);
  }

  asString() {
    return this.value;
  }

  toWire() {
    return this.value;
  }

  equals(other) {
    const otherValue = _normalize{{ type_name }}Input(other);
    return this.value === otherValue;
  }

  isEmpty() {
    return this.value.length === 0;
  }

  toString() {
    return this.value;
  }
}
