import{jsx as o,jsxs as D}from"react/jsx-runtime";import{memo as T,useCallback as L,useMemo as k}from"react";import{useAtomValue as y,useAtom as _}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 V}from"../../events/index.js";import{globalStoreAtom as $}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 A}from"../Replay/index.js";import{CodeBlockPanel as w}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 m=X(),[{activeLanguage:q,languages:c},R]=_(K),{samples:n}=F(a),{parser:d,options:{events:l,corsProxyUrl:g,hideReplay:j}}=y($),i=!P&&!a.isWebhook&&!j&&!a.hideReplay,t=!!c?.length,u=Q(n,q),E=e=>{R(e),m.sendSelectLanguageClickedMessage([{object:"select_language",language:e}]);const r=c?.find(s=>s.key===e);if(l?.codeSamplesLanguageSwitch&&r){const s=V({operation:a,sample:{lang:r.lang,label:r.title}});l.codeSamplesLanguageSwitch(s)}},h=y(J),{activeExampleName:C}=y(Y(a.pointer)),f=L(({lang:e,title:r})=>()=>{const s=O({operation:a,label:r,lang:e,type:"request",activeMimeName:h,activeExampleName:C});m.sendCopyCodeSnippetClickedMessage([{object:"code_snippet",snippetType:"request",language:e}]),l?.codeSamplesCopy?.(s)},[a,m,l,h,C]),b=L(e=>{v?.(e,"request-samples")},[v]),x=()=>D(I,{isExpandable:!1,withReplay:!t&&i,children:[o(ee,{operation:a}),t&&o(G,{activeTab:u,samples:n,onChange:E}),!t&&i&&o(A,{operation:a,corsProxyUrl:g,fullOpenApi:d.definition})]}),M=k(()=>{if(i&&t)return o(ae,{children:o(A,{operation:a,corsProxyUrl:g,fullOpenApi:d.definition})})},[i,t,a,g,d.definition]),N=k(()=>n.map(e=>e.key===u?o("div",{children:H(e)?o(W,{content:e.requestBodyContent,onCopyClick:f(e),onPanelToggle:b,...p}):o(B,{lang:e.lang,source:e.source,externalSample:e,operation:a,onCopyClick:f(e),...p})},e.key+a.id):null),[u,f,b,a,p,n]);return c&&!n.length?o(w,{className:"panel-request-samples",header:x,isExpandable:!1,expanded:!1}):n.length?o(w,{className:"panel-request-samples",header:x,footer:M,isExpandable:!1,children:N}):null}const Ce=T(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{Ce as RequestSamples};
