var __rest=this&&this.__rest||function(e,o){var a={};for(var t in e)Object.prototype.hasOwnProperty.call(e,t)&&o.indexOf(t)<0&&(a[t]=e[t]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(t=Object.getOwnPropertySymbols(e);r<t.length;r++)o.indexOf(t[r])<0&&Object.prototype.propertyIsEnumerable.call(e,t[r])&&(a[t[r]]=e[t[r]])}return a};import{jsx as _jsx,jsxs as _jsxs}from"react/jsx-runtime";import{memo,useCallback,useMemo}from"react";import{useAtomValue,useAtom}from"jotai";import{PanelHeader}from"@redocly/theme";import styled from"styled-components";import{PayloadSamples}from"../PayloadSamples";import{CodeSample}from"./CodeSample";import{useCodeSamples}from"./useCodeSamples";import{isPayloadSample}from"../../models";import{createCodeSampleCopyEvent}from"../../events";import{globalStoreAtom}from"../../jotai/store";import{ServerListDropdown}from"../ServerListDropdown";import{LanguageDropdown}from"../Language";import{activeMimeNameAtom,languageAtom}from"../../jotai/app";import{Replay}from"../Replay";import{CodeBlockPanel}from"../common";import{useActiveWithFallback,useOtelTelemetry}from"../../hooks";import{operationStore}from"../../jotai/operation";function RequestSamplesComponent(e){var{operation:o,disableFooter:a}=e,t=__rest(e,["operation","disableFooter"]);const r=useOtelTelemetry(),[{activeLanguage:l,languages:p},s]=useAtom(languageAtom),{samples:n}=useCodeSamples(o),{parser:i,options:{events:m,corsProxyUrl:d,hideReplay:c}}=useAtomValue(globalStoreAtom),u=!(a||o.isWebhook||c||o.hideReplay),y=!!(null==p?void 0:p.length),g=useActiveWithFallback(n,l),f=e=>{s(e),r.send({type:"openapi_docs.select_language.clicked",payload:{language:e}})},v=useAtomValue(activeMimeNameAtom),{activeExampleName:x}=useAtomValue(operationStore(o.pointer)),S=useCallback((({lang:e,title:a})=>()=>{var t;const l=createCodeSampleCopyEvent({operation:o,label:a,lang:e,type:"request",activeMimeName:v,activeExampleName:x});r.send({type:"openapi_docs.copy_code_snippet.clicked",payload:{snippet_type:"request",language:e}}),null===(t=null==m?void 0:m.codeSamplesCopy)||void 0===t||t.call(m,l)}),[o,r,m,v,x]),b=()=>_jsxs(StyledPanelHeader,{isExpandable:!1,withReplay:!y&&u,children:[_jsx(StyledServerListDropdown,{operation:o}),y&&_jsx(LanguageDropdown,{activeTab:g,samples:n,onChange:f}),!y&&u&&_jsx(Replay,{operation:o,corsProxyUrl:d,fullOpenApi:i.definition})]}),j=useMemo((()=>{if(u&&y)return _jsx(ReplayWrapper,{children:_jsx(Replay,{operation:o,corsProxyUrl:d,fullOpenApi:i.definition})})}),[u,y,o,d,i.definition]),h=useMemo((()=>n.map((e=>e.key===g?_jsx("div",{children:isPayloadSample(e)?_jsx(PayloadSamples,Object.assign({content:e.requestBodyContent,onCopyClick:S(e)},t)):_jsx(CodeSample,Object.assign({lang:e.lang,source:e.source,externalSample:e,operation:o,onCopyClick:S(e)},t))},e.key+o.id):null))),[g,S,o,t,n]);return p&&!n.length?_jsx(CodeBlockPanel,{className:"panel-request-samples",header:b,isExpandable:!1,expanded:!1}):n.length?_jsx(CodeBlockPanel,{className:"panel-request-samples",header:b,footer:j,isExpandable:!1,children:h}):null}export const RequestSamples=memo(RequestSamplesComponent);const StyledPanelHeader=styled(PanelHeader)`
  flex-wrap: nowrap;
  ${({withReplay:e})=>e&&"padding: var(--spacing-xs) var(--spacing-sm);"}
`,StyledServerListDropdown=styled(ServerListDropdown)`
  padding-right: var(--spacing-base);
  min-width: 0;
`,ReplayWrapper=styled.div`
  display: flex;
  justify-content: flex-end;
  border-top: var(--panel-border);
  padding: var(--spacing-base) var(--spacing-md) var(--spacing-md);
`;
//# sourceMappingURL=RequestSamples.js.map