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