const e=require(`./paths-Bcdg3ma0.cjs`);let t=require(`node:path`);t=e.p(t);let n=require(`node:fs`);n=e.p(n);function r(e){try{if(!n.default.existsSync(e))return;let t=n.default.readFileSync(e,`utf8`);return JSON.parse(t)}catch{return}}function i(e,r){let i=t.default.dirname(e);n.default.existsSync(i)||n.default.mkdirSync(i,{recursive:!0,mode:448}),n.default.writeFileSync(e,`${JSON.stringify(r,null,2)}\n`,`utf8`),n.default.chmodSync(e,384)}var a=e.f({DEFAULT_COPILOT_API_BASE_URL:()=>u,deriveCopilotApiBaseUrlFromToken:()=>d,resolveCopilotApiToken:()=>f});const o=`https://api.github.com/copilot_internal/v2/token`;function s(n=process.env){return t.default.join(e.c(n),`credentials`,`github-copilot.token.json`)}function c(e,t=Date.now()){return e.expiresAt-t>300*1e3}function l(e){if(!e||typeof e!=`object`)throw Error(`Unexpected response from GitHub Copilot token endpoint`);let t=e,n=t.token,r=t.expires_at;if(typeof n!=`string`||n.trim().length===0)throw Error(`Copilot token response missing token`);let i;if(typeof r==`number`&&Number.isFinite(r))i=r>1e10?r:r*1e3;else if(typeof r==`string`&&r.trim().length>0){let e=Number.parseInt(r,10);if(!Number.isFinite(e))throw Error(`Copilot token response has invalid expires_at`);i=e>1e10?e:e*1e3}else throw Error(`Copilot token response missing expires_at`);return{token:n,expiresAt:i}}const u=`https://api.individual.githubcopilot.com`;function d(e){let t=e.trim();if(!t)return null;let n=t.match(/(?:^|;)\s*proxy-ep=([^;\s]+)/i)?.[1]?.trim();if(!n)return null;let r=n.replace(/^https?:\/\//,``).replace(/^proxy\./i,`api.`);return r?`https://${r}`:null}async function f(e){let t=e.env??process.env,n=e.cachePath?.trim()||s(t),a=e.loadJsonFileImpl??r,u=e.saveJsonFileImpl??i,f=a(n);if(f&&typeof f.token==`string`&&typeof f.expiresAt==`number`&&c(f))return{token:f.token,expiresAt:f.expiresAt,source:`cache:${n}`,baseUrl:d(f.token)??`https://api.individual.githubcopilot.com`};let p=await(e.fetchImpl??fetch)(o,{method:`GET`,headers:{Accept:`application/json`,Authorization:`Bearer ${e.githubToken}`}});if(!p.ok)throw Error(`Copilot token exchange failed: HTTP ${p.status}`);let m=l(await p.json()),h={token:m.token,expiresAt:m.expiresAt,updatedAt:Date.now()};return u(n,h),{token:h.token,expiresAt:h.expiresAt,source:`fetched:${o}`,baseUrl:d(h.token)??`https://api.individual.githubcopilot.com`}}Object.defineProperty(exports,`a`,{enumerable:!0,get:function(){return i}}),Object.defineProperty(exports,`i`,{enumerable:!0,get:function(){return r}}),Object.defineProperty(exports,`n`,{enumerable:!0,get:function(){return a}}),Object.defineProperty(exports,`r`,{enumerable:!0,get:function(){return f}}),Object.defineProperty(exports,`t`,{enumerable:!0,get:function(){return u}});
