import{isObject}from"#shared/guards.js";import{deref,isArray}from"#runtime/connections/openapi-schema.js";function resolveSecurity(r,i){let a=isArray(i.security)?i.security:isArray(r.security)?r.security:void 0;if(a===void 0||a.length===0)return;let o=isObject(r.components)?r.components:void 0,s=o!==void 0&&isObject(o.securitySchemes)?o.securitySchemes:void 0;if(s!==void 0)for(let n of a){if(!isObject(n))continue;let i=Object.keys(n)[0];if(i===void 0)continue;let a=s[i],o=isObject(a)?deref(r,a):void 0;if(!isObject(o))continue;let c=mapSecurityScheme(o);if(c!==void 0)return c}}function mapSecurityScheme(e){if(e.type===`apiKey`){let t=e.in;return(t===`header`||t===`query`||t===`cookie`)&&typeof e.name==`string`&&e.name.length>0?{kind:`apiKey`,in:t,name:e.name}:void 0}if(e.type===`http`)return(typeof e.scheme==`string`?e.scheme.toLowerCase():``)===`basic`?{kind:`basic`}:{kind:`bearer`};if(e.type===`oauth2`||e.type===`openIdConnect`)return{kind:`bearer`}}function applySecurity(e,t,n,r,i){if(e===void 0||t.authorization===void 0)return;let a=extractBearerToken(n);if(a!==void 0&&e.kind!==`bearer`){if(e.kind===`basic`){n.Authorization=`Basic ${a}`;return}delete n.Authorization,e.in===`header`?n[e.name]=a:e.in===`query`?r.set(e.name,a):i.push(`${e.name}=${a}`)}}function extractBearerToken(e){let t=e.Authorization??e.authorization;return typeof t==`string`?/^Bearer\s+(.+)$/i.exec(t)?.[1]:void 0}export{applySecurity,resolveSecurity};