var __awaiter=this&&this.__awaiter||function(e,o,t,r){return new(t||(t=Promise))((function(n,a){function i(e){try{l(r.next(e))}catch(e){a(e)}}function s(e){try{l(r.throw(e))}catch(e){a(e)}}function l(e){var o;e.done?n(e.value):(o=e.value,o instanceof t?o:new t((function(e){e(o)}))).then(i,s)}l((r=r.apply(e,o||[])).next())}))};import{jsx as _jsx,Fragment as _Fragment,jsxs as _jsxs}from"react/jsx-runtime";import{lazy,memo,Suspense,useCallback,useRef,useState}from"react";import{Button,CircleDashIcon,Portal,RocketIcon,useFocusTrap,useModalScrollLock}from"@redocly/theme";import{useAtom,useAtomValue}from"jotai";import merge from"deepmerge";const ReplayOverlay=lazy((()=>__awaiter(void 0,void 0,void 0,(function*(){return import("@redocly/replay").then((e=>({default:e.ReplayOverlay})))}))));import{globalOptionsAtom,globalStoreAtom}from"../../jotai/store";import{allOperationsAtom,replayStateAtom}from"../../jotai/replay";import{allOperations,getDefaultOperationStore}from"../../jotai/operation";import{LOADING_STATE}from"../../constants";import{environmentAtom,userClaimsAtom}from"../../jotai/app";import{convertOperationToReplayValueForRedoc}from"./utils";import{useTranslate,useOtelTelemetry}from"../../hooks";import{JsonPointer}from"../../utils";function ReplayComponent({operation:e,corsProxyUrl:o,fullOpenApi:t}){var r,n;const a=useTranslate(),i=useOtelTelemetry(),s=useRef(null),{mockServer:l,oAuth2RedirectURI:u,routingBasePath:m}=useAtomValue(globalOptionsAtom),p=useAtomValue(allOperationsAtom),{parser:c}=useAtomValue(globalStoreAtom),d=useAtomValue(userClaimsAtom),[A,f]=useState(!1),[y,O]=useAtom(replayStateAtom),[v,_]=useAtom(allOperations),[[,T],h]=useAtom(environmentAtom);useFocusTrap(s),useModalScrollLock(A);const I=useCallback((({operations:e,environment:o,environments:t})=>{const r=e.reduce(((e,o)=>{o.requestValues.body=o.requestValues.body instanceof URLSearchParams?Object.fromEntries(o.requestValues.body.entries()):o.requestValues.body;const{path:t,method:r}=o.sourcePath,n=JsonPointer.compile(["paths",t,r]);return e[n]=merge(getDefaultOperationStore(n),o),e}),{});_(Object.assign(Object.assign({},v),r)),h({environment:o,environments:t})}),[v,_,h]),S=()=>{y!==LOADING_STATE.LOADING&&(A||i.send({type:"openapi_docs.try_it.opened"}),f(!A)),y===LOADING_STATE.NOT_LOADED&&O(LOADING_STATE.LOADING)},g=useCallback((e=>{if(!e)return;const o=p.find((o=>o.operationId===e));return m+(null==o?void 0:o.href)}),[p,m]);return t?_jsxs(_Fragment,{children:[_jsx(Button,{onClick:S,variant:"primary",iconPosition:"left",icon:y===LOADING_STATE.LOADING?_jsx(CircleDashIcon,{}):_jsx(RocketIcon,{}),children:y===LOADING_STATE.LOADING?a("openapi.loading","Loading..."):a("openapi.tryIt","Try it")}),A&&_jsx(Suspense,{children:_jsx(Portal,{mountId:"api-content",children:_jsx("div",{ref:s,children:_jsx(ReplayOverlay,{activeOperationId:e.operationId||e.pointer,source:{data:t,type:"openapi"},onRequestChange:I,onClose:S,onLoad:()=>O(LOADING_STATE.LOADED),settings:{corsProxyUrl:o,mockServer:l,environment:T,apiId:null===(n=null===(r=null==c?void 0:c.definition.info)||void 0===r?void 0:r["x-metadata"])||void 0===n?void 0:n.apiId,oAuth2RedirectURI:u,disableCollectionsTab:!0,disableWorkflowTab:!0,storageKey:m,convertOperationToReplayValue:(e,o,t)=>convertOperationToReplayValueForRedoc(d)(e,o,t),getOperationUrl:g}})})})})]}):null}export const Replay=memo(ReplayComponent);
//# sourceMappingURL=Replay.js.map