import{W as e}from"./flowdrop.es.js?v=2.0.0-beta.7";import{t}from"./flowdrop-fetch-with-auth.js?v=2.0.0-beta.7";var r=new Map,o=e;function a(e,t){const r={};for(const[o,s]of Object.entries(e))if("string"==typeof s){let e=s;t.workflowId&&(e=e.replace(/\{workflowId\}/g,encodeURIComponent(t.workflowId))),e=e.replace(/\{nodeId\}/g,encodeURIComponent(t.nodeId)),r[o]=e}else"object"!=typeof s||null===s||Array.isArray(s)?r[o]=s:r[o]=a(s,t);return r}async function s(e,s,c,n,i){const d=n.endpoint,l={workflowId:s,nodeId:c},u=function(e,t){return`variables:${e||"unknown"}:${t}`}(s,c);if(!1!==d.cacheEnabled){const e=r.get(u),t=n.cacheTtl??o;if(e&&function(e,t=o){return Date.now()-e.cachedAt<t}(e,t))return{success:!0,schema:e.schema,fromCache:!0}}let f=function(e,t){let r=e;return t.workflowId&&(r=r.replace(/\{workflowId\}/g,encodeURIComponent(t.workflowId))),r=r.replace(/\{nodeId\}/g,encodeURIComponent(t.nodeId)),r}(d.url,l);f.startsWith("/")&&e?.baseUrl&&(f=`${e.baseUrl.replace(/\/$/,"")}${f}`);const m=d.method??"GET",h=d.timeout??3e4,b={method:m,signal:AbortSignal.timeout(h)};if("GET"!==m&&d.body){const e=a(d.body,l);b.body=JSON.stringify(e)}try{const e=await t(f,b,{authProvider:i,baseHeaders:{Accept:"application/json","Content-Type":"application/json",...d.headers}});if(404===e.status)return{success:!0,schema:{variables:{}},fromCache:!1};if(!e.ok){if(401===e.status||403===e.status)return{success:!1,error:"Authentication failed"};const t=await e.text();return{success:!1,error:`HTTP ${e.status}: ${t||e.statusText}`}}const o=await e.json();let a;return o.variables&&"object"==typeof o.variables?a=o:o.data?.variables&&"object"==typeof o.data.variables?a=o.data:o.schema?.variables&&"object"==typeof o.schema.variables?a=o.schema:o.success&&o.data?.variables&&(a=o.data),a?(!1!==d.cacheEnabled&&r.set(u,{schema:a,cachedAt:Date.now(),cacheKey:u}),{success:!0,schema:a,fromCache:!1}):{success:!1,error:"Invalid variable schema format received from endpoint"}}catch(p){return p instanceof Error?"AbortError"===p.name||"TimeoutError"===p.name?{success:!1,error:`Request timed out after ${h}ms`}:{success:!1,error:p.message}:{success:!1,error:"Unknown error occurred while fetching variable schema"}}}export{s as fetchVariableSchema};
//# sourceMappingURL=flowdrop-api-variable-service.js.map