/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{__decorate as e}from"tslib";import t from"../core/Accessor.js";import i from"../core/Logger.js";import{property as r,cast as s,subclass as o}from"../core/accessorSupport/decorators.js";import{ensureNumber as l}from"../core/accessorSupport/ensureType.js";import n from"./Geometry.js";import a from"./SpatialReference.js";import{getLatitude as u,getLongitude as h,distance as p}from"./support/pointUtils.js";import{getInfo as c}from"./support/spatialReferenceUtils.js";import{lngLatToXY as d,xyToLngLat as y}from"./support/webMercatorUtils.js";import{writer as m}from"../core/accessorSupport/decorators/writer.js";import{reader as f}from"../core/accessorSupport/decorators/reader.js";var g;const w=[0,0];function x(e){return e&&("esri.geometry.SpatialReference"===e.declaredClass||null!=e.wkid)}let _=g=class extends n{static copy(e,i){i._set("x",e._get("x")),i._set("y",e._get("y")),i._set("z",e._get("z")),i._set("m",e._get("m"));const r=e._get("spatialReference");i._set("spatialReference",t.isFrozen(r)?r:r.clone())}constructor(...e){super(...e),this.x=0,this.y=0,this.z=void 0,this.m=void 0,this.type="point"}normalizeCtorArgs(e,t,r,s,o){let l;if(Array.isArray(e))l=e,o=t,e=l[0],t=l[1],r=l[2],s=l[3];else if(e&&"object"==typeof e){if(l=e,e=null!=l.x?l.x:l.longitude,t=null!=l.y?l.y:l.latitude,r=l.z,s=l.m,(o=l.spatialReference)&&"esri.geometry.SpatialReference"!==o.declaredClass&&(o=new a(o)),null!=l.longitude||null!=l.latitude)if(null==l.longitude)i.getLogger(this).warn(".longitude=","Latitude was defined without longitude");else if(null==l.latitude)i.getLogger(this).warn(".latitude=","Longitude was defined without latitude");else if(!l.declaredClass&&o?.isWebMercator){const i=d(l.longitude,l.latitude,w);e=i[0],t=i[1]}}else x(r)?(o=r,r=null):x(s)&&(o=s,s=null);const n={x:e,y:t};return null==n.x&&null!=n.y?i.getLogger(this).warn(".y=","Y coordinate was defined without an X coordinate"):null==n.y&&null!=n.x&&i.getLogger(this).warn(".x=","X coordinate was defined without a Y coordinate"),null!=o&&(n.spatialReference=o),null!=r&&(n.z=r),null!=s&&(n.m=s),n}get cache(){return this.commitProperty("x"),this.commitProperty("y"),this.commitProperty("z"),this.commitProperty("m"),this.commitProperty("spatialReference"),{}}get hasM(){return void 0!==this.m}set hasM(e){e!==(void 0!==this._get("m"))&&(this._set("m",e?0:void 0),this._set("hasM",e))}get hasZ(){return void 0!==this.z}set hasZ(e){e!==(void 0!==this._get("z"))&&(this._set("z",e?0:void 0),this._set("hasZ",e))}get latitude(){return u(this)}set latitude(e){const{spatialReference:t,x:i}=this;null!=e&&t&&(t.isWebMercator?this._set("y",d(i,e,w)[1]):t.isGeographic&&this._set("y",e),this._set("latitude",e))}get longitude(){return h(this)}set longitude(e){const{y:t,spatialReference:i}=this;null!=e&&i&&(i.isWebMercator?this._set("x",d(e,t,w)[0]):i.isGeographic&&this._set("x",e),this._set("longitude",e))}writeX(e,t,i){t[i]=isNaN(e)?"NaN":e}readX(e){return"string"==typeof e?NaN:e}clone(){const e=new g;return e.x=this.x,e.y=this.y,e.z=this.z,e.m=this.m,e.spatialReference=this.spatialReference,e}copy(e){return g.copy(e,this),this}equals(e){if(null==e)return!1;const{x:t,y:i,z:r,m:s,spatialReference:o}=this,{z:l,m:n}=e;let{x:a,y:u,spatialReference:h}=e;if(!o.equals(h))if(o.isWebMercator&&h.isWGS84)[a,u]=d(a,u),h=o;else{if(!o.isWGS84||!h.isWebMercator)return!1;[a,u]=y(a,u),h=o}return t===a&&i===u&&r===l&&s===n&&o.wkid===h.wkid}offset(e,t,i){return this.x+=e,this.y+=t,null!=i&&(this.z=(this.z??0)+i),this}normalize(){if(!this.spatialReference)return this;const e=c(this.spatialReference);if(!e)return this;let t=this.x;const[i,r]=e.valid,s=2*r;let o;return t>r?(o=Math.ceil(Math.abs(t-r)/s),t-=o*s):t<i&&(o=Math.ceil(Math.abs(t-i)/s),t+=o*s),this._set("x",t),this}distance(e){return p(this,e)}toArray(){const e=this.hasZ,t=this.hasM;return e&&t?[this.x,this.y,this.z,this.m]:e?[this.x,this.y,this.z]:t?[this.x,this.y,this.m]:[this.x,this.y]}toJSON(e){return this.write({},e)}};e([r({readOnly:!0})],_.prototype,"cache",null),e([r({type:Boolean,json:{read:!1,write:{enabled:!1,overridePolicy:null}}})],_.prototype,"hasM",null),e([r({type:Boolean,json:{read:!1,write:{enabled:!1,overridePolicy:null}}})],_.prototype,"hasZ",null),e([r({type:Number})],_.prototype,"latitude",null),e([r({type:Number})],_.prototype,"longitude",null),e([r({type:Number,json:{type:[Number,String],write:{isRequired:!0,allowNull:!0}}}),s(e=>isNaN(e)?e:l(e))],_.prototype,"x",void 0),e([m("x")],_.prototype,"writeX",null),e([f("x")],_.prototype,"readX",null),e([r({type:Number,json:{write:!0}})],_.prototype,"y",void 0),e([r({type:Number,json:{write:{overridePolicy(){return{enabled:this.hasZ}}}}})],_.prototype,"z",void 0),e([r({type:Number,json:{write:{overridePolicy(){return{enabled:this.hasM}}}}})],_.prototype,"m",void 0),_=g=e([o("esri.geometry.Point")],_),_.prototype.toJSON.isDefaultToJSON=!0;export{_ as default};
