var __rest=this&&this.__rest||function(e,t){var n={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t.indexOf(r)<0&&(n[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var a=0;for(r=Object.getOwnPropertySymbols(e);a<r.length;a++)t.indexOf(r[a])<0&&Object.prototype.propertyIsEnumerable.call(e,r[a])&&(n[r[a]]=e[r[a]])}return n};import{isPrimitive}from"@redocly/theme";import{getOperation}from"../../models";import{normalizeOptions,OpenAPIParser}from"../../services";import{JsonPointer}from"../../utils";export function convertOperationToReplayValue(e,t,n,r){var a;if(!n)return;const o=new OpenAPIParser(n),i=null===(a=o.definition.paths)||void 0===a?void 0:a[e],s=null==i?void 0:i[t],l=getOperation(o,Object.assign(Object.assign({},s),{pathName:e,httpVerb:t,pathParameters:i.parameters||[],pathServers:i.servers||[]}),void 0,normalizeOptions({}),"",{isCallback:!1},r);if(!l)return;const m={id:l.id,name:l.name,method:l.httpVerb,operationId:l.operationId,parameters:convertParameters(l.parameters),body:convertRequestBody(l.requestBody,n),path:l.path,href:l.href,servers:l.servers,security:convertRequestSecurity(l.security),responses:Object.fromEntries(l.responses.map((e=>{var t;return[e.code,{code:e.code,description:e.description,mediaTypes:null===(t=e.content)||void 0===t?void 0:t.mediaTypes.reduce(((e,t)=>{var r;return e[t.name]={name:t.name,schema:getBodySchema(null===(r=t.schema)||void 0===r?void 0:r.rawSchema,n)},e}),{})}]})))};return m}export function convertOperationToReplayValueForRedoc(e){return function(t,n,r){const a=convertOperationToReplayValue(t,n,r,e);if(a)return Object.assign(Object.assign({},a),{operationId:a.operationId||JsonPointer.compile(["paths",a.path,a.method])})}}function convertParameters(e,t){const n=new Set,r=e.map((e=>{var r;return n.add(e.in+":"+e.name),{name:e.name,in:e.in,required:e.required,description:e.description,example:(null===(r=null==t?void 0:t[String(e.in)])||void 0===r?void 0:r[e.name])||e.example,schema:e.schema}}));for(const[e,a]of Object.entries(t||{}))for(const[t,o]of Object.entries(a||{}))n.has(e+":"+t)||r.push({name:t,in:e,required:!1,description:"",example:String(o),schema:void 0});return r}function getInputType({type:e,format:t,enum:n}){return"string"!==e||"binary"!==t&&"base64"!==t?"object"===e?"multiline":n.length>0?"select":"text":"file"}function getFieldExamples(e,t){var n,r,a,o,i;const s=(null===(n=e[0])||void 0===n?void 0:n.example)||(null===(r=e[0])||void 0===r?void 0:r.examples);if(s)return s;if(null===(a=e.schema)||void 0===a?void 0:a.example)return e.schema.example;const l=(null===(o=null==t?void 0:t.default)||void 0===o?void 0:o.value)||(null===(i=null==t?void 0:t.default)||void 0===i?void 0:i.rawValue);return(null==l?void 0:l[e.name])?isPrimitive(null==l?void 0:l[e.name])?String(null==l?void 0:l[e.name]):JSON.stringify(null==l?void 0:l[e.name]):""}function convertRequestBody(e,t,n){var r;if(!e||!(null===(r=e.content)||void 0===r?void 0:r.mediaTypes)){if(!n)return;const{activeExampleName:e}=n,t=__rest(n,["activeExampleName"]);return{activeExampleName:e,mediaTypes:Object.fromEntries(Object.entries(t).map((([e,t])=>[e,{name:e,examples:{default:{value:t}},schema:{},parameters:[]}])))}}return{required:e.required,activeExampleName:null==n?void 0:n.activeExampleName,mediaTypes:e.content.mediaTypes.reduce(((e,r)=>{var a,o;const i=r.schema,s=(null==n?void 0:n[r.name])?{default:{value:null==n?void 0:n[r.name],rawValue:null==n?void 0:n[r.name],mime:r.name}}:r.examples||r.formExamples;return e[r.name]={name:r.name,examples:s,schema:getBodySchema(null===(a=r.schema)||void 0===a?void 0:a.rawSchema,t),parameters:null===(o=null==i?void 0:i.fields)||void 0===o?void 0:o.map((e=>({inputType:getInputType(e.schema),name:e.name,example:getFieldExamples(e,s)})))},e}),{})}}function getBodySchema(e,t){if(e&&t)return Object.assign(Object.assign({},e),{components:(null==t?void 0:t.components)||{}})}export function convertRequestSecurity(e){if(e)return e.map((({schemes:e})=>e.map((e=>{const t={id:e.id,type:e.type,in:e.in,openIdConnectUrl:e.openIdConnectUrl,scopes:e.scopes,scheme:e.scheme,name:e.name,flows:e.flows,"x-defaultAccessToken":e["x-defaultAccessToken"],"x-defaultTokenType":e["x-defaultTokenType"],"x-defaultClientId":e["x-defaultClientId"],"x-defaultClientSecret":e["x-defaultClientSecret"],"x-defaultUsername":e["x-defaultUsername"],"x-defaultPassword":e["x-defaultPassword"]};return Object.keys(t).forEach((e=>void 0===t[e]&&delete t[e])),t}))))}
//# sourceMappingURL=utils.js.map