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