import{getEndpointConfig as e}from"./flowdrop-api.js?v=1.0.0";import{b7 as t}from"./flowdrop.es.js?v=1.0.0";import{D as o}from"./flowdrop-constants.js?v=1.0.0";const a=new Map,r=o;function s(e,t){let o=e;return t.workflowId&&(o=o.replace(/\{workflowId\}/g,encodeURIComponent(t.workflowId))),o=o.replace(/\{nodeId\}/g,encodeURIComponent(t.nodeId)),o}function n(e,t){return`variables:${e||"unknown"}:${t}`}function c(e,t){const o={};for(const[a,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)),o[a]=e}else"object"!=typeof r||null===r||Array.isArray(r)?o[a]=r:o[a]=c(r,t);return o}async function i(o,u,d,f){const l=d.endpoint,h={workflowId:o,nodeId:u},m=n(o,u);if(!1!==l.cacheEnabled){const e=a.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(g){t.warn("Failed to get auth headers:",g)}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 v={method:b,headers:y,signal:AbortSignal.timeout(w)};if("GET"!==b&&l.body){const e=c(l.body,h);v.body=JSON.stringify(e)}try{const e=await fetch(p,v);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(o,u,d,f)}return{success:!1,error:"Authentication failed"}}const t=await e.text();return{success:!1,error:`HTTP ${e.status}: ${t||e.statusText}`}}const t=await e.json();let r;return t.variables&&"object"==typeof t.variables?r=t:t.data?.variables&&"object"==typeof t.data.variables?r=t.data:t.schema?.variables&&"object"==typeof t.schema.variables?r=t.schema:t.success&&t.data?.variables&&(r=t.data),r?(!1!==l.cacheEnabled&&a.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(g){return g instanceof Error?"AbortError"===g.name||"TimeoutError"===g.name?{success:!1,error:`Request timed out after ${w}ms`}:{success:!1,error:g.message}:{success:!1,error:"Unknown error occurred while fetching variable schema"}}}function u(e){if(e)for(const t of a.keys())t.includes(e)&&a.delete(t);else a.clear()}function d(e,t){const o=n(e,t);a.delete(o)}function f(){return a.size}function l(){return Array.from(a.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
