/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import t from"../../request.js";import i from"../../core/Error.js";import{clone as e}from"../../core/lang.js";import{estimateNumberArrayMemory as a}from"../../core/memoryEstimations.js";import{newUintArray as o}from"../../geometry/support/UintArray.js";class l{constructor(t){s(t);const{location:i,data:l}=t;this.location=Object.freeze(e(i));const r=this.location.width,n=this.location.height;let h=!0,c=!0;const m=Math.ceil(r*n/32),f=o(m);let w=0;for(let e=0;e<l.length;e++){const t=e%32;l[e]?(c=!1,f[w]|=1<<t):h=!1,31===t&&++w}c?(this._availability="unavailable",this.byteSize=40):h?(this._availability="available",this.byteSize=40):(this._availability=f,this.byteSize=40+a(f))}getAvailability(t,i){if("unavailable"===this._availability||"available"===this._availability)return this._availability;const e=(t-this.location.top)*this.location.width+(i-this.location.left),a=e%32,o=e>>5,l=this._availability;return o<0||o>l.length?"unknown":l[o]&1<<a?"available":"unavailable"}static fromDefinition(e,a){const o=e.service.request||t,{row:r,col:s,width:h,height:c}=e,m={query:{f:"json"}};return a=a?{...m,...a}:m,o(n(e),a).then(t=>t.data).catch(t=>{if(422===t?.details?.httpStatus)return{location:{top:r,left:s,width:h,height:c},valid:!0,data:new Array(h*c).fill(0)};throw t}).then(t=>{if(t.location&&(t.location.top!==r||t.location.left!==s||t.location.width!==h||t.location.height!==c))throw new i("tilemap:location-mismatch","Tilemap response for different location than requested",{response:t,definition:{top:r,left:s,width:h,height:c}});return l.fromJSON(t)})}static fromJSON(t){return Object.freeze(new l(t))}}function r(t){return`${t.level}/${t.row}/${t.col}/${t.width}/${t.height}`}function n(t){let i;if(t.service.tileServers?.length){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}`}else i=`${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}function s(t){if(!t?.location)throw new i("tilemap:missing-location","Location missing from tilemap response");if(!1===t.valid)throw new i("tilemap:invalid","Tilemap response was marked as invalid");if(!t.data)throw new i("tilemap:missing-data","Data missing from tilemap response");if(!Array.isArray(t.data))throw new i("tilemap:data-mismatch","Data must be an array of numbers");if(t.data.length!==t.location.width*t.location.height)throw new i("tilemap:data-mismatch","Number of data items does not match width/height of tilemap")}export{l as Tilemap,r as tilemapDefinitionId};
