import{jsx as o,jsxs as N}from"react/jsx-runtime";import{memo as D,useCallback as w,useMemo as L}from"react";import{useAtomValue as y,useAtom as T}from"jotai";import{PanelHeader as U}from"@redocly/theme/components/Panel/PanelHeader";import{PayloadSamples as W}from"../PayloadSamples/index.js";import{CodeSample as B}from"./CodeSample.js";import{useCodeSamples as F}from"./useCodeSamples.js";import{isPayloadSample as H}from"../../models/index.js";import{createCodeSampleCopyEvent as O,createLanguageSwitchEvent as $}from"../../events/index.js";import{globalStoreAtom as V}from"../../jotai/store.js";import{ServerListDropdown as z}from"../ServerListDropdown/index.js";import{LanguageDropdown as G}from"../Language/index.js";import{activeMimeNameAtom as J,languageAtom as K}from"../../jotai/app.js";import{Replay as k}from"../Replay/index.js";import{CodeBlockPanel as A}from"../common/index.js";import{useActiveWithFallback as Q,useTelemetry as X}from"../../hooks/index.js";import{operationStore as Y}from"../../jotai/operation.js";import{styled as S}from"../../styled-components.js";function Z({operation:a,disableFooter:P,onPanelToggle:v,...p}){const c=X(),[{activeLanguage:q,languages:m},R]=T(K),{samples:n}=F(a),{parser:d,options:{events:s,corsProxyUrl:g,hideReplay:j}}=y(V),i=!P&&!a.isWebhook&&!j&&!a.hideReplay,t=!!m?.length,u=Q(n,q),E=e=>{R(e),c.sendSelectLanguageClickedMessage([{id:`button_select_language_${e}`,object:"button",uri:window.location.href,language:e}]);const r=m?.find(l=>l.key===e);if(s?.codeSamplesLanguageSwitch&&r){const l=$({operation:a,sample:{lang:r.lang,label:r.title}});s.codeSamplesLanguageSwitch(l)}},h=y(J),{activeExampleName:b}=y(Y(a.pointer)),f=w(({lang:e,title:r})=>()=>{const l=O({operation:a,label:r,lang:e,type:"request",activeMimeName:h,activeExampleName:b});c.sendCopyCodeSnippetClickedMessage([{id:"copy-code-snippet",object:"code_snippet",uri:window.location.href,snippetType:"request",language:e}]),s?.codeSamplesCopy?.(l)},[a,c,s,h,b]),C=w(e=>{v?.(e,"request-samples")},[v]),x=()=>N(I,{isExpandable:!1,withReplay:!t&&i,children:[o(ee,{operation:a}),t&&o(G,{activeTab:u,samples:n,onChange:E}),!t&&i&&o(k,{operation:a,corsProxyUrl:g,fullOpenApi:d.definition})]}),_=L(()=>{if(i&&t)return o(ae,{children:o(k,{operation:a,corsProxyUrl:g,fullOpenApi:d.definition})})},[i,t,a,g,d.definition]),M=L(()=>n.map(e=>e.key===u?o("div",{children:H(e)?o(W,{content:e.requestBodyContent,onCopyClick:f(e),onPanelToggle:C,...p}):o(B,{lang:e.lang,source:e.source,externalSample:e,operation:a,onCopyClick:f(e),...p})},e.key+a.id):null),[u,f,C,a,p,n]);return m&&!n.length?o(A,{className:"panel-request-samples",header:x,isExpandable:!1,expanded:!1}):n.length?o(A,{className:"panel-request-samples",header:x,footer:_,isExpandable:!1,children:M}):null}const be=D(Z),I=S(U)`
  flex-wrap: nowrap;
  ${({withReplay:a})=>a&&"padding: var(--spacing-xs) var(--spacing-sm);"}
`,ee=S(z)`
  padding-right: var(--spacing-base);
  min-width: 0;
`,ae=S.div`
  display: flex;
  justify-content: flex-end;
  border-top: var(--panel-border);
  padding: var(--spacing-base) var(--spacing-md) var(--spacing-md);
`;export{be as RequestSamples};
