var __extends=this&&this.__extends||function(){var o=function(t,e){return(o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])})(t,e)};return function(t,e){function r(){this.constructor=t}o(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}}();import SourceState from"./State.js";import TileImage from"./TileImage.js";import{applyTransform,intersects}from"../extent.js";import{assert}from"../asserts.js";import{createFromTemplates}from"../tileurlfunction.js";import{createXYZ,extentFromProjection}from"../tilegrid.js";import{get as getProjection,getTransformFromProjections}from"../proj.js";import{jsonp as requestJSONP}from"../net.js";var TileJSON=function(o){function t(t){var e,r=o.call(this,{attributions:t.attributions,cacheSize:t.cacheSize,crossOrigin:t.crossOrigin,imageSmoothing:t.imageSmoothing,projection:getProjection("EPSG:3857"),reprojectionErrorThreshold:t.reprojectionErrorThreshold,state:SourceState.LOADING,tileLoadFunction:t.tileLoadFunction,wrapX:void 0===t.wrapX||t.wrapX,transition:t.transition})||this;return r.tileJSON_=null,r.tileSize_=t.tileSize,t.url?t.jsonp?requestJSONP(t.url,r.handleTileJSONResponse.bind(r),r.handleTileJSONError.bind(r)):((e=new XMLHttpRequest).addEventListener("load",r.onXHRLoad_.bind(r)),e.addEventListener("error",r.onXHRError_.bind(r)),e.open("GET",t.url),e.send()):t.tileJSON?r.handleTileJSONResponse(t.tileJSON):assert(!1,51),r}return __extends(t,o),t.prototype.onXHRLoad_=function(t){t=t.target;if(!t.status||200<=t.status&&t.status<300){var e=void 0;try{e=JSON.parse(t.responseText)}catch(t){return void this.handleTileJSONError()}this.handleTileJSONResponse(e)}else this.handleTileJSONError()},t.prototype.onXHRError_=function(t){this.handleTileJSONError()},t.prototype.getTileJSON=function(){return this.tileJSON_},t.prototype.handleTileJSONResponse=function(e){var t,r,o=getProjection("EPSG:4326"),i=this.getProjection(),n=(void 0!==e.bounds&&(t=getTransformFromProjections(o,i),t=applyTransform(e.bounds,t)),e.minzoom||0),s=e.maxzoom||22,i=createXYZ({extent:extentFromProjection(i),maxZoom:s,minZoom:n,tileSize:this.tileSize_});this.tileGrid=i,this.tileUrlFunction=createFromTemplates(e.tiles,i),void 0===e.attribution||this.getAttributions()||(r=void 0!==t?t:o.getExtent(),this.setAttributions(function(t){return intersects(r,t.extent)?[e.attribution]:null})),this.tileJSON_=e,this.setState(SourceState.READY)},t.prototype.handleTileJSONError=function(){this.setState(SourceState.ERROR)},t}(TileImage);export default TileJSON;