function f(e,a,o){const t=`${o}. ${a}.md`;return`${e}/${t}`}const l="https://api.github.com";function d(e){return e.split("/").map(encodeURIComponent).join("/")}async function g({token:e,owner:a,repo:o,path:t,content:s,message:c}){const u=d(t),p=`${l}/repos/${a}/${o}/contents/${u}`;let n;const r=await fetch(p,{headers:{Authorization:`Bearer ${e}`,Accept:"application/vnd.github+json"}});r.ok&&(n=(await r.json()).sha);const i=await fetch(p,{method:"PUT",headers:{Authorization:`Bearer ${e}`,Accept:"application/vnd.github+json","Content-Type":"application/json"},body:JSON.stringify({message:c,content:btoa(unescape(encodeURIComponent(s))),...n?{sha:n}:{}})});if(!i.ok){const h=await i.text();throw new Error(h)}}chrome.runtime.onInstalled.addListener(async({reason:e})=>{e===chrome.runtime.OnInstalledReason.INSTALL&&console.log("LeetSync installed successfully")});chrome.runtime.onMessage.addListener((e,a,o)=>e.type==="OPEN_SETUP"?(chrome.runtime.openOptionsPage().then(()=>o({success:!0})).catch(t=>o({success:!1,error:t instanceof Error?t.message:"Failed to open setup"})),!0):e.type!=="SAVE_TO_GITHUB"?!1:((async()=>{try{const t=e.payload;if(!t)throw new Error("Missing payload");const{title:s,pattern:c,markdown:u,questionId:p}=t;if(typeof s!="string"||typeof c!="string"||typeof u!="string")throw new Error("Invalid payload data");const{githubToken:n,githubOwner:r,githubRepo:i}=await chrome.storage.local.get(["githubToken","githubOwner","githubRepo"]);if(!n||typeof n!="string")throw new Error("GitHub token not found. Complete setup page first.");if(!r||typeof r!="string")throw new Error("GitHub username not found. Complete setup page first.");if(!i||typeof i!="string")throw new Error("GitHub repository not found. Complete setup page first.");const h=f(c,s,p??"XXXX");await g({token:n.trim(),owner:r.trim(),repo:i.trim(),path:h,content:u,message:`Add ${s}`}),o({success:!0})}catch(t){console.error("GitHub save failed:",t),o({success:!1,error:t instanceof Error?t.message:"Unknown error"})}})(),!0));
