// All material copyright ESRI, All Rights Reserved, unless otherwise specified.
// See http://@sbaseurl@/jsapi/jsapi/esri/copyright.txt and http://www.arcgis.com/apps/webappbuilder/copyright.txt for details.

//>>built
define(["dojo/_base/declare","dojo/_base/array","dojo/_base/Deferred","esri/tasks/GeometryService","jimu/utils"],function(h,k,f,l,e){return h(null,{constructor:function(a){this.appConfig=a.appConfig;this.nls=a.nls;(a=this.appConfig.geometryService)||(a="//utility.arcgisonline.com/arcgis/rest/services/Geometry/GeometryServer");this.geomService=new l(a)},getCleanInput:function(a){a=a.replace(/\n/g,"");a=a.replace(/\s+/g," ").trim();return a.toUpperCase()},getCoordValues:function(a,d,b){var c=new f;
a={sr:4326,coordinates:[[a.x,a.y]],conversionType:d.name?d.name:d,numOfDigits:b||6,rounding:!0,addSpaces:!1};switch(d){case "DD":a.numOfDigits=6;break;case "USNG":a.numOfDigits=5;break;case "MGRS":a.conversionMode="mgrsDefault";a.numOfDigits=5;break;case "UTM (H)":a.conversionType="utm";a.conversionMode="utmNorthSouth";a.addSpaces=!0;break;case "UTM":a.conversionType="utm";a.conversionMode="utmDefault";a.addSpaces=!0;break;case "GARS":a.conversionMode="garsDefault"}this.geomService.toGeoCoordinateString(a).then(function(g){c.resolve(g)},
function(){c.resolve(null)});return c.promise},getXYNotation:function(a,d){var b=new f;var c=d.name?d.name:d;d={sr:4326,conversionType:c,strings:[]};switch(c){case "DD":case "DDM":case "DMS":d.numOfDigits=2;a=a.replace(/[\u00b0\u02da\u00ba^~*"'\u2032\u00a8\u02dd]/g,"");d.strings.push(a);break;case "USNG":d.strings.push(a);d.addSpaces="false";break;case "MGRS":d.conversionMode="mgrsNewStyle";d.strings.push(a);d.addSpaces="false";break;case "UTM (H)":d.conversionType="utm";d.conversionMode="utmNorthSouth";
d.strings.push(a);break;case "UTM":d.conversionType="utm";d.conversionMode="utmDefault";d.strings.push(a);break;case "GARS":d.conversionMode="garsCenter";d.strings.push(a);break;case "GEOREF":d.strings.push(a)}this.geomService.fromGeoCoordinateString(d).then(function(g){b.resolve(g)},function(){b.resolve(null)});return b.promise},getNotations:function(){return[{name:"DD",pattern:/^(([NS\+\-\s])*([0-8]?\d([,.]\d*)?|90([,.]0*)?)([\u00b0\u02da\u00ba^~*]*)([NS\+\-\s])*)([,:;\s|\/\\]+)(([EW\+\-\s])*([0]?\d?\d([,.]\d*)?|1[0-7]\d([,.]\d*)?|180([,.]0*)?)([\u00b0\u02da\u00ba^~*]*)([EW\+\-\s])*)$/,
notationType:this.nls.DDLatLongNotation,conversionType:"DD"},{name:"DDrev",pattern:/^(([EW\+\-\s])*([0]?\d?\d([,.]\d*)?|1[0-7]\d([,.]\d*)?|180([,.]0*)?)([\u00b0\u02da\u00ba^~*]*)([EW\+\-\s])*)([,:;\s|\/\\]+)(([NS\+\-\s])*([0-8]?\d([,.]\d*)?|90([,.]0*)?)([\u00b0\u02da\u00ba^~*]*)([NS\+\-\s])*)$/,notationType:this.nls.DDLongLatNotation,conversionType:"DD"},{name:"DDM",pattern:/^(([NS\+\-\s])*([0-8]?\d|90)[\u00b0\u02da\u00ba^~*\s\-_]+(([0-5]?\d|\d)([,.]\d*)?)['\u2032\s_]*([NS\+\-\s])*)([,:;\s|\/\\]+)(([EW\+\-\s])*([0]?\d?\d|1[0-7]\d|180)[\u00b0\u02da\u00ba^~*\s\-_]+(([0-5]\d|\d)([,.]\d*)?)['\u2032\s_]*([EW\+\-\s])*)[\s]*$/,
notationType:this.nls.DDMLatLongNotation,conversionType:"DDM"},{name:"DDMrev",pattern:/^(([EW\+\-\s])*([0]?\d?\d|1[0-7]\d|180)[\u00b0\u02da\u00ba^~*\s\-_]+(([0-5]\d|\d)([,.]\d*)?)['\u2032\s_]*([EW\+\-\s])*)([,:;\s|\/\\]+)(([NS\+\-\s])*([0-8]?\d|90)[\u00b0\u02da\u00ba^~*\s\-_]+(([0-5]?\d|\d)([,.]\d*)?)['\u2032\s_]*([NS\+\-\s])*)[\s]*$/,notationType:this.nls.DDMLongLatNotation,conversionType:"DDM"},{name:"DMS",pattern:/^(([NS\+\-\s])*([0-8]?\d|90)[\u00b0\u02da\u00ba^~*\s\-_]+([0-5]?\d|\d)['\u2032\s\-_]+(([0-5]?\d|\d)([,.]\d*)?)["\u00a8\u02dd\s_]*([NS\+\-\s])*)([,:;\s|\/\\]+)(([EW\+\-\s])*([0]?\d?\d|1[0-7]\d|180)[\u00b0\u02da\u00ba^~*\s\-_]+([0-5]\d|\d)['\u2032\s\-_]+(([0-5]?\d|\d)([,.]\d*)?)["\u00a8\u02dd\s_]*([EW\+\-\s])*)[\s]*$/,
notationType:this.nls.DMSLatLongNotation,conversionType:"DMS"},{name:"DMSrev",pattern:/^(([EW\+\-\s])*([0]?\d?\d|1[0-7]\d|180)[\u00b0\u02da\u00ba^~*\s\-_]+([0-5]\d|\d)['\u2032\s\-_]+(([0-5]?\d|\d)([,.]\d*)?)["\u00a8\u02dd\s_]*([EW\+\-\s])*)([,:;\s|\/\\]+)(([NS\+\-\s])*([0-8]?\d|90)[\u00b0\u02da\u00ba^~*\s\-_]+([0-5]?\d|\d)['\u2032\s\-_]+(([0-5]?\d|\d)([,.]\d*)?)["\u00a8\u02dd\s_]*([NS\+\-\s])*)[\s]*$/,notationType:this.nls.DMSLongLatNotation,conversionType:"DMS"},{name:"GARS",pattern:/^\d{3}[a-zA-Z]{2}[1-4]?[1-9]?$/,
notationType:this.nls.GARSNotation,conversionType:"GARS"},{name:"GEOREF",pattern:/^[a-zA-Z]{4}\d{1,8}$/,notationType:this.nls.GEOREFNotation,conversionType:"GEOREF"},{name:"MGRS",pattern:/^\d{1,2}[-,;:\s]*[C-HJ-NP-X][-,;:\s]*[A-HJ-NP-Z]{2}[-,;:\s]*(\d[-,;:\s]+\d|\d{2}[-,;:\s]+\d{2}|\d{3}[-,;:\s]+\d{3}|\d{4}[-,;:\s]+\d{4}|\d{5}[-,;:\s]+\d{5})$|^(\d{1,2}[-,;:\s]*[C-HJ-NP-X][-,;:\s]*[A-HJ-NP-Z]{2}[-,;:\s]*)(\d{2}|\d{4}|\d{6}|\d{8}|\d{10})?$|^[ABYZ][-,;:\s]*[A-HJ-NP-Z]{2}[-,;:\s]*(\d[-,;:\s]+\d|\d{2}[-,;:\s]+\d{2}|\d{3}[-,;:\s]+\d{3}|\d{4}[-,;:\s]+\d{4}|\d{5}[-,;:\s]+\d{5})$|^[ABYZ][-,;:\s]*[A-HJ-NP-Z]{2}[-,;:\s]*(\d{2}|\d{4}|\d{6}|\d{8}|\d{10})?$/,
notationType:this.nls.MGRSNotation,conversionType:"MGRS"},{name:"UTM",pattern:/^\d{1,2}[-,;:\s]*[c-hj-np-xC-HJ-NP-X][-,;:\s]*\d{1,6}\.?\d*[mM]?[-,;:\s]?\d{1,7}\.?\d*[mM]?$/,notationType:this.nls.UTMBandNotation,conversionType:"UTM"},{name:"UTM (H)",pattern:/^\d{1,2}[-,;:\s]*[NnSs][-,;:\s]*\d{1,6}\.?\d*[mM]?[-,;:\s]+\d{1,7}\.?\d*[mM]?$/,notationType:this.nls.UTMHemNotation,conversionType:"UTM (H)"}]},getCoordinateType:function(a){var d=this.getCleanInput(a);a=new f;var b=this.getNotations();d=k.filter(b,
function(c){return c.pattern.test(this.v)},{v:d});0<d.length?a.resolve(d):a.resolve(null);return a.promise},getFormattedDDStr:function(a,d,b){var c={};c.sourceValue=a;c.sourceFormatString=d;a=a[0].split(/[ ,]+/);c.latdeg=a[0].replace(/[nNsS]/,"");c.londeg=a[1].replace(/[eEwW]/,"");c.latdeg=e.localizeNumber(parseFloat(c.latdeg));c.londeg=e.localizeNumber(parseFloat(c.londeg));b&&(c.latdeg="N"===a[0].slice(-1)?"+"+c.latdeg:"-"+c.latdeg,c.londeg="W"===a[1].slice(-1)?"-"+c.londeg:"+"+c.londeg);d=d.replace(/X/,
c.londeg);d=d.replace(/[eEwW]/,a[1].slice(-1));d=d.replace(/[nNsS]/,a[0].slice(-1));d=d.replace(/Y/,c.latdeg);c.formatResult=d;return c},getFormattedDDMStr:function(a,d,b){var c={};c.sourceValue=a;c.sourceFormatString=d;c.parts=a[0].split(/[ ,]+/);c.latdeg=c.parts[0];c.latmin=c.parts[1].replace(/[nNsS]/,"");c.londeg=c.parts[2];c.lonmin=c.parts[3].replace(/[eEwW]/,"");c.latmin=e.localizeNumber(parseFloat(c.latmin));c.lonmin=e.localizeNumber(parseFloat(c.lonmin));b&&(c.latdeg="N"===c.parts[1].slice(-1)?
"+"+c.latdeg:"-"+c.latdeg,c.londeg="W"===c.parts[3].slice(-1)?"-"+c.londeg:"+"+c.londeg);a=d.replace(/[EeWw]/,c.parts[3].slice(-1));a=a.replace(/Y/,c.lonmin);a=a.replace(/X/,c.londeg);a=a.replace(/[NnSs]/,c.parts[1].slice(-1));a=a.replace(/B/,c.latmin);a=a.replace(/A/,c.latdeg);c.formatResult=a;return c},getFormattedDMSStr:function(a,d,b){var c={};c.sourceValue=a;c.sourceFormatString=d;c.parts=a[0].split(/[ ,]+/);c.latdeg=c.parts[0];c.latmin=c.parts[1];c.latsec=c.parts[2].replace(/[NnSs]/,"");c.londeg=
c.parts[3];c.lonmin=c.parts[4];c.lonsec=c.parts[5].replace(/[EWew]/,"");c.latsec=e.localizeNumber(parseFloat(c.latsec));c.lonsec=e.localizeNumber(parseFloat(c.lonsec));b&&(c.latdeg="N"===c.parts[2].slice(-1)?"+"+c.latdeg:"-"+c.latdeg,c.londeg="W"===c.parts[5].slice(-1)?"-"+c.londeg:"+"+c.londeg);a=d.replace(/A/,c.latdeg);a=a.replace(/B/,c.latmin);a=a.replace(/C/,c.latsec);a=a.replace(/X/,c.londeg);a=a.replace(/Y/,c.lonmin);a=a.replace(/Z/,c.lonsec);a=a.replace(/[NnSs]/,c.parts[2].slice(-1));a=a.replace(/[EeWw]/,
c.parts[5].slice(-1));c.formatResult=a;return c},getFormattedUSNGStr:function(a,d){var b={};b.sourceValue=a;b.sourceFormatString=d;a[0].match(/^[ABYZ]/)?b.gzd=a[0].match(/[ABYZ]/)[0].trim():b.gzd=a[0].match(/\d{1,2}[C-HJ-NP-X]/)[0].trim();b.grdsq=a[0].replace(b.gzd,"").match(/[a-hJ-zA-HJ-Z]{2}/)[0].trim();b.easting=a[0].replace(b.gzd+b.grdsq,"").match(/^\d{1,5}/)[0].trim();b.northing=a[0].replace(b.gzd+b.grdsq,"").match(/\d{1,5}$/)[0].trim();a=d.replace(/Y/,b.northing);a=a.replace(/X/,b.easting);
a=a.replace(/S/,b.grdsq);a=a.replace(/Z/,b.gzd);b.formatResult=a;return b},getFormattedMGRSStr:function(a,d){var b={};b.sourceValue=a;b.sourceFormatString=d;a[0].match(/^[ABYZ]/)?b.gzd=a[0].match(/[ABYZ]/)[0].trim():b.gzd=a[0].match(/\d{1,2}[C-HJ-NP-X]/)[0].trim();b.grdsq=a[0].replace(b.gzd,"").match(/[a-hJ-zA-HJ-Z]{2}/)[0].trim();b.easting=a[0].replace(b.gzd+b.grdsq,"").match(/^\d{1,5}/)[0].trim();b.northing=a[0].replace(b.gzd+b.grdsq,"").match(/\d{1,5}$/)[0].trim();a=d.replace(/Y/,b.northing);a=
a.replace(/X/,b.easting);a=a.replace(/S/,b.grdsq);a=a.replace(/Z/,b.gzd);b.formatResult=a;return b},getFormattedGARSStr:function(a,d){var b={};b.sourceValue=a;b.sourceFormatString=d;b.lon=a[0].match(/\d{3}/);b.lat=a[0].match(/[a-zA-Z]{2}/);a=a[0].match(/\d*$/);b.quadrant=a[0][0];b.key=a[0][1];d=d.replace(/K/,b.key);d=d.replace(/Q/,b.quadrant);d=d.replace(/Y/,b.lat);d=d.replace(/X/,b.lon);b.formatResult=d;return b},getFormattedGEOREFStr:function(a,d){var b={};b.sourceValue=a;b.sourceFormatString=d;
b.lon=a[0].match(/[a-zA-Z]{1}/)[0].trim();b.lat=a[0].replace(b.lon,"").match(/[a-zA-Z]{1}/)[0].trim();b.quadrant15lon=a[0].replace(b.lon+b.lat,"").match(/[a-zA-Z]{1}/)[0].trim();b.quadrant15lat=a[0].replace(b.lon+b.lat+b.quadrant15lon,"").match(/[a-zA-Z]{1}/)[0].trim();a=a[0].replace(b.lon+b.lat+b.quadrant15lon+b.quadrant15lat,"");b.quadrant1lon=a.substr(0,a.length/2);b.quadrant1lat=a.substr(a.length/2,a.length);d=d.replace(/Y/,b.quadrant1lat);d=d.replace(/X/,b.quadrant1lon);d=d.replace(/D/,b.quadrant15lat);
d=d.replace(/C/,b.quadrant15lon);d=d.replace(/B/,b.lat);d=d.replace(/A/,b.lon);b.formatResult=d;return b},getFormattedUTMStr:function(a,d){var b={};b.sourceValue=a;b.sourceFormatString=d;b.parts=a[0].split(/[ ,]+/);b.zone=b.parts[0].replace(/[A-Z]/,"");b.bandLetter=b.parts[0].slice(-1);b.easting=b.parts[1];b.westing=b.parts[2];a=d.replace(/Y/,b.westing);a=a.replace(/X/,b.easting);a=a.replace(/B/,b.bandLetter);a=a.replace(/Z/,b.zone);b.formatResult=a;return b},getFormattedUTMHStr:function(a,d){var b=
{};b.sourceValue=a;b.sourceFormatString=d;b.parts=a[0].split(/[ ,]+/);b.zone=b.parts[0].replace(/[A-Z]/,"");b.hemisphere=b.parts[0].slice(-1);b.easting=b.parts[1];b.westing=b.parts[2];a=d.replace(/Y/,b.westing);a=a.replace(/X/,b.easting);a=a.replace(/H/,b.hemisphere);a=a.replace(/Z/,b.zone);b.formatResult=a;return b},convertMetersToUnits:function(a,d){var b=0;switch(d.toLowerCase()){case "meters":b=a;break;case "feet":b=3.28084*a;break;case "kilometers":b=.001*a;break;case "miles":b=6.21371E-4*a;
break;case "nautical-miles":b=5.39957E-4*a;break;case "yards":b=1.09361*a}return b},convertToMeters:function(a,d){var b=a;switch(d){case "meters":b=a;break;case "feet":b=.3048*a;break;case "kilometers":b=1E3*a;break;case "miles":b=1609.34*a;break;case "nautical-miles":b=1852.001376036*a;break;case "yards":b=.9144*a}return b}})});