L.TileLayer.addInitHook((function(){if(!this.options.useCache)return this._db=null,void(this._canvas=null);var t=this.options.dbName||"offline-tiles";this.options.dbOptions?this._db=new PouchDB(t,this.options.dbOptions):this._db=new PouchDB(t),this._canvas=document.createElement("canvas"),this._canvas.getContext&&this._canvas.getContext("2d")||(this._canvas=null)})),L.TileLayer.prototype.options.useCache=!1,L.TileLayer.prototype.options.saveToCache=!0,L.TileLayer.prototype.options.useOnlyCache=!1,L.TileLayer.prototype.options.cacheFormat="image/png",L.TileLayer.prototype.options.cacheMaxAge=864e5,L.TileLayer.include({createTile:function(t,i){var e=document.createElement("img");e.onerror=L.bind(this._tileOnError,this,i,e),this.options.crossOrigin&&(e.crossOrigin=""),e.alt="";var o=this.getTileUrl(t);return this.options.useCache&&this._canvas?this._db.get(o,{revs_info:!0},this._onCacheLookup(e,o,i)):e.onload=L.bind(this._tileOnLoad,this,i,e),e.src=o,e},_onCacheLookup:function(t,i,e){return function(o,s){s?(this.fire("tilecachehit",{tile:t,url:i}),Date.now()>s.timestamp+this.options.cacheMaxAge&&!this.options.useOnlyCache?(this.options.saveToCache&&(t.onload=L.bind(this._saveTile,this,t,i,s._revs_info[0].rev,e)),t.crossOrigin="Anonymous",t.src=i,t.onerror=function(t){this.src=s.dataUrl}):(t.onload=L.bind(this._tileOnLoad,this,e,t),t.src=s.dataUrl)):(this.fire("tilecachemiss",{tile:t,url:i}),this.options.useOnlyCache?(t.onload=L.Util.falseFn,t.src=L.Util.emptyImageUrl):(this.options.saveToCache?t.onload=L.bind(this._saveTile,this,t,i,null,e):t.onload=L.bind(this._tileOnLoad,this,e,t),t.crossOrigin="Anonymous",t.src=i))}.bind(this)},_saveTile:function(t,i,e,o){if(null!==this._canvas){var s;this._canvas.width=t.naturalWidth||t.width,this._canvas.height=t.naturalHeight||t.height,this._canvas.getContext("2d").drawImage(t,0,0);try{s=this._canvas.toDataURL(this.options.cacheFormat)}catch(i){return this.fire("tilecacheerror",{tile:t,error:i}),o()}var n={_id:i,dataUrl:s,timestamp:Date.now()};e?this._db.get(i).then(function(t){return this._db.put({_id:t._id,_rev:t._rev,dataUrl:s,timestamp:Date.now()})}.bind(this)).then((function(t){})):this._db.put(n).then((function(t){})),o&&o()}},seed:function(t,i,e){if(this.options.useCache&&!(i>e)&&this._map){for(var o=[],s=i;s<=e;s++)for(var n=this._map.project(t.getNorthEast(),s),a=this._map.project(t.getSouthWest(),s),h=this.getTileSize(),r=L.bounds(L.point(Math.floor(n.x/h.x),Math.floor(n.y/h.y)),L.point(Math.floor(a.x/h.x),Math.floor(a.y/h.y))),l=r.min.y;l<=r.max.y;l++)for(var c=r.min.x;c<=r.max.x;c++)point=new L.Point(c,l),point.z=s,o.push(this._getTileUrl(point));var d={bbox:t,minZoom:i,maxZoom:e,queueLength:o.length};this.fire("seedstart",d);var u=this._createTile();return u._layer=this,this._seedOneTile(u,o,d),this}},_createTile:function(){return new Image},_getTileUrl:function(t){var i=t.z;return this.options.zoomReverse&&(i=this.options.maxZoom-i),i+=this.options.zoomOffset,L.Util.template(this._url,L.extend({r:this.options.detectRetina&&L.Browser.retina&&this.options.maxZoom>0?"@2x":"",s:this._getSubdomain(t),x:t.x,y:this.options.tms?this._globalTileRange.max.y-t.y:t.y,z:this.options.maxNativeZoom?Math.min(i,this.options.maxNativeZoom):i},this.options))},_seedOneTile:function(t,i,e){if(i.length){this.fire("seedprogress",{bbox:e.bbox,minZoom:e.minZoom,maxZoom:e.maxZoom,queueLength:e.queueLength,remainingLength:i.length});var o=i.pop();this._db.get(o,function(s,n){n?this._seedOneTile(t,i,e):(t.onload=function(s){this._saveTile(t,o,null),this._seedOneTile(t,i,e)}.bind(this),t.onerror=function(o){this._seedOneTile(t,i,e)}.bind(this),t.crossOrigin="Anonymous",t.src=o)}.bind(this))}else this.fire("seedend",e)}});