import{jsx as o,Fragment as M,jsxs as z}from"react/jsx-runtime";import{lazy as H,memo as J,Suspense as K,useCallback as m,useRef as W,useState as w}from"react";import{useAtom as u,useAtomValue as p}from"jotai";import Q from"deepmerge";const X=H(async()=>import("@redocly/replay").then(e=>({default:e.ReplayOverlay})));import{Button as Y}from"@redocly/theme/components/Button/Button";import{useFocusTrap as Z,useModalScrollLock as $}from"@redocly/theme/core/openapi";import{RocketIcon as ee}from"@redocly/theme/icons/RocketIcon/RocketIcon";import{CircleDashIcon as te}from"@redocly/theme/icons/CircleDashIcon/CircleDashIcon";import{Portal as oe}from"@redocly/theme/components/Portal/Portal";import{useConfigureReplay as re}from"@redocly/theme/ext/use-configure-replay";import{globalOptionsAtom as ne,globalStoreAtom as ae}from"../../jotai/store.js";import{allOperationsAtom as se,replayStateAtom as ie}from"../../jotai/replay.js";import{allOperations as le,getDefaultOperationStore as me}from"../../jotai/operation.js";import{LOADING_STATE as n}from"../../constants.js";import{environmentAtom as pe,userClaimsAtom as ce}from"../../jotai/app.js";import{convertOperationToReplayValueForRedoc as fe}from"./utils.js";import{useTranslate as ue,useTelemetry as de}from"../../hooks/index.js";import{JsonPointer as ye}from"../../utils/index.js";import{getDefaultCollectorUrl as he}from"../../utils/telemetry.js";import{createTryItOpenEvent as Oe,createTryItSentEvent as Ie}from"../../events/index.js";import{joinWithSeparator as Re}from"../../services/history/helpers.js";import{HTTPSnippet as ge}from"../../services/code-samples/httpsnippet/index.js";function ve({operation:e,corsProxyUrl:L,fullOpenApi:d}){const y=ue(),P=de(),h=W(null),{mockServer:q,oAuth2RedirectURI:E,routingBasePath:c}=p(ne),O=p(se),{parser:I,options:{events:a}}=p(ae),R=p(ce),[s,N]=w(!1),[l,g]=u(ie),[v,S]=u(le),[[,V],A]=u(pe),{config:j,refresh:T}=re({userClaims:R,info:I?.definition.info,operation:{name:e.name,path:e.path,operationId:e.operationId||"",href:e.href,method:e.httpVerb},servers:e.servers},s);Z(h),$(s);const x=m(()=>T(),[T]),k=m(({operations:t,environment:i,environments:f})=>{const _=t.reduce((C,r)=>{r.requestValues.body=r.requestValues.body instanceof URLSearchParams?Object.fromEntries(r.requestValues.body.entries()):r.requestValues.body;const{path:F,method:B}=r.apiPath,b=ye.compile(["paths",F,B]);return C[b]=Q(me(b),r),C},{});S({...v,..._}),A({environment:i,environments:f})},[v,S,A]),D=()=>{if(l!==n.LOADING){if(!s&&(P.sendTryItOpenedMessage(),a?.tryItOpen&&e)){const t=Oe({operation:e});a.tryItOpen(t)}N(!s)}l===n.NOT_LOADED&&g(n.LOADING)},G=m(t=>{if(a?.tryItSent&&e){const i=Ie({operation:e,isSuccess:t});a.tryItSent(i)}},[a,e]),U=m(t=>{if(!t)return;const i=O.find(f=>f.operationId===t);return Re(c,i?.href)},[O,c]);return d?z(M,{children:[o(Y,{onClick:D,variant:"primary",iconPosition:"left",icon:l===n.LOADING?o(te,{}):o(ee,{}),children:l===n.LOADING?y("openapi.loading","Loading..."):y("openapi.tryIt","Try it")}),s&&o(K,{children:o(oe,{mountId:"api-content",children:o("div",{ref:h,children:o(X,{activeOperationId:e.operationId||e.pointer,api:d,onRequestChange:k,onClose:D,onLoad:()=>g(n.LOADED),settings:{corsProxyUrl:L,mockServer:q,selectedEnvironment:V,apiId:I?.definition.info?.["x-metadata"]?.apiId,oAuth2RedirectURI:E,disableCollectionsTab:!0,storageKey:c,telemetry:{tracerName:"try-it",collectorUrl:he()},HTTPSnippet:ge,convertOperationToReplayValue:fe(R,j),getOperationUrl:U,onRequestReset:x},onRequestSent:G})})})})]}):null}const He=J(ve);export{He as Replay};
