/*
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","../../core/Error","../../core/maybe","../../geometry/support/scaleUtils","../statistics/spatialStatistics","../support/adapters/support/layerUtils"],(function(e,a,t,n,r,l){"use strict";const s=500,i=1e8,o=1280,c=12,u=30,p=15;function y(e){return f.apply(this,arguments)}function f(){return(f=e._asyncToGenerator((function*(e){const{view:n,sampleSize:r}=e;if(!(e&&n&&e.layer))throw new a("scale-range:missing-parameters","'view' and 'layer' parameters are required");const{layer:i,...o}=e,c=l.createLayerAdapter(i,l.featureCapableLayerTypes),u={layerAdapter:c,...o};if(u.sampleSize=r||s,!c)throw new a("scale-range:invalid-parameters","'layer' must be one of these types: "+l.getLayerTypeLabels(l.featureCapableLayerTypes).join(", "));yield n.when();const p=t.isSome(u.signal)?{signal:u.signal}:null;return yield c.load(p),u}))).apply(this,arguments)}function m(e,a){const t=c,n=o/4,r=u,l=o/4,s=p,i=o/2;let y=null,f=null,m=null,g=null;switch(e){case"point":case"multipoint":{const e=a;y=e.avgMinDistance,f=t,m=e.minDistance,g=n;break}case"polyline":{const e=a;y=e.avgLength,f=r,m=e.minLength,g=l;break}case"polygon":{const e=a;y=e.avgSize,f=s,m=e.minSize,g=i;break}}return{resolutionForMinScale:y>0?y/f:null,resolutionForMaxScale:m>0?m/g:null}}function g(e,a,t){const r=m(e.geometryType,a);return{minScale:n.getScaleForResolution(r.resolutionForMinScale,t.spatialReference),maxScale:n.getScaleForResolution(r.resolutionForMaxScale,t.spatialReference)}}function S(e,a,t=!0){if(e.constraints&&"effectiveLODs"in e.constraints){const n=e.constraints.effectiveLODs,r=t?n:n.slice(0).reverse();let l=null;for(const e of r)if(!(t?e.scale>a:e.scale<a)){l=e;break}return l}}function d(e,t,n,r){const{view:l,snapToLOD:s,layerAdapter:o}=e;if(s){const e=S(l,t),a=S(l,n,!1);t=e?e.scale:t,n=a?a.scale:n}if(t<n)throw new a("scale-range:invalid","calculated minScale is less than maxScale.");return n>t/2&&(n=Math.floor(n/2)),t>i&&(t=0),"polygon"!==o.geometryType&&(n=0),{minScale:Math.ceil(t),maxScale:Math.floor(n),spatialStatistics:r}}function h(e){return v.apply(this,arguments)}function v(){return(v=e._asyncToGenerator((function*(e){const a=yield y(e),{view:t,sampleSize:n,layerAdapter:l,signal:s}=a,i=yield l.getSampleFeatures({view:t,sampleSize:n,returnGeometry:!0,signal:s}),o=yield r({features:i,geometryType:l.geometryType}),{minScale:c,maxScale:u}=g(l,o,t);return d(a,c,u,o)}))).apply(this,arguments)}return h}));
