import{getEndpointConfig as e}from"./flowdrop-api.js?v=1.8.1";import{bK as t,b8 as a}from"./flowdrop.es.js?v=1.8.1";const o=new Map,r=t;function s(e,t){let a=e;return t.workflowId&&(a=a.replace(/\{workflowId\}/g,encodeURIComponent(t.workflowId))),a=a.replace(/\{nodeId\}/g,encodeURIComponent(t.nodeId)),a}function n(e,t){return`variables:${e||"unknown"}:${t}`}function c(e,t){const a={};for(const[o,r]of Object.entries(e))if("string"==typeof r){let e=r;t.workflowId&&(e=e.replace(/\{workflowId\}/g,encodeURIComponent(t.workflowId))),e=e.replace(/\{nodeId\}/g,encodeURIComponent(t.nodeId)),a[o]=e}else"object"!=typeof r||null===r||Array.isArray(r)?a[o]=r:a[o]=c(r,t);return a}async function i(t,u,d,f){const l=d.endpoint,h={workflowId:t,nodeId:u},m=n(t,u);if(!1!==l.cacheEnabled){const e=o.get(m),t=d.cacheTtl??r;if(e&&function(e,t=r){return Date.now()-e.cachedAt<t}(e,t))return{success:!0,schema:e.schema,fromCache:!0}}let p=s(l.url,h);if(p.startsWith("/")){const t=e();if(t?.baseUrl){p=`${t.baseUrl.replace(/\/$/,"")}${p}`}}const b=l.method??"GET",w=l.timeout??3e4,y={Accept:"application/json","Content-Type":"application/json",...l.headers};if(f)try{const e=await f.getAuthHeaders();Object.assign(y,e)}catch(k){a.warn("Failed to get auth headers:",k)}const I=e();I?.auth&&("bearer"===I.auth.type&&I.auth.token?y.Authorization=y.Authorization??`Bearer ${I.auth.token}`:"api_key"===I.auth.type&&I.auth.apiKey?y["X-API-Key"]=y["X-API-Key"]??I.auth.apiKey:"custom"===I.auth.type&&I.auth.headers&&Object.assign(y,I.auth.headers));const g={method:b,headers:y,signal:AbortSignal.timeout(w)};if("GET"!==b&&l.body){const e=c(l.body,h);g.body=JSON.stringify(e)}try{const e=await fetch(p,g);if(404===e.status)return{success:!0,schema:{variables:{}},fromCache:!1};if(!e.ok){if(401===e.status||403===e.status){if(f?.onUnauthorized){if(await f.onUnauthorized())return i(t,u,d,f)}return{success:!1,error:"Authentication failed"}}const a=await e.text();return{success:!1,error:`HTTP ${e.status}: ${a||e.statusText}`}}const a=await e.json();let r;return a.variables&&"object"==typeof a.variables?r=a:a.data?.variables&&"object"==typeof a.data.variables?r=a.data:a.schema?.variables&&"object"==typeof a.schema.variables?r=a.schema:a.success&&a.data?.variables&&(r=a.data),r?(!1!==l.cacheEnabled&&o.set(m,{schema:r,cachedAt:Date.now(),cacheKey:m}),{success:!0,schema:r,fromCache:!1}):{success:!1,error:"Invalid variable schema format received from endpoint"}}catch(k){return k instanceof Error?"AbortError"===k.name||"TimeoutError"===k.name?{success:!1,error:`Request timed out after ${w}ms`}:{success:!1,error:k.message}:{success:!1,error:"Unknown error occurred while fetching variable schema"}}}function u(e){if(e)for(const t of o.keys())t.includes(e)&&o.delete(t);else o.clear()}function d(e,t){const a=n(e,t);o.delete(a)}function f(){return o.size}function l(){return Array.from(o.keys())}export{r as DEFAULT_VARIABLE_CACHE_TTL,u as clearVariableCache,i as fetchVariableSchema,l as getVariableCacheKeys,f as getVariableCacheSize,d as invalidateVariableCache,s as resolveEndpointUrl};
//# sourceMappingURL=flowdrop-api-variable-service.js.map
