define(["angular","lodash","cjt/util/locale","cjt/modules","cjt/directives/callout","cjt/directives/actionButtonDirective","app/services/domains"],(function(e,n,o){"use strict";return e.module("cpanel.domains").controller("manageDomain",["$scope","$location","$routeParams","$q","$timeout","domains","alertService","DOMAIN_TYPE_CONSTANTS","PAGE",function(t,a,i,r,m,d,c,s,u){function l(){var o=e.copy(t.currentDomain);o.fullDocumentRoot=o.documentRoot;var a=o.documentRoot,i=o.homedir;if(t.requirePublicHTMLSubs&&(i+="/public_html"),a){var r=new RegExp("^"+n.escapeRegExp(i)+"(/)?");a=a.replace(r,""),o.documentRoot=a}return o}function D(){t.deletionDelayed=!0}e.extend(t,{associatedDomains:[],deletionDelayed:!1,currentDomain:d.findDomainByName(i.domain),requirePublicHTMLSubs:"1"===u.requirePublicHTMLSubs.toString(),webserverRoleAvailable:u.hasWebServerRole,mainDomain:d.getMainDomain(),getFormFieldClasses:function(e){return e&&!e.$pristine&&e.$invalid?"col-xs-12 col-md-6":"col-xs-12"},documentRootPattern:d.getDocumentRootPattern(),toggleAdvancedDetailFields:function(){t.showAdvancedDetailFields=!t.showAdvancedDetailFields},update:function(e,a){var i="updating_"+a.domain,m=d.generateFullDocumentRoot(a.documentRoot);if(a.rootDomain){var s=[];return e.newDocumentRoot.$pristine||s.push(d.updateDocumentRoot(a.domain,m).then((function(){c.add({type:"success",id:i,replace:!0,message:o.maketext("You have successfully updated the document root to “[_1]” for the “[_2]” domain.",n.escape(m),a.domain)}),t.workingDomain=l()}),(function(){c.removeById(i)}))),s.length?r.all(s):void 0}c.add({type:"danger",id:i,replace:!0,message:o.maketext("The domain userdata file for the “[_1]” domain appears to be missing.",a.domain)})},confirmingRemoval:!1,removeDomain:function(){if(t.currentDomain.rootDomain){var e=m(D,1e3);return d.remove(t.currentDomain.domain).then((function(){c.add({message:o.maketext("The system removed the “[_1]” domain and any associated redirections.",t.currentDomain.domain),type:"success"}),a.path("/").search("")})).finally((function(){t.deletionDelayed=!1,m.cancel(e)}))}c.add({type:"danger",replace:!0,message:o.maketext("The domain userdata file for the “[_1]” domain appears to be missing.",t.currentDomain.domain)})},startRemovalConfirmation:function(){t.confirmingRemoval=!0},cancelRemoval:function(){t.confirmingRemoval=!1},deleteDependentDomainsError:function(e,n){return o.maketext("The system created the “[_1]” subdomain when it created the “[_2]” domain. To delete this subdomain, you must first delete the following [numerate,_3,domain,domains]:",e,n[0].domain,n.length)}}),t.currentDomain||(c.add({message:o.maketext("You did not specify a domain to manage."),type:"danger"}),a.path("/").search("")),t.showAdvancedDetailFields=!1,t.showSubdomainInDetails=t.currentDomain.type===s.ADDON,s.SUBDOMAIN===t.currentDomain.type&&d.get().then((function(e){e.forEach((function(e){e.subdomain===t.currentDomain.subdomain&&e.type===s.ADDON&&t.associatedDomains.push(e)}))})),t.canUpdateDocumentRoot=t.currentDomain.type!==s.ALIAS,t.workingDomain=l()}])}));