"use strict";document.addEventListener("alpine:init",(()=>{Alpine.data("operatorData",(()=>({debug:{show:!1,msg:""},language:"nl",shuttle_inside_geofence:!1,state:"unknown",state_msg:"Geen berichten..",operator_default:!0,operator_stop:!1,operator_go:!1,record_id:null,init(){this.getOperatorStatus(),setTimeout((()=>{this.getOperatorStatus()}),1e3),setInterval((()=>{this.getOperatorStatus()}),5e3)},handleShuttleInsideGeofenceChange($event){const shuttle_inside_geofence=$event.detail.shuttle_inside_geofence;this.shuttle_inside_geofence=shuttle_inside_geofence,this.debug.msg=`Vehicle inside geofence: ${$event.detail.shuttle_inside_geofence} | Location: (${$event.detail.longitude} ${$event.detail.latitude} r=${event.detail.radius})`,shuttle_inside_geofence?this.notifyOperator():this.changeOperatorStatus("shuttle_geofence_out")},getOperatorStatus(){if(!this.shuttle_inside_geofence)return this.state="unknown",this.state_msg="nl"===this.language?"Geen berichten..":"No messages..",this.operator_default=!0,this.operator_stop=!1,void(this.operator_go=!1);const url_full=new URL("/api/get-status","https://zgwnz2cs55.execute-api.eu-central-1.amazonaws.com");fetch(url_full).then((response=>{if(200===response.status)return response.json();this.shuttle_inside_geofence&&this.notifyOperator()})).then((data=>{if(data){const state=this.handleState(data[0]);this.state=state;const state_msg=this.handleStateMsg(state);switch(this.state_msg=state_msg,state){case"geofence_in":case"operator_confirm":this.operator_default=!1,this.operator_stop=!0,this.operator_go=!1;break;case"operator_go":this.operator_default=!1,this.operator_stop=!1,this.operator_go=!0}}else this.state="unknown",this.state_msg="nl"===this.language?"Geen berichten..":"No messages..",this.operator_default=!0,this.operator_stop=!1,this.operator_go=!1})).catch((error=>{console.error("Error:",error),this.operator_default=!0,this.operator_stop=!1,this.operator_go=!1}))},handleState:data=>!data.shuttle_geofence_in||data.operator_confirm||data.operator_go||data.shuttle_geofence_out?data.shuttle_geofence_in&&data.operator_confirm&&!data.operator_go&&!data.shuttle_geofence_out?"operator_confirm":data.shuttle_geofence_in&&data.operator_confirm&&data.operator_go&&!data.shuttle_geofence_out?"operator_go":"unkown":"geofence_in",handleStateMsg(state){switch(state){case"geofence_in":return"nl"===this.language?"Vehicle bevindt zich binnen geofence..":"Vehicle is inside the geofence.";case"operator_confirm":return"nl"===this.language?"Operator MAPtm heeft uw aanwezigheid bevestigd. Stop en blijf wachten bij het kruispunt..":"The MAPtm operator confirmed your pressence. Please stop and remain at the intersection.";case"operator_go":return"nl"===this.language?"Operator MAPtm geeft aan dat u kunt gaan. Let goed op of u veilig het kruispunt kunt passeren..":"The MAPtm operator informs that you can proceed. Check if you can cross the intersection safely.";default:return"nl"===this.language?"Geen berichten..":"No messages.."}},notifyOperator(){const url_full=new URL("/api/add-status","https://zgwnz2cs55.execute-api.eu-central-1.amazonaws.com"),payload={dt:dayjs().format(),state:"shuttle_geofence_in"};fetch(url_full,{method:"POST",body:JSON.stringify(payload),headers:{"Content-Type":"application/json"}}).then((response=>{if(200===response.status)return response.json()})).then((data=>{data&&(this.record_id=data)})).catch((error=>{console.error("Error:",error)}))},changeOperatorStatus(state){const url_full=new URL("/api/operator/update-status","https://zgwnz2cs55.execute-api.eu-central-1.amazonaws.com"),payload={dt:dayjs().format(),state:state};fetch(url_full,{method:"PATCH",body:JSON.stringify(payload),headers:{"Content-Type":"application/json"}}).then((response=>{if(200===response.status)return response.json()})).then((data=>{data&&(this.record_id=data)})).catch((error=>{console.error("Error:",error)}))},handleLanguageChange($event){console.log($event.detail.language),this.language=$event.detail.language}})))}));