import{in as e,t}from"./flowdrop.es.js?v=2.0.0-beta.7";function r(e){switch(e){case"string":return"string";case"number":return"number";case"integer":return"integer";case"boolean":return"boolean";case"array":return"array";case"object":return"object";case"float":return"float";default:return"mixed"}}function i(e,t,o,a){const s={name:e,label:t.title??e,description:t.description,type:r(t.type),sourcePort:o,sourceNode:a};if("object"===t.type&&t.properties){s.properties={};for(const[e,r]of Object.entries(t.properties))s.properties[e]=i(e,r,o,a)}return"array"===t.type&&t.items&&(s.items=i("item",t.items,o,a)),s}function o(e,t){if(e.schema&&e.schema.properties){const r={name:e.id,label:e.name,description:e.description,type:"object",sourcePort:e.id,sourceNode:t,properties:{}};for(const[o,a]of Object.entries(e.schema.properties))r.properties[o]=i(o,a,e.id,t);return r}return{name:e.id,label:e.name,description:e.description,type:r(e.dataType),sourcePort:e.id,sourceNode:t}}function a(e){if(!e)return;const t=e.match(/-output-(.+)$/);if(t)return t[1];const r=e.match(/-input-(.+)$/);return r?r[1]:e}function s(e,t,r,s){const n={},{targetPortIds:c,includePortName:p}=s??{},u=function(e,t,r){const i=[],o=r.filter(t=>t.target===e.id);for(const s of o){const r=t.find(e=>e.id===s.source);if(!r)continue;const o=a(s.sourceHandle),n=a(s.targetHandle),c=r.data.metadata.outputs.find(e=>e.id===o),p=e.data.metadata.inputs.find(e=>e.id===n);i.push({edge:s,sourceNode:r,sourcePort:c,targetPort:p})}return i}(e,t,r);for(const a of u){const{sourceNode:e,sourcePort:t,targetPort:r}=a;if("trigger"===t?.dataType)continue;if("trigger"===r?.dataType)continue;const s=r?.id??t?.id??"data";if((void 0===c||c.includes(s))&&t)if(t.schema?.properties&&!p)for(const[o,a]of Object.entries(t.schema.properties))n[o]||(n[o]=i(o,a,t.id,e.id));else{const i=s;if(n[i])continue;const a=o(t,e.id);a.name=i,a.label=r?.name??t.name,n[i]=a}}return{variables:n}}function n(e,t){const r=t.split(".");let i;for(let o=0;o<r.length;o++){const t=r[o],a=t.match(/^(\w+)\[(\d+|\*)\]$/);if(a){const[,t]=a;0===o?i=e.variables[t]:i?.properties&&(i=i.properties[t]),i?.items&&(i=i.items);continue}if(0===o)i=e.variables[t];else{if(!i?.properties)return[];i=i.properties[t]}}return i?.properties?Object.values(i.properties):[]}function c(e=2){const t=[];for(let r=0;r<=e;r++)t.push(`${r}]`);return t.push("*]"),t}function p(e,t){const r=t.split(".");let i;for(let o=0;o<r.length;o++){const t=r[o],a=t.match(/^(\w+)\[(\d+|\*)\]$/);if(a){const[,t]=a;0===o?i=e.variables[t]:i?.properties&&(i=i.properties[t]),i?.items&&(i=i.items);continue}if(0===o)i=e.variables[t];else{if(!i?.properties)return!1;i=i.properties[t]}}return"array"===i?.type}function u(e,t){const r={...t.variables};for(const[i,o]of Object.entries(e.variables))r[i]=o;return{variables:r}}async function f(r,i,o,a,n,c,p){let f={variables:{}};if(n.api)try{const{fetchVariableSchema:l}=await t(async()=>{const{fetchVariableSchema:e}=await import("./flowdrop-api-variable-service.js?v=2.0.0-beta.7");return{fetchVariableSchema:e}},[]),d=await l(r,c,i.id,n.api,p);if(d.success&&d.schema){if(f=d.schema,!1!==n.api.mergeWithSchema&&n.schema&&(f=u(f,n.schema)),n.api.mergeWithPorts){f=u(f,s(i,o,a,{targetPortIds:n.ports,includePortName:n.includePortName}))}return f}if(!n.api.fallbackOnError)return e.error("Failed to fetch variables from API:",d.error),{variables:{}}}catch(l){if(e.error("Error fetching variables from API:",l),!1===n.api.fallbackOnError)return{variables:{}}}return void 0===n.ports&&n.api||(f=s(i,o,a,{targetPortIds:n.ports,includePortName:n.includePortName})),n.schema&&(f=u(n.schema,f)),f}export{p as a,f as i,s as n,n as r,c as t};
//# sourceMappingURL=flowdrop-variable-service.js.map