Registry.require(["promise","icon","helper"],function(){var g=Registry.log,r=Registry.get("promise"),x=Registry.get("icon"),w=Registry.get("helper"),k=rea.FEATURES,y=function(){var a={},d=null,e=0,b,f={init:function(){if(b)return b;var e=r(),f=function(a){d=a||"unknown";g.debug("notify: chronos level",a);e.resolve()};rea.notifications.supported?(rea.notifications.getPermissionLevel(f),rea.notifications.onPermissionLevelChanged.addListener(f),rea.notifications.onClicked.addListener(function(c){g.debug("notify: chronos click",
c);a[c]&&(a[c].cb.click&&a[c].cb.click(),a[c].cancel(),delete a[c])}),rea.notifications.onClosed.addListener(function(c){g.debug("notify: chronos close",c);a[c]&&a[c].cb.close&&a[c].cb.close();delete a[c]})):f("unsupported");return b=e.promise()},create:function(b,p,c,q){return f.init().then(function(){var f=r(),h=10,l=function(){if(d)if("granted"==d){var n={nid:null,cb:{},on:function(a,c){n.cb[a]=c},cancel:function(){},show:function(){var m={type:"basic",title:p||"",message:c||""};m.iconUrl=0==e?
b:"data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==";k.RUNTIME.CHROME&&(70<=k.RUNTIME.BROWSER_VERSION&&void 0!==q.silent&&(m.silent=q.silent),50<=k.RUNTIME.BROWSER_VERSION&&(m.requireInteraction=!0));var d=w.createUUID();rea.notifications.create(d,m,function(){rea.runtime.lastError?1>e?(g.debug("notify: chronos creating failed, retry...",rea.runtime.lastError),e++,n.show()):(g.debug("notify: chronos creating finally failed",rea.runtime.lastError),f.reject()):(g.debug("notify: chronos created",
d),n.cancel=function(){a[d]&&rea.notifications.clear(d,function(){})},a[d]=n)})}};f.resolve(n)}else f.resolve();else{var u=function(){d?l():h--?window.setTimeout(u,200):f.resolve()};u()}};l();return f.promise()})}};return f}(),z=function(){var a={},d=null,e,b={init:function(){if(e)return e;var a=function(a){d=a||"unknown";g.debug("notify: notification level",a)};window.Notification?a(Notification.permission):a("unsupported");return e=r.Pledge()},create:function(f,e,p,c){return b.init().then(function(){var b=
r(),t=function(){if("granted"==d){var h={nid:null,cb:{},on:function(a,c){h.cb[a]=c},cancel:function(){},show:function(){var d={body:p||"",icon:f};k.RUNTIME.CHROME&&(43<=k.RUNTIME.BROWSER_VERSION&&void 0!==c.silent&&(d.silent=c.silent),d.requireInteraction=!0);var b=w.createUUID(),m=new window.Notification(e||"",d);m.onclick=function(){g.debug("notify: notification click",b);a[b]&&(a[b].cb.click&&a[b].cb.click(),a[b].cancel(),delete a[b])};m.onclose=m.onerror=function(){g.debug("notify: notification close",
b);a[b]&&a[b].cb.close&&a[b].cb.close();delete a[b]};h.cancel=function(){a[b]&&m.close()};a[b]=h}};b.resolve(h)}else if("denied"==d)b.resolve();else if("unsupported"==d)b.resolve();else{var l=function(a){d="granted"==a?"granted":"denied";t()};k.RUNTIME.SAFARI?Notification.requestPermission(l):Notification.requestPermission().then(l)}};t();return b.promise()})}};return b}(),v={notify:function(a,d,e,b,f){b=b||{};var k=b.timeout,p=!1;f||(p=!0,f=function(){});var c=null,q=!1,t=!1,h=null,l,n=function(){h&&
window.clearTimeout(h);q||f({});t=!0},u=function(){q=!0;var a={clicked:q};f&&f(a);c&&c.cancel()};e=e||rea.extension.getURL("images/icon128.png");x.getDataUriFromUrl(e).then(function(a){l=a||e;return r.Pledge()}).then(function(){return y.create(l,a,d,b)}).then(function(b){if(!b&&window.webkitNotifications)try{var c=window.webkitNotifications.createNotification(l,a||"",d||"");b={on:function(a,b){c["on"+a]=b},cancel:function(){t||c.cancel()},show:function(){c.show()}}}catch(e){g.warn("notify: webkitNotifications creation failed with: "+
e.message)}return b}).then(function(c){return c?c:z.create(l,a,d,b)}).then(function(b){b||(b={cb:{},on:function(a,c){b.cb[a]=c},cancel:function(){},show:function(){p||window.setTimeout(function(){confirm((a?a+"\n\n":"")+d)?b.cb.click&&b.cb.click():b.cb.close&&b.cb.close()},1)}});return b}).then(function(b){c=b;c.on("close",n);c.on("click",u);h=window.setTimeout(function(){h=null;c.cancel()},k?k:6E5);g.debug("notify:",a,d,e,k);c.show()});return{cancel:function(){c&&c.cancel()}}},showUpdate:function(a,
d,e,b){return v.notify(a,d,e,{timeout:3E5},b)},show:function(a,d,e,b,f){return v.notify(a,d,e,b,f)},highlight:function(a,d){rea.tabs.highlight({tabs:a},d)}};Registry.register("notify","6095",function(){return v})});
