class Service{constructor(e={},t){this.viewModel=t,this.id=ko.observable(e.id||null),this.name=ko.observable(e.name||"").extend({required:!0,maxLength:64}),this.description=ko.observable(e.description||"").extend({required:!1,maxLength:255}),this.endpoint=ko.observable(e.endpoint||"").extend({required:!1,maxLength:255}),this.apikey=ko.observable(e.apikey||"").extend({required:!1}),this.script=ko.observable(e.script||"").extend({required:!0}),this.editingName=ko.observable(!1).extend({throttle:200}),this.showTestResult=ko.observable(!1),this.mouseover=ko.observable(ISTABLET),this.initialState={},this.changeTracker=ko.observable(),this.iconMapping={1:"slack.png",2:"discord.png",3:"pagerduty.png",4:"teams.png",5:"telegram.png"},this.iconSrc=ko.computed((()=>{const e=this.id();return e&&this.iconMapping[e]?`../../img/logos/${this.iconMapping[e]}`:"../../img/logos/default.png"})),this.isModified=ko.pureComputed((()=>{const e=this.serialize();return this.changeTracker(),JSON.stringify(this.initialState)!==JSON.stringify(e)})),this.script.subscribe((e=>{""===e.trim()&&this.showTestResult(!1)})),e.id&&this.setInitialState()}serialize=()=>({id:this.id(),name:this.name(),description:this.description(),endpoint:this.endpoint(),apikey:this.apikey(),script:this.script()});setInitialState=()=>{this.initialState=this.serialize(),this.showTestResult(!1)};resetValidationState=e=>{e.isModified(!1),e.setError(null)};update=async()=>{if(!this.viewModel.services.loading)if(this.validate()){this.editingName(!1);const e=this.id()?"../../admin/alert/service/update":"../../admin/alert/service/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.services.collapseRow(this),this.viewModel.services.createInProgress(!1)}catch(e){MMONIT.error.json(e)}}else this.viewModel.services.expandedRow(this),$("#description"+this.id()+" .description").focus()};test=async()=>{if(this.validate()){this.testInProgress&&4!=this.testInProgress.readyState&&this.testInProgress.abort(),this.showTestResult(!0);const e=$(`#service${this.id()} #testoutput`);e.text("Processing...");try{await $.ajax({url:"../../admin/alert/service/test",data:this.serialize(),type:"POST",dataType:"json",timeout:5e3});e.text("Test message successfully sent")}catch(t){"timeout"===t.statusText?e.text("Test timed out"):0===t.status?e.text("Connection failed. Please check your network or mmonit server."):"abort"!==t.statusText&&e.text(t.responseText||"An unknown error occurred.")}}};editName=(e,t)=>{t.stopPropagation(),this.initialState.name=this.name(),this.editingName(!0)};stopEditName=()=>{this.name().trim().length>0&&(this.editingName(!1),this.initialState.name!==this.name()&&(this.update(),setTimeout((()=>{$(`#description${this.id()} .descSentinel`).focus()}),0)))};validate=()=>this.name.isValid()&&this.script.isValid()}class ServiceList{constructor(e){this.viewModel=e,this.loading=!0,this.list=ko.observableArray(),this.currentService=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.currentService(e),$("#confirmServiceDelete").modal("show")};add=(e,t)=>{if(t.stopPropagation(),!this.createInProgress()){this.createInProgress(!0),this.expandedRow(null);const e=new Service({},this.viewModel);e.editingName(!0),this.list.push(e)}};remove=async(e,t)=>{if(t.stopPropagation(),e.id()>0)try{await $.ajax({url:"../../admin/alert/service/delete",data:{id:e.id()},type:"POST",timeout:5e3}),$("#confirmServiceDelete").modal("hide"),this.list.remove(e)}catch(e){$("#confirmServiceDelete").modal("hide"),MMONIT.error.json(e)}else $("#confirmServiceDelete").modal("hide"),this.createInProgress(!1),this.list.remove(e)};loadServices=async()=>{try{(await $.getJSON("../../admin/alert/service/list")).forEach((e=>{const t=new Service(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.services=new ServiceList(this),this.services.loadServices()};ko.applyBindings(t,document.getElementById("serviceTab")),e(window).on("resize orientationchange",(()=>{t.width(e("#main").outerWidth())}))})(jQuery);