function login(a){chrome.runtime.sendMessage({action:"login",data:{ref:a}})}
function upgradeToPro(){chrome.storage.sync.get("uid",function(a){(a=a.uid)?(a=config.host+"/"+config.productId+"/lp.html?uid="+a+"&successURL="+encodeURIComponent(config.host+"/gateway.html?t="+encodeURIComponent(chrome.runtime.getURL("/auth/pay/success.html")))+"&canceledURL="+encodeURIComponent(config.host+"/gateway.html?t="+encodeURIComponent(chrome.runtime.getURL("/auth/pay/fail.html"))),chrome.tabs.create({url:a,active:!0},function(b){})):alert("Login First")})}
async function getrole(a){try{const b=await fetch(config.host+"/api/profile/",{method:"post",headers:{"Content-Type":"application/json"},body:JSON.stringify({product_id:config.productId,user_id:a})});if(b.ok){const c=await b.json();console.log(c);const d=c.data||{};chrome.storage.sync.set({myrole:d},function(){});return d}console.log(`get role info from server failed: : ${b.status}`);a={};chrome.storage.sync.set({myrole:a},function(){});return a}catch(b){console.log(b)}}
async function rolecheck(){var a=(await chrome.storage.sync.get(["uid"])).uid;if(a)return a=await getrole(a),console.log("current role: "+a),a;alert("Login First");a=encodeURIComponent(chrome.runtime.getURL(window.location.pathname));login(a);throw Error("no login");}
function customerportal(){chrome.storage.sync.get("uid",function(a){(a=a.uid)&&fetch(config.host+"/api/pay/stripe/customer-portal",{method:"post",headers:{"Content-Type":"application/json"},body:JSON.stringify({user_id:a,product_id:config.productId,return_url:config.host+"/gateway.html?t="+encodeURIComponent(chrome.runtime.getURL("/help.html"))})}).then(b=>b.json()).then(b=>{chrome.tabs.create({url:b.url,active:!0},function(c){})}).catch(b=>{console.log(b)})})}
async function updateuse(a){var b=(await chrome.storage.sync.get(["uid"])).uid;if(b)try{const c=await fetch(config.host+"/api/profile/use",{method:"post",headers:{"Content-Type":"application/json"},body:JSON.stringify({product_id:config.productId,user_id:b,use:a})});if(c.ok){const d=await c.json();console.log(d)}else console.log(`update usage failed: : ${c.status}`)}catch(c){console.log(c)}else throw alert("Login First"),a=encodeURIComponent(chrome.runtime.getURL(window.location.pathname)),login(a),
Error("no login");};
