Registry.require("promise helper i18n xmlhttprequest uri permission convert".split(" "),function(){var e={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"},g=Registry.log,u=rea.FEATURES,l=Registry.get("promise"),r=Registry.get("helper"),k=Registry.get("permission"),v=Registry.get("i18n"),C=Registry.get("xmlhttprequest"),D=Registry.get("uri"),
E=Registry.get("convert"),F=C.run,m=e.OFF,w=[],p=null,x=!1,n={},y=!1,z=function(){var a=l();Registry.vendor(["vendor/saveas/filesaver"],function(){z=l.Pledge;a.resolve()});return a.promise()},t=function(){return null!==p?l.Pledge(p):!rea.permissions.supported&&rea.downloads.supported?(p=!0,l.Pledge(p)):k.has(k.permDownloads).then(function(a){p=a;g.debug("downs: permission to use rea.downloads ->",a);return t()})},G=function(a,b){if(this[a])this[a]("function"==typeof b?b():b)},q=function(a,b){this[a]&&
(G.apply(this,arguments),this[a]=null)},H=0,I=function(a,b,c){p&&!y&&rea.downloads.supported&&(rea.downloads.onChanged.addListener(A),y=!0);var f={filename:b.name},d;u.RUNTIME.FIREFOX&&52>u.RUNTIME.BROWSER_VERSION?d=["url"]:(d=["url","method","saveAs","headers"],f.body=b.data);d.forEach(function(a){f[a]=b[a]});var h=n[a]={callbacks:c,url:b.url,name:b.name},e=function(){return q.apply(c,arguments)};rea.downloads.download(f,function(a,c){if(void 0===a)e("onerror",{error:c});else{h.id=a;var b=function(c){rea.downloads.cancel(a,
function(){d();c&&c()})},d=function(){rea.downloads.search({id:a},function(c){var b;(b=c[0])?A(b):g.warn("downs: unable to query download ID",a)})};!0===h.cancel?b():(h.cancel=b,h.interval=window.setInterval(d,1E3))}})},J=function(a,b,c){void 0===c&&(c={});var f=function(){return q.apply(c,arguments)};b.responseType="blob";b.method=b.method||"GET";var d=F(b,{onload:function(a){if(4!=a.readyState||200!=a.status&&0!=a.status||a.error)f("onerror",{});else{var c=null;(a.responseHeaders?a.responseHeaders.split("\n"):
[]).forEach(function(a){var b=a.split(":");a=b.shift()||"";b=b.join(":")||"";"content-type"==a.trim().toLowerCase()&&(c=b.trim())});a=new Blob([a.response],{type:b.overrideMimeType||c||"binary/octet-stream"});saveAs(a,b.name);f("onload",{})}},ondone:function(){f("ondone",{});delete n[a]},onerror:c.onerror,ontimeout:c.ontimeout,onprogress:c.onprogress});n[a]={cancel:d?d.abort:function(){}}},A=function(a){var b,c;Object.keys(n).every(function(d){var f;return(f=n[d])&&f.id==a.id?(c=d,b=f,!1):!0});if(b){var f=
b.callbacks,d=function(){return q.apply(f,arguments)},h=function(){b.interval&&(window.clearInterval(b.interval),b.interval=null);delete n[c]};a.error||a.state&&"interrupted"==a.state.current?(g.warn("downs: download of",b.name,"("+b.url+")","failed",a.error),d("onerror",{error:e.NOT_SUCCEEDED,details:a.error}),h()):a.endTime||a.state&&"complete"==a.state.current?(g.debug("downs: download of",b.name,"("+b.url+")","finished"),d("onload",{}),d("ondone",{}),h()):void 0===a.totalBytes&&void 0===a.bytesReceived||
d("onprogress",{loaded:a.bytesReceived,total:a.totalBytes,estimatedEndTime:a.estimatedEndTime})}},B=function(a){var b=!1;r.each(w,function(c,f){if(c&&c.length)try{var d;if("/"===c[0])c=c.replace(/^\//g,"").replace(/\/$/g,""),"$"!==c[c.length-1]&&(g.debug("downs: patching $ into",c),c+="$"),d=new RegExp(c,"i");else if("."===c[0]){var e=[r.escapeForRegExp(c),"$"].join("");d=new RegExp(e,"i")}else g.warn("downs: invalid file extension:",'"'+c+'"','starts neither with "." nor with "/"');if(d&&-1!==a.search(d))return g.debug("downs:",
c,"matched @",a),b=!0}catch(k){g.warn("downs: can't process",c,k)}});return b};Registry.register("downloads","5828",{start:function(a,b){var c=this,f=H++,d=[f].concat(Array.prototype.slice.call(arguments)),h=function(){return q.apply(b,arguments)};g.debug("downs: start",a);if(!a.internal){if(m==e.OFF){g.warn("downs: feature is not enabled");h("onerror",{error:e.NOT_ENABLED});return}if(!a.name||!B(a.name)){g.warn("downs:",a.name,"is not whitelisted");h("onerror",{error:e.NOT_WHITELISTED});
return}if(!rea.downloads.supported&&m==e.CHROME){g.warn("downs: this download mode is not supported");h("onerror",{error:e.NOT_SUPPORTED});return}}(function(){return!rea.downloads.supported||m!=e.CHROME&&m!=e.DEFAULT?l.Breach():t().then(function(a){if(a)I.apply(c,d);else if(m==e.CHROME)g.warn("downs: download permission is missing"),h("onerror",{error:e.NOT_PERMITTED});else return l.Breach()})})().fail(function(){var b=D.parse(a.url);a.name=a.name||"File.download";z().done(function(){"data:"==b.protocol?
saveAs(E.dataURItoBlob(a.url),a.name):J.apply(c,d)})});return f},cancel:function(a){if(a=n[a])return a.cancel?"function"===typeof a.cancel&&a.cancel():a.cancel=!0,!0},set_mode:function(a){m=a;m==e.CHROME&&t().done(function(a){a||k.has(k.permDownloads).then(function(a){var b=l();x||a?b.resolve({permission:a,asked:!1}):k.ask(k.permDownloads,v.getMessage("Browser_API_Downloads"),v.getMessage("Click_here_to_allow_TM_to_start_downloads")).done(function(a){b.resolve({permission:a,asked:!0})});x=!0;return b.promise()}).done(function(a){a.permission&&
a.asked&&rea.page.reload()})})},set_whitelist:function(a){"Array"===r.toType(a)&&(w=a)},is_whitelisted:B,remove_permission:function(){return k.remove(k.permDownloads)},staticVars:e})});
