!function(t){"object"==typeof exports&&"object"==typeof module?t(require("../../lib/codemirror"),require("../../mode/sql/sql")):"function"==typeof define&&define.amd?define(["../../lib/codemirror","../../mode/sql/sql"],t):t(CodeMirror)}((function(t){"use strict";var e,r,n,o,i=t.Pos,s=t.cmpPos;function a(t){return"[object Array]"==Object.prototype.toString.call(t)}function u(t){return"string"==typeof t?t:t.text}function l(t,e){return a(e)&&(e={columns:e}),e.text||(e.text=t),e}function f(t){return e[t.toUpperCase()]}function c(t){var e={};for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r]);return e}function p(t,e){var r=t.length,n=u(e).substr(0,r);return t.toUpperCase()===n.toUpperCase()}function d(t,e,r,n){if(a(r))for(var o=0;o<r.length;o++)p(e,r[o])&&t.push(n(r[o]));else for(var i in r)if(r.hasOwnProperty(i)){var s=r[i];p(e,s=s&&!0!==s?s.displayText?{text:s.text,displayText:s.displayText}:s.text:i)&&t.push(n(s))}}function g(t){"."==t.charAt(0)&&(t=t.substr(1));for(var e=t.split(o+o),r=0;r<e.length;r++)e[r]=e[r].replace(new RegExp(o,"g"),"");return e.join(o)}function h(t){for(var e=u(t).split("."),r=0;r<e.length;r++)e[r]=o+e[r].replace(new RegExp(o,"g"),o+o)+o;var n=e.join(".");return"string"==typeof t?n:((t=c(t)).text=n,t)}function v(t,e){for(var r=t.split(/\s+/),n=0;n<r.length;n++)r[n]&&e(r[n].replace(/[`,;]/g,""))}function x(t,e){for(var r=e.doc,n=r.getValue(),o=t.toUpperCase(),a="",u="",l=[],c={start:i(0,0),end:i(e.lastLine(),e.getLineHandle(e.lastLine()).length)},p=n.indexOf(";");-1!=p;)l.push(r.posFromIndex(p)),p=n.indexOf(";",p+1);l.unshift(i(0,0)),l.push(i(e.lastLine(),e.getLineHandle(e.lastLine()).text.length));for(var d=null,g=e.getCursor(),h=0;h<l.length;h++){if((null==d||s(g,d)>0)&&s(g,l[h])<=0){c={start:d,end:l[h]};break}d=l[h]}if(c.start){var x=r.getRange(c.start,c.end,!1);for(h=0;h<x.length&&(v(x[h],(function(t){var e=t.toUpperCase();e===o&&f(a)&&(u=a),"AS"!==e&&(a=t)})),!u);h++);}return u}t.registerHelper("hint","sql",(function(s,p){e=function(t){var e={};if(a(t))for(var r=t.length-1;r>=0;r--){var n=t[r];e[u(n).toUpperCase()]=l(u(n),n)}else if(t)for(var o in t)e[o.toUpperCase()]=l(o,t[o]);return e}(p&&p.tables);var v=p&&p.defaultTable,m=p&&p.disableKeywords;r=v&&f(v),n=function(e){var r=e.doc.modeOption;return"sql"===r&&(r="text/x-sql"),t.resolveMode(r).keywords}(s),o=function(e){var r=e.doc.modeOption;return"sql"===r&&(r="text/x-sql"),t.resolveMode(r).identifierQuote||"`"}(s),v&&!r&&(r=x(v,s)),(r=r||[]).columns&&(r=r.columns);var y,b,C,q=s.getCursor(),A=[],j=s.getTokenAt(q);if(j.end>q.ch&&(j.end=q.ch,j.string=j.string.slice(0,q.ch-j.start)),j.string.match(/^[.`"'\w@][\w$#]*$/g)?(C=j.string,y=j.start,b=j.end):(y=b=q.ch,C=""),"."==C.charAt(0)||C.charAt(0)==o)y=function(t,n,s,a){for(var u=!1,l=[],p=n.start,v=!0;v;)v="."==n.string.charAt(0),u=u||n.string.charAt(0)==o,p=n.start,l.unshift(g(n.string)),"."==(n=a.getTokenAt(i(t.line,n.start))).string&&(v=!0,n=a.getTokenAt(i(t.line,n.start)));var m=l.join(".");d(s,m,e,(function(t){return u?h(t):t})),d(s,m,r,(function(t){return u?h(t):t})),m=l.pop();var y=l.join("."),b=!1,C=y;if(!f(y)){var q=y;(y=x(y,a))!==q&&(b=!0)}var A=f(y);return A&&A.columns&&(A=A.columns),A&&d(s,m,A,(function(t){var e=y;return 1==b&&(e=C),"string"==typeof t?t=e+"."+t:(t=c(t)).text=e+"."+t.text,u?h(t):t})),p}(q,j,A,s);else{var w=function(t,e){return"object"==typeof t?t.className=e:t={text:t,className:e},t};d(A,C,r,(function(t){return w(t,"CodeMirror-hint-table CodeMirror-hint-default-table")})),d(A,C,e,(function(t){return w(t,"CodeMirror-hint-table")})),m||d(A,C,n,(function(t){return w(t.toUpperCase(),"CodeMirror-hint-keyword")}))}return{list:A,from:i(q.line,y),to:i(q.line,b)}}))}));