(()=>{function e(){return{restrict:"EA",scope:{user:"=",allSelected:"=?",allowedApplications:"=",disabledEdit:"=?"},templateUrl:":::PLUGIN_PATH:::/applicationAccess/applicationAccess.html",controller:i}}function i(l,e,i){function t(e){return _.uniqBy(e,"id")}function n(e){l.applications=_.filter(e,function(e){return"MICROSERVICE"!==e.type}),l.marketApps=_.filter(l.applications,{availability:"MARKET"}),l.privateApps=_.filter(l.applications,{availability:"PRIVATE"})}function a(e){return l.allSelected||_.some(l.allowedApplications,c(e))||r(e)}function c(i){return function(e){return _.toNumber(i.id)===_.toNumber(e.id)}}function r(e){var i,e=e.id,t=l.user;return t&&!l.allSelected&&(t=_(_.get(t,"groups.references")).map("group").map("applications").flatten().uniqBy("id").map("id").value(),i=_.includes(t,e)),i}function o(e){var i=_.get(l,"user._owner.applications");return i&&!r(e)&&!_.some(i,{id:e.id})}l.isAccessAllowed=a,l.toggleAccess=function(e){(a(e)?e=>{l.allowedApplications=_.reject(l.allowedApplications,c(e))}:e=>{l.allowedApplications=l.allowedApplications||[],l.allowedApplications.push({id:e.id,type:e.type})})(e)},l.alreadyInGlobalRole=r,l.isDisabled=function(e){return l.allSelected||l.disabledEdit||r(e)||o(e)},l.notAvailableInOwner=o,l.noOwnerAvailable=function(){return _.get(l,"user")&&!_.get(l,"user._owner")},e.current().then(i.listByUser).then(t).then(n)}i.$inject=["$scope","c8yUser","c8yApplication"],angular.module("c8y.users").directive("c8yApplicationAccess",e).directive("applicationAccessEditor",e)})();