import{a as l,d as Q,e as E,h as p,j as A}from"./chunk-YCFX3Q22.js";import*as w from"antlr4-c3";function U(r,n,e,o,t){let s=p(r,n,t),i=new A(e);return s.removeErrorListeners(),s.addErrorListener(i),o(s),{errors:i.errors}}l(U,"parseQueryWithoutCursor");var v=/^'(.*)'$/;function q(r,n,e,o,t,s,i,a,m,d){let c=p(r,n,a),{tokenStream:u}=c,y=new A(e);c.removeErrorListeners(),c.addErrorListener(y),s(c);let T=new w.CodeCompletionCore(c);T.ignoredTokens=o,T.preferredRules=t;let g=E(u,m,e);if(g===void 0)throw new Error(`Could not find cursor token index for line: ${m.line}, column: ${m.column}`);let b=[],{tokens:P,rules:f}=T.collectCandidates(g,d);P.forEach((S,L)=>{let R=c.vocabulary.getLiteralName(L)?.replace(v,"$1")||c.vocabulary.getSymbolicName(L);if(!R)throw new Error(`Could not get name for token ${L}`);b.push({value:R})});let x={errors:y.errors,suggestKeywords:b};return i(x,f,u,g,m,a)}l(q,"parseQuery");function H(r){let n=r.split(Q),e="|",o;for(let u=0;u<n.length;u++)if(n[u]?.includes(e)){o=u;break}if(o===void 0)throw new Error(`Cursor not provided for query ${r}`);let t=n[o];if(!t)throw new Error(`Line ${o} not found`);let[s,i,...a]=r.split(e),[m,d,...c]=t.split(e);if(a.length>0||c.length>0)throw new Error(`Multiple cursors not allowed, but present in query ${r}`);if(s===void 0||i===void 0||m===void 0||d===void 0)throw new Error(`Cursor not provided for query ${r}`);return[s+i,{line:o+1,column:m.length+1}]}l(H,"separateQueryAndCursor");import*as C from"antlr4-c3";var I=class extends C.TypedSymbol{static{l(this,"TableSymbol")}constructor(n,e,o,t){super(n,t),this.name=n,this.alias=e,this.columns=o}},k=class extends C.VariableSymbol{static{l(this,"VariableSymbol")}};function G(r=[]){let n=r.reduce((e,o)=>{let t=e[o.name]?.aliases??new Set;return o.alias&&t.add(o.alias),e[o.name]={aliases:t,columns:o.columns},e},{});return Object.keys(n).reduce((e,o)=>{let t=n[o]?.aliases,s=n[o]?.columns;return t&&t.size>0?t?.forEach(i=>{e.push({name:o,alias:i,columns:s})}):e.push({name:o,columns:s}),e},[])}l(G,"getUniqueTableSuggestions");function O(r){let n=r.symbolTable.getNestedSymbolsOfTypeSync(I);return G(n)}l(O,"getTablesFromSymbolTable");var h=class extends C.TypedSymbol{static{l(this,"ColumnAliasSymbol")}constructor(n,e){super(n,e),this.name=n}};function N(r){return r.symbolTable.getNestedSymbolsOfTypeSync(h).map(({name:n})=>({name:n}))}l(N,"getColumnAliasesFromSymbolTable");function $(r,n){let e=n.symbolWithContextSync(r);if(e)return e;if(r.parent)return $(r.parent,n)}l($,"getScope");function j(r,n,e){let o=n;for(;o<r.size;){let s=r.get(o);if(s.type===e.CLOSING_BRACKET||s.type===e.SEMICOLON)return{cursorIndex:s.start,tokenIndex:o};if(s.type===e.OPENING_BRACKET)return;o++}let t=r.size-1;return{cursorIndex:r.get(t).start,tokenIndex:t}}l(j,"getClosingBracketIndex");function K(r,n,e){let o=r.get(r.size-1).start,t=n,s=!1;for(;t>=0&&t<r.size;){let i=r.get(t);if(i.type===e.OPENING_BRACKET||i.type===e.CLOSING_BRACKET||i.type===e.SEMICOLON){if(s)break;t=n,s=!0}if(i.type===e.FROM){let a=j(r,n,e);if(!a)break;let m=M(r,t,a.tokenIndex,e),d=m?{start:m,end:a.cursorIndex}:void 0,c=B(r,e,a.tokenIndex,e.SELECT),u=c?{start:c.start,end:a.cursorIndex}:void 0;return{start:i.start,end:a.cursorIndex,type:"from",joinTableQueryPosition:d,selectTableQueryPosition:u}}s?t++:t--,t===-1&&(t=n,s=!0)}for(t=n;t>=0;){let i=r.get(t);if(i.type===e.SEMICOLON)return;if(i.type===e.ALTER&&!!!B(r,e,t,e.ALTER))return{start:i.start,end:o,type:"alter"};if(i.type===e.INSERT||i.type===e.UPSERT)return{start:i.start,end:o,type:"insert"};if(i.type===e.UPDATE)return{start:i.start,end:o,type:"update"};t--}}l(K,"getTableQueryPosition");function M(r,n,e,o){let t=n;for(;t<e;){let s=r.get(t);if(s.type===o.JOIN)return s.stop+1;t++}}l(M,"getJoinIndex");function B(r,n,e,o){let t=e-1;for(;t>-1;){let s=r.get(t);if(s.type===n.SEMICOLON)return;if(s.type===o)return s;t--}}l(B,"getPreviousToken");function oe(r,n,e,o,t,s,i,a,m){let d=E(s,i,o.SPACE,!0);if(!d)throw new Error(`Could not find actualCursorTokenIndex at Ln ${i.line}, Col ${i.column}`);let c={},u=K(s,d,o);if(u){let y=a.slice(u.start,u.end),T=p(r,n,y),g=t(T,u.type);if(e.visit(g),m&&u.joinTableQueryPosition){let f=a.slice(u.joinTableQueryPosition.start,u.joinTableQueryPosition.end),x=p(r,n,f),S=t(x,"from");e.visit(S)}if(u.selectTableQueryPosition){let f=a.slice(u.selectTableQueryPosition.start,u.selectTableQueryPosition.end),x=p(r,n,f),S=t(x,"select");e.visit(S)}let b=O(e);b.length&&(c.tableContextSuggestion={tables:b});let P=N(e);P.length&&(c.suggestColumnAliases=P.map(({name:f})=>({name:f})))}return c}l(oe,"getContextSuggestions");export{I as a,k as b,G as c,h as d,$ as e,B as f,oe as g,U as h,q as i,H as j};
