import{requireProjectPath}from"../state.js";import{join}from"node:path";import{readFile}from"node:fs/promises";const ENV_FILE_CANDIDATES=[`.env.local`,`.env`];function readEnvValue(e,t){let n=e.trim();if(n.length===0||n.startsWith(`#`)||!n.startsWith(`${t}=`))return;let r=n.slice(t.length+1);if(r.length===0)return;let i=r.length>=2&&r.startsWith(`"`)&&r.endsWith(`"`)?r.slice(1,-1):r;return i.length===0?void 0:i}async function hasApiKeyInFile(e,t){let r;try{r=await readFile(e,`utf8`)}catch{return!1}for(let e of r.split(`
`))if(readEnvValue(e,t)!==void 0)return!0;return!1}async function detectApiKey(e){for(let n of ENV_FILE_CANDIDATES)if(await hasApiKeyInFile(join(e,n),`AI_GATEWAY_API_KEY`))return n}async function detectAiGatewayResolution(e){let t=await detectApiKey(e);return t===void 0?{kind:`unresolved`}:{kind:`api-key`,envFile:t}}function detectAiGateway(){return{id:`detect-ai-gateway`,shouldRun(e){return e.projectPath.kind===`resolved`},async gather(){return null},async perform({state:t}){return detectAiGatewayResolution(requireProjectPath(t))},apply(e,t){return{...e,aiGatewayCredentials:t}}}}export{detectAiGateway,detectAiGatewayResolution};