var y=Object.create;var P=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var A=Object.getOwnPropertyNames;var R=Object.getPrototypeOf,z=Object.prototype.hasOwnProperty;var m=(t,e)=>P(t,"name",{value:e,configurable:!0});var K=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports);var $=(t,e,o,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of A(e))!z.call(t,r)&&r!==o&&P(t,r,{get:()=>e[r],enumerable:!(s=w(e,r))||s.enumerable});return t};var Q=(t,e,o)=>(o=t!=null?y(R(t)):{},$(e||!t||!t.__esModule?P(o,"default",{value:t,enumerable:!0}):o,t));import{CharStream as L,CommonTokenStream as N}from"antlr4ng";var v=/[\w]$/,T=/\r\n|\n|\r/g;function S(t,e){let o=t.column,s=t.column+(t.text?.length||0),r=t.line,n=t.type!==e||!t.text?r:r+(t.text.match(T)?.length||0);return{startColumn:o,startLine:r,endColumn:s,endLine:n}}m(S,"getTokenPosition");function W(t,e,o,s){let r=e.column-1;for(let n=0;n<t.size;n++){let i=t.get(n),{startColumn:u,startLine:a,endColumn:c,endLine:l}=S(i,o);if(l>e.line||a===e.line&&c>r)return s?n:n>0&&a===e.line&&u===r&&v.test(t.get(n-1).text||"")?n-1:t.get(n).type===o?n+1:n}}m(W,"findCursorTokenIndex");function C(t,e){let o=t.split(T),s=t.match(T),r="";s&&(r=s[0]);let n=0;return o.reduce((i,u,a)=>(e.line-1===a&&(i.length?n=i.length+e.column:n=e.column-1),a===0?u:i+r+u),""),n}m(C,"getCursorIndex");function _(t,e){return t===e.startTokenIndex}m(_,"isStartingToWriteRule");function B(t,e){let o=t.slice(0,e-1),s=t.slice(e-1),r=o.lastIndexOf(";"),n=s.indexOf(";"),i=r>-1?r+1:0,u=n>-1?n+o.length:t.length,a=t.slice(i,u),c=e-i;return{statement:a,cursorIndex:c}}m(B,"getCurrentStatement");var x=`(\\s|\r
|
|\r)+`,M=new RegExp(`^(${x})?explain${x}$`),F=new RegExp(`^(${x})?\\S+${x}`);function Z(t,e){let o=C(t,e),s=B(t,o),r=s.statement.slice(0,s.cursorIndex).toLowerCase();return!!(o===0||!r.match(F)||r.match(M))}m(Z,"shouldSuggestTemplates");function h(t,e,o){let s=L.fromString(o),r=new t(s),n=new N(r),i=new e(n);return i.removeErrorListeners(),i}m(h,"createParser");function q(t,e){let o=L.fromString(e);return new t(o)}m(q,"createLexer");var d=class{static{m(this,"SqlErrorListener")}constructor(e){this.errors=[],this.whitespaceToken=e}syntaxError(e,o,s,r,n){if(o){let i=S(o,this.whitespaceToken);this.errors.push({message:n,...i})}else this.errors.push({message:n,startLine:s,startColumn:r,endLine:s,endColumn:r})}reportAmbiguity(){}reportAttemptingFullContext(){}reportContextSensitivity(){}};var G=(o=>(o.Autocomplete="autocomplete",o.Tokens="tokens",o))(G||{});function mt(t,e,o,s,r,n,i,u){let a=h(e,o,t),{tokenStream:c}=a,l=new d(s);a.removeErrorListeners(),a.addErrorListener(l);let f=u(a),p=O(f,c,i,r,n);if(p.length)return{statementPositions:b(t,p),strategy:"autocomplete"};let g=I(c,r,n);return{statementPositions:b(t,g),strategy:"tokens"}}m(mt,"extractStatementPositionsFromQuery");function I(t,e,o,s=0){let r=0,n=!1,i=t.size-2,u=[];for(let a=s;a<=i;a++){let c=t.get(a),l=c.type===o,f=e.includes(c.type);if(!n&&f)continue;if(n||(n=!0,r=c.start),l&&r===c.start){n=!1;continue}if(l||a===i){let g=c.text?.length||0,k=c.start+g,E={startIndex:r,endIndex:k};u.push(E)}l&&(n=!1)}return u}m(I,"extractStatementsUsingTokens");function O(t,e,o,s,r){o.visit(t);let n=o.statementPositions,i=n[n.length-1];if(!i)return[];let u=U(e,s);if(i.endIndex===u)return n;let a=I(e,s,r,o.lastTokenIndex);return a[0]&&i.endIndex>=a[0].startIndex?[]:[...n,...a]}m(O,"extractStatementsUsingAutocomplete");function U(t,e){let o=t.size-2;for(let s=o;s>=0;s--){let r=t.get(s);if(!e.includes(r.type))return r.start+(r.text?.length||0)}return-1}m(U,"getLastNonEmptyTokenEndIndex");function b(t,e){let o=Array.from(t),s=new Map,r=0;for(let n=0;n<o.length;n++){s.set(n,r);let i=o[n];if(i===void 0)throw new Error(`Character at position ${n} not found`);r+=i.length}return s.set(o.length,r),e.map(({startIndex:n,endIndex:i})=>{let u=s.get(n),a=s.get(i);if(u===void 0||a===void 0)throw new Error(`Actual start: ${n} or end: ${i} index not found`);return{startIndex:u,endIndex:a}})}m(b,"normalizeStatementPositions");function ut(t,e){return e?e.symbol.start+1:t.type===-1?t.start:t.start+(t.text?.length??0)}m(ut,"getStatementEndIndex");export{m as a,K as b,Q as c,T as d,W as e,_ as f,Z as g,h,q as i,d as j,G as k,mt as l,ut as m};
