class JabberServer{constructor(e={},t){this.viewModel=t,this.id=ko.observable(e.id||null),this.hostname=ko.observable(e.hostname||"").extend({required:!0,maxLength:255}),this.port=ko.observable(e.port||null).extend({required:!0,number:!0,min:1,max:65535,step:1}),this.username=ko.observable(e.username||"").extend({required:!0,maxLength:255}),this.password=ko.observable(e.password||"").extend({required:!0,maxLength:255}),this.editingHostName=ko.observable(!1).extend({throttle:200}),this.showTestResult=ko.observable(!1),this.mouseover=ko.observable(ISTABLET),this.initialState={},this.changeTracker=ko.observable(),this.isModified=ko.pureComputed((()=>{const e=this.serialize();return this.changeTracker(),JSON.stringify(this.initialState)!==JSON.stringify(e)})),e.id&&this.setInitialState()}serialize=()=>({id:this.id(),hostname:this.hostname(),port:this.port(),username:this.username(),password:this.password()});setInitialState=()=>{this.initialState=this.serialize(),this.showTestResult(!1)};resetValidationState=e=>{e.isModified(!1),e.setError(null)};update=async()=>{if(!this.viewModel.jabberservers.loading)if(this.validate()){this.editingHostName(!1);const e=this.id()?"../../admin/alert/jabber/update":"../../admin/alert/jabber/create";try{const t=await $.ajax({url:e,data:this.serialize(),type:"POST",dataType:"json"});this.id()||this.id(t.id),this.setInitialState(),this.changeTracker.valueHasMutated(),this.viewModel.jabberservers.collapseRow(this),this.viewModel.jabberservers.createInProgress(!1)}catch(e){MMONIT.error.json(e)}}else this.viewModel.jabberservers.expandedRow(this),$("#jabberserver"+this.id()+" .jabberPort").focus()};test=async()=>{if(this.validate()){this.testInProgress&&4!=this.testInProgress.readyState&&this.testInProgress.abort(),this.showTestResult(!0);const e=$(`#jabberserver${this.id()} .testResult .accessory`);e.html('<svg class="spinner-button active" style="width:16px;height:16px;"><use href="#spinner-icon" class="spinner-arrows" style="fill:orange;"></use></svg>');try{const t=await $.ajax({url:"../../admin/alert/jabber/test",data:this.serialize(),type:"POST",dataType:"json",timeout:5e3});e.css("color",0==t.status?"red":"green"),e.text(t.description)}catch(t){"timeout"===t.statusText?(e.css("color","red"),e.text("Test timed out")):"abort"!==t.statusText&&MMONIT.error.json(t)}}};editHostName=(e,t)=>{t.stopPropagation(),this.initialState.hostname=this.hostname(),this.editingHostName(!0)};stopEditHostName=()=>{this.hostname().trim().length>0&&(this.editingHostName(!1),this.initialState.hostname!==this.hostname()&&(this.update(),setTimeout((()=>{$(`#jabberserver${this.id()} .portSentinelClass`).focus()}),0)))};validate=()=>this.hostname.isValid()&&this.port.isValid()&&this.username.isValid()&&this.password.isValid()}class JabberServerList{constructor(e){this.viewModel=e,this.loading=!0,this.list=ko.observableArray(),this.currentJabberServer=ko.observable(),this.expandedRow=ko.observable(),this.createInProgress=ko.observable(!1)}toggleExpand=(e,t)=>{t.stopPropagation(),this.expandedRow(this.expandedRow()!==e?e:null)};collapseRow=e=>{this.expandedRow()===e&&(e.setInitialState(),this.expandedRow(null))};isExpanded=e=>e===this.expandedRow();enableMouseover=(e,t)=>{t.stopPropagation(),ISTABLET||e.mouseover(!0)};disableMouseover=(e,t)=>{t.stopPropagation(),ISTABLET||e.mouseover(!1)};confirmDelete=(e,t)=>{t.stopPropagation(),this.currentJabberServer(e),$("#confirmJabberServerDelete").modal("show")};add=(e,t)=>{if(t.stopPropagation(),!this.createInProgress()){this.createInProgress(!0),this.expandedRow(null);const e=new JabberServer({},this.viewModel);e.editingHostName(!0),this.list.push(e)}};remove=async(e,t)=>{if(t.stopPropagation(),e.id()>0)try{await $.ajax({url:"../../admin/alert/jabber/delete",data:{id:e.id()},type:"POST",timeout:5e3}),$("#confirmJabberServerDelete").modal("hide"),this.list.remove(e)}catch(e){$("#confirmJabberServerDelete").modal("hide"),MMONIT.error.json(e)}else $("#confirmJabberServerDelete").modal("hide"),this.createInProgress(!1),this.list.remove(e)};loadJabberServers=async()=>{try{(await $.getJSON("../../admin/alert/jabber/list")).forEach((e=>{const t=new JabberServer(e,this.viewModel);this.list.push(t)})),this.loading=!1}catch(e){MMONIT.error.json(e)}}}(e=>{ko.validation.init({insertMessages:!1,decorateElement:!0,errorElementClass:"error"},!0);const t=new function(){this.width=ko.observable(e("#main").outerWidth()),this.jabberservers=new JabberServerList(this),this.jabberservers.loadJabberServers()};ko.applyBindings(t,document.getElementById("xmppTab")),e(window).on("resize orientationchange",(()=>{t.width(e("#main").outerWidth())}))})(jQuery);