(function(){Worksmobile={oauthTab:null,state:null,callback:null,checkOAuthConfigCallback:function(a,b,d,c,f){Worksmobile.checkOAuthConfig(a,b,d,f).then(e=>{console.log("OAuth configuration check result:",e);c(e)})},focusOAuthWindow:function(){if(Worksmobile.oauthTab&&!Worksmobile.oauthTab.closed)try{Worksmobile.oauthTab.focus(),console.log("Focused existing OAuth window")}catch(a){console.warn("Failed to focus OAuth window, assuming it's closed:",a),Worksmobile.oauthTab=null}else console.log("No OAuth window to focus")}, checkOAuthConfig:async function(a,b,d,c){if(Worksmobile.oauthTab!=null)if(Worksmobile.oauthTab.closed)Worksmobile.oauthTab=null;else if(Worksmobile.oauthTab!=null)return{has_error:!0,error_code:"OAUTH_WINDOW_ALREADY_OPENED",error_message:"OAuth window is already open. Please complete the authentication in that window. If you cannot confirm, please close the window and try again new setting "};({has_error:a,auth_uri:b,state:d}=await this.getOAuthUri(a,b,d));if(a)return{has_error:a,error_message:"Failed to get authentication URL from server", error_code:"OAUTH_URI_ERROR"};Worksmobile.state=d;if(!b)return{has_error:!0,error_message:"No authentication URL received from server",error_code:"OAUTH_URI_ERROR"};Worksmobile.oauthTab=window.open(b,"_blank");if(!Worksmobile.oauthTab)return{has_error:!0,error_message:"Failed to open OAuth window. Please check your popup blocker settings.",error_code:"OAUTH_WINDOW_OPEN_FAILED"};setTimeout(()=>{Worksmobile.oauthTab&&Worksmobile.oauthTab.closed&&(console.log("OAuth window was immediately closed, likely blocked by popup blocker"), Worksmobile.forceCloseOAuth())},100);setTimeout(()=>{Worksmobile.oauthTab&&!Worksmobile.oauthTab.closed&&(console.log("OAuth window timeout - cleaning up after 10 minutes"),Worksmobile.forceCloseOAuth())},6E5);c&&(Worksmobile.callback=c);window.removeEventListener("message",Worksmobile.handleOAuthMessage);window.addEventListener("message",Worksmobile.handleOAuthMessage);return{has_error:!1}},handleOAuthMessage:function(a){if(a.origin===window.location.origin&&(console.log("Received message from OAuth window:", a.data),a.data&&a.data.state===Worksmobile.state)){a.source.postMessage({type:"ACK",ackId:a.data.id},a.origin);if(Worksmobile.oauthTab){console.log("Closing OAuth window");try{Worksmobile.oauthTab.closed||Worksmobile.oauthTab.close()}catch(b){console.warn("Failed to close OAuth window:",b)}finally{Worksmobile.oauthTab=null;try{window.focus()}catch(b){console.warn("Failed to focus main window:",b)}}}a.data.message==="config_success"?(console.log("OAuth authentication successful"),Worksmobile.callback&& Worksmobile.callback(!1,a.data.message)):Worksmobile.callback&&Worksmobile.callback(!0,a.data.message)}},forceCloseOAuth:function(){if(Worksmobile.oauthTab)try{Worksmobile.oauthTab.closed||Worksmobile.oauthTab.close()}catch(a){console.warn("Failed to close OAuth window:",a)}Worksmobile.oauthTab=null;Worksmobile.state=null;Worksmobile.callback=null;window.removeEventListener("message",Worksmobile.handleOAuthMessage);console.log("OAuth window and state forcefully cleaned up")},checkOAuthWindowStatus:function(){return Worksmobile.oauthTab&& Worksmobile.oauthTab.closed?(console.log("OAuth window was closed externally, cleaning up"),Worksmobile.oauthTab=null,Worksmobile.state=null,Worksmobile.callback=null,window.removeEventListener("message",Worksmobile.handleOAuthMessage),!1):Worksmobile.oauthTab!=null},getOAuthUri:async function(a,b,d){if(!a||!b||!d)return{has_error:!0,error_message:"Missing required parameters: domain, clientId, or clientSecret"};try{return await $.ajax({url:"/get-oauth-uri-worksmobile",type:"POST",dataType:"json", timeout:3E4,data:{clientId:b,clientSecret:d,domain:a}})}catch(c){return c.status===0?{has_error:!0,error_message:"Network error: Unable to connect to server"}:c.timeout?{has_error:!0,error_message:"Request timeout: Server did not respond within 30 seconds"}:c.responseJSON&&c.responseJSON.has_error?c.responseJSON:{has_error:!0,error_message:"Server error: "+(c.statusText||"Unknown error"),error_code:"OAUTH_SERVER_ERROR"}}}}})();