/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../request","../../core/arrayUtils","../../core/Error","../../core/lang"],(function(t,i,e,a,l){"use strict";let o=function(){function t(){this.location={left:0,top:0,width:0,height:0},this._allAvailability="unknown",this.byteSize=40}var o=t.prototype;return o.getAvailability=function(t,i){if("unknown"!==this._allAvailability)return this._allAvailability;const e=(t-this.location.top)*this.location.width+(i-this.location.left),a=e%8,l=e>>3,o=this._tileAvailabilityBitSet;return l<0||l>o.length?"unknown":o[l]&1<<a?"available":"unavailable"},o._updateFromData=function(t){const i=this.location.width,e=this.location.height;let a=!0,l=!0;const o=Math.ceil(i*e/8),n=new Uint8Array(o);let r=0;for(let s=0;s<t.length;s++){const i=s%8;t[s]?(l=!1,n[r]|=1<<i):a=!1,7===i&&++r}l?this._allAvailability="unavailable":a?this._allAvailability="available":(this._allAvailability="unknown",this._tileAvailabilityBitSet=n,this.byteSize+=n.length)},t.fromDefinition=function(l,o){const n=l.service.request||i,{row:s,col:h,width:c,height:f}=l,u={query:{f:"json"}};return o=o?{...u,...o}:u,n(r(l),o).then((t=>t.data)).catch((t=>{if(t&&t.details&&422===t.details.httpStatus)return{location:{top:s,left:h,width:c,height:f},valid:!0,data:e.constant(c*f,0)};throw t})).then((i=>{if(i.location&&(i.location.top!==s||i.location.left!==h||i.location.width!==c||i.location.height!==f))throw new a("tilemap:location-mismatch","Tilemap response for different location than requested",{response:i,definition:{top:s,left:h,width:c,height:f}});return t.fromJSON(i)}))},t.fromJSON=function(i){t._validateJSON(i);const e=new t;return e.location=Object.freeze(l.clone(i.location)),e._updateFromData(i.data),Object.freeze(e)},t._validateJSON=function(t){if(!t||!t.location)throw new a("tilemap:missing-location","Location missing from tilemap response");if(!1===t.valid)throw new a("tilemap:invalid","Tilemap response was marked as invalid");if(!t.data)throw new a("tilemap:missing-data","Data missing from tilemap response");if(!Array.isArray(t.data))throw new a("tilemap:data-mismatch","Data must be an array of numbers");if(t.data.length!==t.location.width*t.location.height)throw new a("tilemap:data-mismatch","Number of data items does not match width/height of tilemap")},t}();function n(t){return`${t.level}/${t.row}/${t.col}/${t.width}/${t.height}`}function r(t){let i;if("vector-tile"===t.service.type)i=`${t.service.url}/tilemap/${t.level}/${t.row}/${t.col}/${t.width}/${t.height}`;else{const e=t.service.tileServers;i=`${e&&e.length?e[t.row%e.length]:t.service.url}/tilemap/${t.level}/${t.row}/${t.col}/${t.width}/${t.height}`}const e=t.service.query;return e&&(i=`${i}?${e}`),i}t.Tilemap=o,t.tilemapDefinitionId=n,Object.defineProperties(t,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
