/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["../../chunks/_rollupPluginBabelHelpers","../../chunks/tslib.es6","../../geometry","../../core/Accessor","../../core/maybe","../../core/screenUtils","../../core/accessorSupport/decorators/property","../../core/arrayUtils","../../core/has","../../core/accessorSupport/ensureType","../../core/accessorSupport/decorators/subclass","../../geometry/support/geodesicUtils","../../geometry/support/normalizeUtils","../../geometry/support/spatialReferenceUtils","../../geometry/support/webMercatorUtils","../../geometry/support/WKIDUnitConversion","../../geometry/SpatialReference","../../geometry/Polyline","../../geometry/Point"],(function(e,t,r,n,i,o,s,a,c,l,u,p,d,m,g,f,h,y,w){"use strict";function _(e){const{wkid:t}=e;if(null!=f[t])return f.values[f[t]];const{wkt:r}=e,n=r.lastIndexOf(",")+1,i=r.lastIndexOf("]]");return parseFloat(r.substring(n,i))}function S(e,t){return e&&e.includes(t)}function v(e,t){const{x:r,y:n}="decimal-degrees"===e?g.webMercatorToGeographic(t,!0):t;return[r,n]}function b({state:{paddedViewState:e},spatialReference:t,width:r}){return t.isWrappable&&e.worldScreenWidth<r}let P=function(t){function r(e){var r;return(r=t.call(this,e)||this).scaleComputedFrom=o.createScreenPoint(),r.view=null,r}e._inheritsLoose(r,t);var n=r.prototype;return n.getScaleBarProperties=function(e,t){if("disabled"===this.state||isNaN(e)||!t)return null;const r=this._getDistanceInKm(this.view,this.scaleComputedFrom);if(i.isNone(r))return null;if("metric"===t)return this._getScaleBarProps(e,r,"metric");const n=r/1.609;return this._getScaleBarProps(e,n,"non-metric")},n._getLocationUnit=function(){const e=this.get("view.spatialReference"),{isWebMercator:t,wkid:r,wkt:n}=e;return t||S(n,"WGS_1984_Web_Mercator")?"decimal-degrees":null!=f[r]||S(n,"PROJCS")?"linear-unit":"unknown"},n._getDistanceInKm=function(e,t){const{state:r,spatialReference:n}=e,i=this._getLocationUnit();if("linear-unit"===i){const e=1e3;return r.extent.width*_(n)/e}const[o,s]=this._getScaleMeasuringPoints(e,t),a="decimal-degrees"===i||n.isGeographic&&!p.isSupported(n)?h.WGS84:n,c=new y({paths:[[v(i,o),v(i,s)]],spatialReference:a}),l=d.straightLineDensify(c,10);let u;try{[u]=p.geodesicLengths([l],"kilometers")}catch{return null}return u},n._getScaleMeasuringPoints=function(e,t){const{width:r,height:n,position:i,spatialReference:s}=e;if(b(e)){const{valid:e}=m.getInfo(s);return[new w(e[0],0,s),new w(e[1],0,s)]}let a=t.y-i[1];a>n?a=n:a<0&&(a=0);const c=o.createScreenPoint(0,a),l=o.createScreenPoint(r,a);return[e.toMap(c),e.toMap(l)]},n._getScaleBarProps=function(e,t,r){const{view:n}=this;let i=e*t/(b(n)?n.state.paddedViewState.worldScreenWidth:n.width),o="metric"===r?"km":"mi";if(i<.1)if("mi"===o){i*=5280,o="ft"}else if("km"===o){i*=1e3,o="m"}let s=0;for(;i>=1;)i/=10,s++;const a=this._getConstraints(i);if(!a)return null;const{min:c,max:l}=a,u=l/i>=i/c?c:l;return{length:e*(u/i),value:10**s*u,unit:o}},n._getConstraints=function(e){return e>.5?{min:.5,max:1}:e>.3?{min:.3,max:.5}:e>.2?{min:.2,max:.3}:e>.15?{min:.15,max:.2}:e>=.1?{min:.15,max:.1}:void 0},e._createClass(r,[{key:"state",get:function(){return this.get("view.ready")&&"2d"===this.get("view.type")?"ready":"disabled"}}]),r}(n);t.__decorate([s.property()],P.prototype,"scaleComputedFrom",void 0),t.__decorate([s.property({readOnly:!0})],P.prototype,"state",null),t.__decorate([s.property()],P.prototype,"view",void 0),P=t.__decorate([u.subclass("esri.widgets.Scalebar.ScaleBarViewModel")],P);return P}));
