define(["angular","lodash","cjt/util/locale","app/services/apiTokens","app/filters/htmlSafeString","app/validators/uniqueTokenName","cjt/modules","cjt/services/alertService","cjt/directives/actionButtonDirective","cjt/services/cpanel/componentSettingSaverService","cjt/directives/validationContainerDirective","cjt/directives/validationItemDirective","cjt/directives/toggleSwitchDirective","cjt/directives/searchDirective","cjt/directives/indeterminateState"],(function(e,t,n,i,r){"use strict";var a="manageAPITokenView",s=n.maketext("Manage [asis,API] Token"),o="cpanel.apiTokens.views.manage",c=["cjt2.directives.validationContainer","cjt2.directives.validationItem","cjt2.directives.toggleSwitch","cjt2.directives.search","cjt2.directives.indeterminateState",r.namespace],u=["$scope","$location","$routeParams","alertService",i.serviceName,"componentSettingSaverService","CAN_CREATE_LIMITED","apiTokens"],l="ManageTokenController",d=function(i,r,o,c,u,l,d,f){i.pageTitle=s,i.RTL=n.is_rtl(),i.showAllHelp=!1,i.ui={confirmingRevocation:!1},i.selectedFeatures=[],i.checkAll={all:!1},i.apiTokens=f,i.working={},i.expiringMessage=function(e){return i.current.expired?n.maketext("[output,strong,Danger]: This [asis,API] token expired on [datetime,_1,datetime_format_medium].",i.current.expiresAt):i.current.expiresSoon?n.maketext("[output,strong,Warning]: This [asis,API] token will expire on [datetime,_1,datetime_format_medium].",i.current.expiresAt):void 0},i.getExpirationLabel=function(e){return e?n.local_datetime(e,"datetime_format_medium"):""},i.init=function(){l.register(a).then((function(e){e&&(i.showAllHelp=e.showAllHelp,i.stayOnView=e.stayOnView)}));var e=o.token,t=u.getTokenById(e);t?(i.canEditFeatureRestrictions=!t.unrestricted||d,i.current=t,i.working={name:t.id,unrestricted:t.unrestricted,features:{}},t.features.forEach((function(e){i.working.features[e]=!0,i.selectedFeatures.push(e)})),i.current.unrestricted||u.getFeatures().then(i._featuresLoaded)):r.path("/")},i._updateCSSS=function(){l.set(a,{showAllHelp:i.showAllHelp})},i._featuresLoaded=function(e){i.features=e},i.toggleHelp=function(){i.showAllHelp=!i.showAllHelp,i._updateCSSS()},i._tokenRenamed=function(e,i){c.success(n.maketext("You have successfully renamed the API Token “[_1]” to “[_2]”.",t.escape(e),t.escape(i)))},i._renameToken=function(e,t){return u.renameToken(e,t).then(i._tokenRenamed.bind(i,e,t))},i._getFeatureById=function(e){for(var t=i.features,n=0;n<t.length;n++)if(t[n].id===e)return t[n]},i._tokenRestrictionUpdated=function(e,r,a){if(r)c.success({message:n.maketext("You have successfully set the [asis,API] token “[_1]” to unrestricted.",t.escape(e)),replace:!1});else{var s=a.map((function(e){return i._getFeatureById(e).label})).map(t.escape);c.success({message:n.maketext("The [asis,API] Token “[_1]” is now limited to the following features: [list_and_quoted,_2].",e,s),replace:!1})}},i.update=function(e){var t,n=i.current,r=e.unrestricted!==i.current.unrestricted||i.selectedFeatures.sort().join()!==i.current.features.sort().join();if(n.id!==e.name&&(t=i._renameToken(n.id,e.name),r||t.finally(i.backToListView)),r){var a=u.updateTokenRestrictions.bind(u,e.name,e.unrestricted,i.selectedFeatures),s=i._tokenRestrictionUpdated.bind(i,e.name,e.unrestricted,i.selectedFeatures);t?t.then(a).then(s):t=a().then(s),t.finally(i.backToListView)}return t},i.unrestrictedToggled=function(){i.working.unrestricted||i.features||u.getFeatures().then(i._featuresLoaded)},i.toggleSelectAllFeatures=function(){i.working.features=i.working.features||{},i.selectedFeatures.length<i.features.length?i.features.forEach((function(e){i.working.features[e.id]=!0})):i.features.forEach((function(e){i.working.features[e.id]=!1})),i.updateSelectedFeatures()},i.getFeaturesIndeterminateState=function(){return i.selectedFeatures.length&&i.features.length&&i.features.length!==i.selectedFeatures.length},i.updateSelectedFeatures=function(){i.selectedFeatures=[],e.forEach(i.working.features,(function(e,t){e&&i.selectedFeatures.push(t)}))},i.backToListView=function(){r.path("/")},i.showRevokeConfirm=function(){i.ui.confirmingRevocation=!0},i.hideRevokeConfirm=function(){i.ui.confirmingRevocation=!1},i._tokenRevoked=function(e){c.success(n.maketext("You have successfully revoked the following [asis,API] [numerate,_1,token,tokens]: “[_1]”",t.escape(e)))},i.revokeToken=function(e){return u.deleteTokens([e.id]).then(i._tokenRevoked.bind(i,e.id)).then(i.backToListView)},i.$on("$destroy",l.unregister.bind(l,a)),i.init()};e.module(o,c).controller(l,u.concat(d));var f={apiTokens:[i.serviceName,function(e){return e.fetchTokens()}]};return{id:"manageAPIToken",route:"/manage",controller:l,class:d,templateUrl:"views/manage.ptt",title:s,namespace:o,showResourcePanel:!0,resolve:f}}));