"use strict";var m=Object.create;var l=Object.defineProperty;var u=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var b=Object.getPrototypeOf;var _=Object.prototype.hasOwnProperty;var h=(i,e)=>l(i,"name",{value:e,configurable:true});var p=(i,e)=>{for(var t in e)l(i,t,{get:e[t],enumerable:true})};var f=(i,e,t,s)=>{if(e&&typeof e==="object"||typeof e==="function"){for(let r of w(e))if(!_.call(i,r)&&r!==t)l(i,r,{get:()=>e[r],enumerable:!(s=u(e,r))||s.enumerable})}return i};var A=(i,e,t)=>(t=i!=null?m(b(i)):{},f(e||!i||!i.__esModule?l(t,"default",{value:i,enumerable:true}):t,i));var E=i=>f(l({},"__esModule",{value:true}),i);var o=(i,e,t,s)=>{var r=s>1?void 0:s?u(e,t):e;for(var g=i.length-1,d;g>=0;g--)if(d=i[g])r=(s?d(e,t,r):d(r))||r;if(s&&r)l(e,t,r);return r};var W={};p(W,{default:()=>a});module.exports=E(W);var n=require("mobx");var c=A(require("../helpers/userAgent-helpers"));const v=require("../preload-safe-debug")("Ferdium:UserAgent");class a{_willNavigateListener=e=>{};_didNavigateListener=e=>{};webview=null;userAgentPref=null;overrideUserAgent=()=>"";constructor(e=null){(0,n.makeObservable)(this);if(typeof e==="function"){this.overrideUserAgent=e}(0,n.observe)(this,"webview",t=>{const{oldValue:s,newValue:r}=t;if(s!==null){this._removeWebviewEvents(s)}if(r!==null){this._addWebviewEvents(r)}})}get defaultUserAgent(){const e=this.overrideUserAgent();if(e.length>0){return e}const t=window["ferdium"].stores.settings.all.app.userAgentPref;if(typeof t==="string"){const s=t.trim();if(s!==""){return s}}return(0,c.default)()}get serviceUserAgentPref(){if(typeof this.userAgentPref==="string"){const e=this.userAgentPref.trim();if(e!==""){return e}}return null}get userAgentWithoutChromeVersion(){const e=this.defaultUserAgent;return e.replace(/Chrome\/[\d.]+/,"Chrome")}get userAgent(){return this.serviceUserAgentPref||this.defaultUserAgent}setWebviewReference(e){this.webview=e}_handleNavigate(e){if(e.startsWith("https://accounts.google.com")){v("Setting user agent to chromeless for url",e);this.webview.userAgent=this.serviceUserAgentPref||this.userAgentWithoutChromeVersion}else{v("Setting user agent to default for url",e);this.webview.userAgent=this.serviceUserAgentPref||this.defaultUserAgent}}_addWebviewEvents(e){v("Adding event handlers");this._willNavigateListener=t=>this._handleNavigate(t.url);e.addEventListener("will-navigate",this._willNavigateListener);this._didNavigateListener=t=>this._handleNavigate(t.url);e.addEventListener("did-navigate",this._didNavigateListener)}_removeWebviewEvents(e){v("Removing event handlers");e.removeEventListener("will-navigate",this._willNavigateListener);e.removeEventListener("did-navigate",this._didNavigateListener)}}h(a,"UserAgent");o([n.observable.ref],a.prototype,"webview",2);o([n.observable],a.prototype,"userAgentPref",2);o([n.observable],a.prototype,"overrideUserAgent",2);o([n.computed],a.prototype,"defaultUserAgent",1);o([n.computed],a.prototype,"serviceUserAgentPref",1);o([n.computed],a.prototype,"userAgentWithoutChromeVersion",1);o([n.computed],a.prototype,"userAgent",1);o([n.action],a.prototype,"setWebviewReference",1);o([n.action],a.prototype,"_handleNavigate",1);
