var T=Object.defineProperty,A=Object.defineProperties;var z=Object.getOwnPropertyDescriptors;var g=Object.getOwnPropertySymbols;var _=Object.prototype.hasOwnProperty,k=Object.prototype.propertyIsEnumerable;var f=(r,e,t)=>e in r?T(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,m=(r,e)=>{for(var t in e||(e={}))_.call(e,t)&&f(r,t,e[t]);if(g)for(var t of g(e))k.call(e,t)&&f(r,t,e[t]);return r},y=(r,e)=>A(r,z(e));var p=(r,e,t)=>f(r,typeof e!="symbol"?e+"":e,t);class I{constructor(e=new Map,t=new Set,a=new Set){p(this,"postgresVirtualTables");p(this,"postgresActiveConnections");p(this,"motherduckDatabases");this.postgresVirtualTables=e,this.postgresActiveConnections=t,this.motherduckDatabases=a}analyzeQuery(e){const t=this.extractTableReferences(e),a=[],i=[],n=[];let o;for(const s of t){const l=this.classifyTable(s);l.type==="postgresql"?(a.push(s),o||(o=l.connectionId)):l.type==="motherduck"?i.push(s):n.push(s)}const c=this.determineTarget(a,i,n,o);return y(m({},c),{tableReferences:t,postgresqlTables:a,motherduckTables:i,localTables:n,connectionId:o})}extractTableReferences(e){const t=[],a=new Set,i=[{pattern:/(?:FROM|JOIN|UPDATE|INSERT\s+INTO)\s+(?:[\w\s]*\s+)?(?:["']([^"']+)["']|([a-zA-Z_][a-zA-Z0-9_]*))\.(?:["']([^"']+)["']|([a-zA-Z_][a-zA-Z0-9_]*))\.(?:["']([^"']+)["']|([a-zA-Z_][a-zA-Z0-9_]*))/gi,type:"three-part"},{pattern:/(?:FROM|JOIN|UPDATE|INSERT\s+INTO)\s+(?:[\w\s]*\s+)?(?:["']([^"']+)["']|([a-zA-Z_][a-zA-Z0-9_]*))\.(?:["']([^"']+)["']|([a-zA-Z_][a-zA-Z0-9_]*))/gi,type:"two-part"},{pattern:/(?:FROM|JOIN|UPDATE|INSERT\s+INTO)\s+(?:[\w\s]*\s+)?(?:["']([^"']+)["']|([a-zA-Z_][a-zA-Z0-9_]*))/gi,type:"single"}];let n=!1;for(const{pattern:o,type:c}of i){if(c==="single"&&n)continue;o.lastIndex=0;let s;for(;(s=o.exec(e))!==null;){let l=null,d="";if(c==="three-part"){const u=s[1]||s[2],h=s[3]||s[4],b=s[5]||s[6];u&&h&&b&&(l={database:u,schema:h,table:b,isQuoted:!!(s[1]||s[3]||s[5])},d="".concat(u,".").concat(h,".").concat(b),n=!0)}else if(c==="two-part"){const u=s[1]||s[2],h=s[3]||s[4];u&&h&&(l={schema:u,table:h,isQuoted:!!(s[1]||s[3])},d="".concat(u,".").concat(h),n=!0)}else if(c==="single"){const u=s[1]||s[2];u&&(l={table:u,isQuoted:!!s[1]},d=u)}l&&!a.has(d)&&(a.add(d),t.push(l))}}return t}classifyTable(e){for(const[t,a]of this.postgresVirtualTables){const i=e.schema===a.schemaName||!e.schema&&a.schemaName==="public",n=e.table===a.tableName;if(i&&n)return{type:"postgresql",connectionId:a.connectionId}}return e.database&&this.motherduckDatabases.has(e.database)?{type:"motherduck"}:e.database&&(e.database.includes("my_db")||e.database.includes("sample_data")||e.database.startsWith("md:"))?{type:"motherduck"}:{type:"local"}}determineTarget(e,t,a,i){const n=e.length,o=t.length,c=a.length;if(n+o+c===0)return{target:"local",confidence:.5,reasoning:"No table references found, defaulting to local execution"};if(n>0&&o===0&&c===0)return{target:"postgresql",confidence:i&&this.postgresActiveConnections.has(i)?.95:.7,reasoning:"All ".concat(n," table(s) reference PostgreSQL. Connection: ").concat(i?"active":"not found")};if(o>0&&n===0&&c===0)return{target:"motherduck",confidence:.9,reasoning:"All ".concat(o," table(s) reference MotherDuck databases")};if(c>0&&n===0&&o===0)return{target:"local",confidence:.8,reasoning:"All ".concat(c," table(s) appear to be local DuckDB tables")};const l=[];return n>0&&l.push("".concat(n," PostgreSQL")),o>0&&l.push("".concat(o," MotherDuck")),c>0&&l.push("".concat(c," local")),{target:"hybrid",confidence:1,reasoning:"Cross-database query detected: ".concat(l.join(", ")," tables")}}updatePostgreSQLState(e,t){this.postgresVirtualTables=e,this.postgresActiveConnections=t}updateMotherDuckState(e){this.motherduckDatabases=e}}function $(r,e,t,a){return new I(e,t,a).analyzeQuery(r)}export{I as QueryRouter,$ as analyzeQuery};
