"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.useCodeSamples=useCodeSamples;const jotai_1=require("jotai"),services_1=require("../../services"),store_1=require("../../jotai/store"),operation_1=require("../../jotai/operation"),app_1=require("../../jotai/app"),hooks_1=require("../../hooks");function useCodeSamples({payload:e,isWebhook:a,definitionSamples:t,pointer:o,envVariables:s}){var n,r;const{parser:l,options:{codeSamples:i,generatedSamplesMaxDepth:p}}=(0,jotai_1.useAtomValue)(store_1.globalStoreAtom),u=(0,jotai_1.useAtomValue)(app_1.activeMimeNameAtom),g=(0,jotai_1.useAtomValue)((0,operation_1.operationStore)(o)),{languages:c}=(0,jotai_1.useAtomValue)(app_1.languageAtom),[m]=(0,jotai_1.useAtomValue)(app_1.environmentAtom),d=(0,hooks_1.useTranslate)(),v=new Map(null==c?void 0:c.map((({key:e,order:a})=>[e,a]))),_=applyEnvironments(t.filter((e=>v.has((0,app_1.getLangKey)(e)))),s,m),b=new Set(_.map(app_1.getLangKey));let j=[];if(!a){j=createCodeSamples((null===(n=i.languages)||void 0===n?void 0:n.filter((({lang:e,label:a})=>"Payload"!==e&&!b.has((0,app_1.getLangKey)({lang:e,label:a})))))||[],i,l.definition,p,g,m,d,u)}const O=[..._,...j],h=null===(r=null==c?void 0:c.find((({lang:a})=>a===(null==e?void 0:e.lang))))||void 0===r?void 0:r.title,y=e&&(0,app_1.getLangKey)({label:h,lang:e.lang});e&&y&&v.has(y)&&O.push(Object.assign(Object.assign({},e),{label:h,order:v.get(y)}));const f=O.map((e=>{var a;const t=null!==(a=e.label)&&void 0!==a?a:e.lang;return Object.assign({key:(0,app_1.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:f}}function createCodeSamples(e,{skipOptionalParameters:a,withOAuth2Call:t},o,s,n,r,l,i){return e.map((({lang:e,label:p,options:u})=>({lang:e,label:p,get:p=>(0,services_1.getCodeSample)(Object.assign(Object.assign({},p),{lang:e,options:Object.assign(Object.assign({},u),{skipOptionalParameters:a,withOAuth2Call:t,spec:o,generatedPayloadSamplesMaxDepth:s,store:n,activeMimeName:i}),environment:r,translate:l}))})))}function applyEnvironments(e,a,t){var o;if(!a||!e.length)return e;const s=null==t?void 0:t.server;let n=Object.assign({},a.values||{});return s&&(null===(o=a.serverValues)||void 0===o?void 0:o[s])&&(n=Object.assign(Object.assign({},n),a.serverValues[s])),0===Object.keys(n).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!==n[a]?n[a]:e))),a}))}
//# sourceMappingURL=useCodeSamples.js.map