import*as e from"react";import{StoreProvider as O,RequestSamples as E,ResponseSamples as S,normalizeOptions as A}from"@redocly/openapi-docs";import{ErrorMessage as M,OpenApiComponentWrap as k}from"../styled.elements";import{useStore as B}from"../../hooks/use-store";import{getOperation as F}from"./get-operation/index";import{getMockServerDocsConfig as I}from"../../../../openapi-docs/template/mock-server-config.js";function W(v){const{descriptionFile:b,operationId:l,pointer:c,exampleKey:n,mimeType:u,language:s,requestBody:T,parameters:C,options:d,environment:i,environments:p}=v,{store:o,error:R,baseSlug:f}=B(b),[r,h]=e.useState(),[q,g]=e.useState(),m=e.useMemo(()=>({...o?.options,...d,...(s||null)&&{codeSamples:{languages:[{lang:s}]},mockServer:I(o?.options?.mockServer,f||"")}}),[s,d,o?.options,f]),t=e.useMemo(()=>{if(o)return F(o,{operationId:l,pointer:c},A(m),{exampleKey:n,mimeType:u,environments:p,environment:i})},[n,m,u,l,c,o,p,i]);e.useEffect(()=>{if(n){if(t?.requestBody?.content?.mediaTypes){const{mediaTypes:a}=t.requestBody.content;if(a.some(x=>x.examples?.[n])){g("request");return}}g("")}},[n,t]);const y=e.useCallback(a=>{r!==a&&h(a)},[r]);return o?t?e.createElement(k,null,e.createElement(O,{options:m,definition:o.definition,withState:{environments:p,environment:i,operation:{pointer:t.pointer,state:{requestValues:{body:T,...C}}}}},n?q==="request"?e.createElement(E,{operation:t,disableFooter:!0}):e.createElement(S,{operation:t,activeResponseTab:r,onTabChange:y}):e.createElement("div",null,e.createElement(E,{operation:t,disableFooter:!0}),e.createElement(S,{operation:t,activeResponseTab:r,onTabChange:y})))):e.createElement("pre",{style:{color:"red"}},"Cannot find OpeAPI operation by operationId or pointer"):e.createElement(M,null,R)}export{W as OpenApiCodeSample};
