/*
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","../../../core/Accessor","../../../core/accessorSupport/decorators/property","../../../core/arrayUtils","../../../core/has","../../../core/accessorSupport/ensureType","../../../core/accessorSupport/decorators/subclass"],(function(e,t,o,s,i,c,a,r){"use strict";var n;let l=n=function(t){function o(e){var o;return(o=t.call(this,e)||this)._lodByScale={},o._scales=[],o.effectiveLODs=null,o.effectiveMinZoom=-1,o.effectiveMaxZoom=-1,o.effectiveMinScale=0,o.effectiveMaxScale=0,o.lods=null,o.minZoom=-1,o.maxZoom=-1,o.minScale=0,o.maxScale=0,o.snapToZoom=!0,o}e._inheritsLoose(o,t);var s=o.prototype;return s.initialize=function(){let e,{lods:t,minScale:o,maxScale:s,minZoom:i,maxZoom:c}=this,a=-1,r=-1,n=!1,l=!1;if(0!==o&&0!==s&&o<s&&([o,s]=[s,o]),!t||!t.length)return this._set("effectiveMinScale",o),void this._set("effectiveMaxScale",s);t=t.map((e=>e.clone())),t.sort(((e,t)=>t.scale-e.scale)),t.forEach(((e,t)=>e.level=t));for(const f of t)!n&&o>0&&o>=f.scale&&(a=f.level,n=!0),!l&&s>0&&s>=f.scale&&(r=e?e.level:-1,l=!0),e=f;-1===i&&(i=0===o?0:a),-1===c&&(c=0===s?t.length-1:r),i=Math.max(i,0),i=Math.min(i,t.length-1),c=Math.max(c,0),c=Math.min(c,t.length-1),i>c&&([i,c]=[c,i]),o=t[i].scale,s=t[c].scale,t.splice(0,i),t.splice(c-i+1,t.length),t.forEach(((e,t)=>{this._lodByScale[e.scale]=e,this._scales[t]=e.scale})),this._set("effectiveLODs",t),this._set("effectiveMinZoom",i),this._set("effectiveMaxZoom",c),this._set("effectiveMinScale",o),this._set("effectiveMaxScale",s)},s.constrain=function(e,t){if(t&&e.scale===t.scale)return e;const o=this.effectiveMinScale,s=this.effectiveMaxScale,i=e.targetGeometry,c=t&&t.targetGeometry,a=0!==s&&e.scale<s,r=0!==o&&e.scale>o;if(a||r){const a=r?o:s;if(c){const o=(a-t.scale)/(e.scale-t.scale);i.x=c.x+(i.x-c.x)*o,i.y=c.y+(i.y-c.y)*o}e.scale=a}return this.snapToZoom&&this.effectiveLODs&&(e.scale=this._getClosestScale(e.scale)),e},s.fit=function(e){if(!this.effectiveLODs||!this.snapToZoom)return this.constrain(e,null);const t=this.scaleToZoom(e.scale),o=Math.abs(t-Math.floor(t));return e.scale=this.zoomToScale(o>.99?Math.round(t):Math.floor(t)),e},s.zoomToScale=function(e){if(!this.effectiveLODs)return 0;e-=this.effectiveMinZoom,e=Math.max(0,e);const t=this._scales;if(e<=0)return t[0];if(e>=t.length)return t[t.length-1];const o=Math.round(e-.5),s=Math.round(e);return t[s]+(s-e)*(t[o]-t[s])},s.scaleToZoom=function(e){if(!this.effectiveLODs)return-1;const t=this._scales;let o,s;if(e>=t[0])return this.effectiveMinZoom;if(e<=t[t.length-1])return this.effectiveMaxZoom;for(let i=0;i<t.length-1;i++){if(o=t[i],s=t[i+1],s===e){return i+this.effectiveMinZoom+1}if(o>e&&s<e){return i+this.effectiveMinZoom+1-(e-s)/(o-s)}}},s.snapToClosestScale=function(e){if(!this.effectiveLODs)return e;const t=this.scaleToZoom(e);return this.zoomToScale(Math.round(t))},s.snapToNextScale=function(e,t=.5){if(!this.effectiveLODs)return e*t;const o=Math.round(this.scaleToZoom(e));return this.zoomToScale(o+1)},s.snapToPreviousScale=function(e,t=2){if(!this.effectiveLODs)return e*t;const o=Math.round(this.scaleToZoom(e));return this.zoomToScale(o-1)},s.clone=function(){return new n({lods:this.lods,minZoom:this.minZoom,maxZoom:this.maxZoom,minScale:this.minScale,maxScale:this.maxScale})},s._getClosestScale=function(e){return this._lodByScale[e]||(e=this._scales.reduce(((t,o)=>Math.abs(o-e)<=Math.abs(t-e)?o:t),this._scales[0])),this._lodByScale[e].scale},o}(o);t.__decorate([s.property({readOnly:!0})],l.prototype,"effectiveLODs",void 0),t.__decorate([s.property({readOnly:!0})],l.prototype,"effectiveMinZoom",void 0),t.__decorate([s.property({readOnly:!0})],l.prototype,"effectiveMaxZoom",void 0),t.__decorate([s.property({readOnly:!0})],l.prototype,"effectiveMinScale",void 0),t.__decorate([s.property({readOnly:!0})],l.prototype,"effectiveMaxScale",void 0),t.__decorate([s.property()],l.prototype,"lods",void 0),t.__decorate([s.property()],l.prototype,"minZoom",void 0),t.__decorate([s.property()],l.prototype,"maxZoom",void 0),t.__decorate([s.property()],l.prototype,"minScale",void 0),t.__decorate([s.property()],l.prototype,"maxScale",void 0),t.__decorate([s.property()],l.prototype,"snapToZoom",void 0),l=n=t.__decorate([r.subclass("esri.views.2d.constraints.ZoomConstraint")],l);return l}));
