import{isPrimitive as v}from"@redocly/theme/core/openapi";import{getOperation as O}from"../../models/index.js";import{normalizeOptions as b,OpenAPIParser as E}from"../../services/index.js";import{JsonPointer as w}from"../../utils/index.js";function V(t,r,n,a,e,o,i,m){if(!n)return;const u=new E(n),c=u.definition.paths?.[t];let d=c?.[r];!d&&c?.additionalOperations&&(d=c.additionalOperations[r]);const s=O(u,{...d,pathName:t,httpVerb:r,pathParameters:c.parameters||[],pathServers:c.servers||[]},void 0,m??b({}),"",{isCallback:!1},a,i);if(!s)return;const h=o?.activeMimeName||s.requestBody?.content?.mediaTypes[0]?.name||"application/json";return{id:s.id,name:s.name,method:s.httpVerb,operationId:s.operationId,parameters:y(s.parameters,e?.requestValues),body:q(s.requestBody,n,e?.requestValues?.body?{activeExampleName:e?.activeExampleName,[h]:e?.requestValues?.body}:e?.activeExampleName?{activeExampleName:e?.activeExampleName}:void 0),path:s.path,href:s.href,servers:s.servers,security:I(s.security),responses:Object.fromEntries(s.responses.map(l=>[l.code,{code:l.code,description:l.description,mediaTypes:l.content?.mediaTypes.reduce((p,f)=>(p[f.name]={name:f.name,schema:x(f.schema?.rawSchema,n)},p),{})}]))}}function k(t,r,n){return function(a,e,o){const i=V(a,e,o,t,void 0,void 0,r,n);if(i)return{...i,operationId:i.operationId||w.compile(["paths",i.path,i.method])}}}function y(t,r){const n=new Set,a=t.map(e=>(n.add(e.in+":"+e.name),{name:e.name,in:e.in,required:e.required,description:e.description,example:r?.[String(e.in)]?.[e.name]||e.example,schema:e.schema,...e.allowReserved&&{allowReserved:e.allowReserved}}));for(const[e,o]of Object.entries(r||{}))for(const[i,m]of Object.entries(o||{}))n.has(e+":"+i)||a.push({name:i,in:e,required:!1,description:"",example:String(m),schema:void 0});return a}function g({type:t,format:r,enum:n}){return t==="string"&&(r==="binary"||r==="base64")?"file":t==="object"?"multiline":n.length>0?"select":"text"}function N(t,r){const n=t[0]?.example||t[0]?.examples;if(n)return n;if(t.schema?.example)return t.schema.example;const a=r?.default?.value||r?.default?.rawValue;return a?.[t.name]?v(a?.[t.name])?String(a?.[t.name]):JSON.stringify(a?.[t.name]):""}function S(t){if(t?.fields)return t.fields;if(t?.oneOf){for(const r of t.oneOf)if(r?.fields)return r.fields}}function q(t,r,n){if(!t||!t.content?.mediaTypes){if(!n)return;const{activeExampleName:a,...e}=n;return{activeExampleName:a,mediaTypes:Object.fromEntries(Object.entries(e).map(([o,i])=>[o,{name:o,examples:{default:{value:i}},schema:{},parameters:[]}]))}}return{required:t.required,activeExampleName:n?.activeExampleName,mediaTypes:t.content.mediaTypes.reduce((a,e)=>{const o=e.schema,i=n?.[e.name]?{default:{value:n?.[e.name],rawValue:n?.[e.name],mime:e.name}}:e.examples||e.formExamples,m=S(o);return a[e.name]={name:e.name,examples:i,schema:x(e.schema?.rawSchema,r),parameters:m?.map(u=>({inputType:g(u.schema),name:u.name,example:N(u,i)}))},a},{})}}function x(t,r){if(!(!t||!r))return{...t,components:r?.components||{}}}function I(t){if(t)return t.map(({schemes:r})=>r.map(n=>{const a={id:n.id,type:n.type,in:n.in,openIdConnectUrl:n.openIdConnectUrl,scopes:n.scopes,scheme:n.scheme,name:n.name,flows:n.flows,serverValues:n.serverValues,"x-defaultAccessToken":n["x-defaultAccessToken"],"x-defaultTokenType":n["x-defaultTokenType"],"x-defaultClientId":n["x-defaultClientId"],"x-defaultClientSecret":n["x-defaultClientSecret"],"x-defaultUsername":n["x-defaultUsername"],"x-defaultPassword":n["x-defaultPassword"]};return Object.keys(a).forEach(e=>a[e]===void 0&&delete a[e]),a}))}export{V as convertOperationToReplayValue,k as convertOperationToReplayValueForRedoc,I as convertRequestSecurity};
