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