import{useAtomValue as u}from"jotai";import{getCodeSample as x}from"../../services/index.js";import{globalStoreAtom as D}from"../../jotai/store.js";import{operationStore as K}from"../../jotai/operation.js";import{environmentAtom as k,getLangKey as f,languageAtom as E,activeMimeNameAtom as j}from"../../jotai/app.js";import{useTranslate as N}from"../../hooks/index.js";function G({payload:t,isWebhook:s,definitionSamples:l,pointer:a,envVariables:r}){const{parser:m,options:{codeSamples:o,generatedSamplesMaxDepth:c}}=u(D),i=u(j),g=u(K(a)),{languages:d}=u(E),[S]=u(k),v=N(),p=new Map(d?.map(({key:e,order:n})=>[e,n])),M=l.filter(e=>p.has(f(e))),y=P(M,r,S),b=new Set(y.map(f));let C=[];if(!s){const e=o.languages?.filter(({lang:n,label:w})=>n!=="Payload"&&!b.has(f({lang:n,label:w})))||[];C=O(e,o,m.definition,c,g,S,v,i)}const L=[...y,...C],A=d?.find(({lang:e})=>e===t?.lang)?.title,h=t&&f({label:A,lang:t.lang});return t&&h&&p.has(h)&&L.push({...t,label:A,order:p.get(h)}),{samples:L.map(e=>{const n=e.label??e.lang;return{key:f(e),title:n,order:typeof p.get(n.toLowerCase())=="number"?p.get(n.toLowerCase()):9999,...e}}).sort((e,n)=>e.order-n.order)}}function O(t,{skipOptionalParameters:s,withOAuth2Call:l},a,r,m,o,c,i){return t.map(({lang:g,label:d,options:S})=>({lang:g,label:d,get:v=>x({...v,lang:g,options:{...S,skipOptionalParameters:s,withOAuth2Call:l,spec:a,generatedPayloadSamplesMaxDepth:r,store:m,activeMimeName:i},environment:o,translate:c})}))}function P(t,s,l){if(!s||!t.length)return t;const a=l?.server;let r={...s.values||{}};return a&&s.serverValues?.[a]&&(r={...r,...s.serverValues[a]}),Object.keys(r).length===0?t:t.map(m=>{const o={...m};return"source"in o&&typeof o.source=="string"&&(o.source=o.source.replace(/\{\{([^}]+)\}\}/g,(c,i)=>r[i]!==void 0?r[i]:c)),o})}export{G as useCodeSamples};
