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