(()=>{function e(e,a,n,i,t,r,o,s,c){var u,l=this,d=(_.assign(l,{refresh:function(){return e.refreshLoading=!0,n.detail(d).then(function(e){u=e.data}).then(function(){return n.listArchives(u).then(function(e){l.archives=e.attachments}).catch(function(){l.archives=[]})}).finally(function(){e.refreshLoading=!1})},isActive:function(e){return u.activeVersionId===e.id},toActivate:function(e){return l.toActivateVersionId===e.id},deleteArchive:function(e){return r({title:s("Delete archive"),body:c.getString("You are about to delete archive \"{{name}}{{description ? (' (' + description + ')') : ''}}\". Do you want to proceed?",e),status:"danger",labels:{ok:s("Delete")}}).then(function(){return l.requestInProgress=!0,n.deleteArchive(u,e).then(l.refresh).finally(function(){l.requestInProgress=!1})})},uploadArchive:function(){var t=a.when();6===l.archives.length&&(t=r({title:s("Delete oldest archive and continue"),body:s("Up to 6 archives can be saved in the platform. If you upload a new archive, the oldest archive that is not active will be deleted. Do you want to proceed?"),labels:{ok:s("Delete and continue")}}));return t.then(function(){l.requestInProgress=!0,l.uploadStatus.inProgress=!0,l.uploadStatus.progress=0;var r=_(l.appArchives).map("file").map(_.partial(n.uploadArchive,u)).value();return _.forEach(r,function(t){t.progress(function(e){t.uploadProgress=e.loaded/e.total;e=_(r).map("uploadProgress").sum();l.uploadStatus.progress=100*e/r.length})}),a.all(r).catch(function(e){o.danger(s("Application archive could not be updated."),e),t.reject()}).finally(function(){l.requestInProgress=!1,l.uploadStatus.inProgress=!1,l.uploadStatus.progress=0})}).then(function(e){1===e.length&&(l.refresh(),l.setActive(e[0].data).then(l.refresh).then(function(){var e="APAMA_CEP_RULE"===u.type?s("Application archive uploaded."):s("Application archive uploaded and activated.");o.success(e)}))})},setActive:f,requestInProgress:!1,archives:[],uploadStatus:{inProgress:!1,progress:0}}),t.applicationId),h="/applications/*",p=e.$id;function f(e){var t=e.id||e;return l.toActivateVersionId=t,l.requestInProgress=!0,n.setActiveArchive(u,e).then(function(e){var r=e,n=a.defer();return"APAMA_CEP_RULE"===u.type?(i.addListener(p,h,i.realtimeActions().UPDATE,function(e,t){t.id!==u.id||_.isUndefined(t.state)||(0<t.state.monitors.length?(n.resolve(r.data),i.destroySubscription(p,h)):0<t.state.errors.length&&(f(u.activeVersionId),o.danger(s("Failed to activate application"),t.state.errors[0]),n.reject(t.state.errors[0]),i.destroySubscription(p,h)))}),i.start(p,h)):n.resolve(r.data),n.promise}).then(function(e){u=e}).finally(function(){l.requestInProgress=!1})}e.refresh=l.refresh,e.paging={refresh:!0},n.detail(d).then(function(e){u=e.data,l.refresh()})}e.$inject=["$scope","$q","c8yApplication","c8yRealtime","$routeParams","c8yModal","c8yAlert","gettext","gettextCatalog"],angular.module("c8y.parts.appArchives").controller("ArchivesListCtrl",e)})();