import{isPrimitive as h}from"@redocly/theme/core/openapi";import{getOperation as v}from"../../models/index.js";import{normalizeOptions as O,OpenAPIParser as b}from"../../services/index.js";import{JsonPointer as E}from"../../utils/index.js";function V(t,r,n,a,e,i,m){if(!n)return;const o=new b(n),u=o.definition.paths?.[t];let l=u?.[r];!l&&u?.additionalOperations&&(l=u.additionalOperations[r]);const s=v(o,{...l,pathName:t,httpVerb:r,pathParameters:u.parameters||[],pathServers:u.servers||[]},void 0,O({}),"",{isCallback:!1},a,m);if(!s)return;const x=i?.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:S(s.requestBody,n,e?.requestValues?.body?{activeExampleName:e?.activeExampleName,[x]:e?.requestValues?.body}:e?.activeExampleName?{activeExampleName:e?.activeExampleName}:void 0),path:s.path,href:s.href,servers:s.servers,security:q(s.security),responses:Object.fromEntries(s.responses.map(c=>[c.code,{code:c.code,description:c.description,mediaTypes:c.content?.mediaTypes.reduce((d,f)=>(d[f.name]={name:f.name,schema:p(f.schema?.rawSchema,n)},d),{})}]))}}function R(t,r){return function(n,a,e){const i=V(n,a,e,t,void 0,void 0,r);if(i)return{...i,operationId:i.operationId||E.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}));for(const[e,i]of Object.entries(r||{}))for(const[m,o]of Object.entries(i||{}))n.has(e+":"+m)||a.push({name:m,in:e,required:!1,description:"",example:String(o),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]?h(a?.[t.name])?String(a?.[t.name]):JSON.stringify(a?.[t.name]):""}function w(t){if(t?.fields)return t.fields;if(t?.oneOf){for(const r of t.oneOf)if(r?.fields)return r.fields}}function S(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(([i,m])=>[i,{name:i,examples:{default:{value:m}},schema:{},parameters:[]}]))}}return{required:t.required,activeExampleName:n?.activeExampleName,mediaTypes:t.content.mediaTypes.reduce((a,e)=>{const i=e.schema,m=n?.[e.name]?{default:{value:n?.[e.name],rawValue:n?.[e.name],mime:e.name}}:e.examples||e.formExamples,o=w(i);return a[e.name]={name:e.name,examples:m,schema:p(e.schema?.rawSchema,r),parameters:o?.map(u=>({inputType:g(u.schema),name:u.name,example:N(u,m)}))},a},{})}}function p(t,r){if(!(!t||!r))return{...t,components:r?.components||{}}}function q(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,R as convertOperationToReplayValueForRedoc,q as convertRequestSecurity};
