Registry.require("promise helper i18n xmlhttprequest uri permission".split(" "),function(){var d={DEFAULT:"default",OFF:"off",NATIVE:"native",CHROME:"chrome",NOT_ENABLED:"not_enabled",NOT_WHITELISTED:"not_whitelisted",NOT_PERMITTED:"not_permitted",NOT_SUPPORTED:"not_supported",NOT_SUCCEEDED:"not_succeeded"},r=rea.FEATURES,f=Registry.get("promise"),m=Registry.get("helper"),e=Registry.get("permission"),s=Registry.get("i18n"),A=Registry.get("xmlhttprequest"),B=Registry.get("uri"),C=A.run,g=d.OFF,t=[],
k=null,u=!1,n={},v=!1,w=function(){var a=f();Registry.vendor(["vendor/saveas/filesaver"],function(){w=f.Pledge;a.resolve()});return a.promise()},p=function(){return null!==k?f.Pledge(k):e.has(e.permDownloads).then(function(a){k=a;console.log("downs: permission to use rea.downloads ->",a);return p()})},D=function(a,b){if(this[a])this[a]("function"==typeof b?b():b)},q=function(a,b){this[a]&&(D.apply(this,arguments),this[a]=null)},F=function(a,b,c){k&&!v&&r.DOWNLOAD.SUPPORTED&&(rea.downloads.onChanged.addListener(E),
v=!0);c={filename:a.name,body:a.data};["url","method","saveAs","headers"].forEach(function(b){c[b]=a[b]});rea.downloads.download(c,function(c){n[c]={callbacks:b,url:a.url,name:a.name}})},H=function(a,b,c){void 0===b&&(b={});void 0===c&&(c={});var d=function(){return q.apply(b,arguments)};a.responseType="blob";a.method=a.method||"GET";C(a,{onload:function(c){var b=c.responseHeaders?c.responseHeaders.split("\n"):null,e=null,x;for(x in b){var l=b[x].split(":"),G=l.shift()||"",l=l.join(":")||"";"content-type"==
G.trim().toLowerCase()&&(e=l.trim())}c=new Blob([c.response],{type:a.overrideMimeType||e||"binary/octet-stream"});saveAs(c,a.name);d("onload",{})},ondone:function(){d("ondone",{})},onerror:c.onerror,ontimeout:c.ontimeout,onprogress:c.onprogress})},E=function(a){var b=n[a.id];if(b){var c=b.callbacks,e=function(){return q.apply(c,arguments)};a.error?(console.warn("downs: download of",b.name,"("+b.url+")","failed",a.error),e("onerror",{error:d.NOT_SUCCEEDED,details:a.error})):a.endTime&&(console.debug("downs: download of",
b.name,"("+b.url+")","finished"),e("onload",{}),e("ondone",{}),delete n[a.id])}},y=function(a){var b=!1;m.each(t,function(c,e){if(c&&c.length)try{var d;if("/"===c[0])c=c.replace(/^\//g,"").replace(/\/$/g,""),"$"!==c[c.length-1]&&(console.log("downs: patching $ into",c),c+="$"),d=RegExp(c,"i");else if("."===c[0]){var h=[m.escapeForRegExp(c),"$"].join("");d=RegExp(h,"i")}else console.warn("downs: invalid file extension:",'"'+c+'"','starts neither with "." nor with "/"');if(d&&-1!==a.search(d))return console.log("downs:",
c,"matched @",a),b=!0}catch(f){console.warn("downs: can't process",c,f)}});return b},z=function(){return e.has(e.permDownloads).then(function(a){var b=f();u||a?b.resolve({permission:a,asked:!1}):e.ask(e.permDownloads,s.getMessage("Browser_API_Downloads"),s.getMessage("Click_here_to_allow_TM_to_start_downloads")).done(function(a){b.resolve({permission:a,asked:!0})});u=!0;return b.promise()})};Registry.register("downloads","58",{start:function(a,b,c){var e=this,f=arguments,h=function(){return q.apply(b,
arguments)};console.log("downs: start",a);if(g==d.OFF)console.log("downs: feature is not enabled"),h("onerror",{error:d.NOT_ENABLED});else if(a.name&&y(a.name))if(g==d.CHROME)r.DOWNLOAD.SUPPORTED?p().done(function(a){a?F.apply(e,f):(console.log("downs: download permission is missing"),h("onerror",{error:d.NOT_PERMITTED}))}):(console.log("downs: this download mode is not supported"),h("onerror",{error:d.NOT_SUPPORTED}));else{var k=B.parse(a.url);a.name=a.name||"File.download";w().done(function(){if("data:"==
k.protocol){var c=saveAs,b,d=a.url;b=0<=d.split(",")[0].indexOf("base64")?window.atob(d.split(",")[1]):window.unescape(d.split(",")[1]);for(var d=d.split(",")[0].split(":")[1].split(";")[0],h=new Uint8Array(b.length),g=0;g<b.length;g++)h[g]=b.charCodeAt(g);b=new Blob([h],{type:d});c(b,a.name)}else H.apply(e,f)})}else console.log("downs:",a.name,"is not whitelisted"),h("onerror",{error:d.NOT_WHITELISTED})},set_mode:function(a){g=a;g==d.CHROME&&p().done(function(a){a||z().done(function(a){a.permission&&
a.asked&&window.location.reload()})})},set_whitelist:function(a){"Array"===m.toType(a)&&(t=a)},is_whitelisted:y,request_permission:z,remove_permission:function(){return e.remove(e.permDownloads)},staticVars:d})});
