'use strict';(function(d){"object"==typeof exports&&"object"==typeof module?d(require("../../lib/codemirror")):"function"==typeof define&&define.amd?define(["../../lib/codemirror"],d):d(CodeMirror)})(function(d){function p(b,a){return"pairs"==a&&"string"==typeof b?b:"object"==typeof b&&null!=b[a]?b[a]:x[a]}function y(b){for(var a=0;a<b.length;a++){var c=b.charAt(a),d="'"+c+"'";m[d]||(m[d]=A(c))}}function A(b){return function(a){return B(a,b)}}function t(b){var a=b.state.closeBrackets;return!a||a.override?
a:b.getModeAt(b.getCursor()).closeBrackets||a}function B(b,a){var c=t(b);if(!c||b.getOption("disableInput"))return d.Pass;var f=p(c,"pairs"),e=f.indexOf(a);if(-1==e)return d.Pass;var C=p(c,"closeBefore");c=p(c,"triples");for(var r=f.charAt(e+1)==a,m=b.listSelections(),u=0==e%2,l,v=0;v<m.length;v++){var q=m[v],g=q.head;var h=b.getRange(g,k(g.line,g.ch+1));if(u&&!q.empty())h="surround";else if(!r&&u||h!=a)if(r&&1<g.ch&&0<=c.indexOf(a)&&b.getRange(k(g.line,g.ch-2),g)==a+a){if(2<g.ch&&/\bstring/.test(b.getTokenTypeAt(k(g.line,
g.ch-2))))return d.Pass;h="addFour"}else if(r){q=0==g.ch?" ":b.getRange(k(g.line,g.ch-1),g);if(d.isWordChar(h)||q==a||d.isWordChar(q))return d.Pass;h="both"}else if(u&&(0===h.length||/\s/.test(h)||-1<C.indexOf(h)))h="both";else return d.Pass;else h=r&&D(b,g)?"both":0<=c.indexOf(a)&&b.getRange(g,k(g.line,g.ch+3))==a+a+a?"skipThree":"skip";if(!l)l=h;else if(l!=h)return d.Pass}var n=e%2?f.charAt(e-1):a,w=e%2?a:f.charAt(e+1);b.operation(function(){if("skip"==l)b.execCommand("goCharRight");else if("skipThree"==
l)for(var a=0;3>a;a++)b.execCommand("goCharRight");else if("surround"==l){var c=b.getSelections();for(a=0;a<c.length;a++)c[a]=n+c[a]+w;b.replaceSelections(c,"around");c=b.listSelections().slice();for(a=0;a<c.length;a++){var f=c,g=a;var e=c[a];var h=0<d.cmpPos(e.anchor,e.head);e={anchor:new k(e.anchor.line,e.anchor.ch+(h?-1:1)),head:new k(e.head.line,e.head.ch+(h?1:-1))};f[g]=e}b.setSelections(c)}else"both"==l?(b.replaceSelection(n+w,null),b.triggerElectric(n+w),b.execCommand("goCharLeft")):"addFour"==
l&&(b.replaceSelection(n+n+n+n,"before"),b.execCommand("goCharRight"))})}function z(b,a){b=b.getRange(k(a.line,a.ch-1),k(a.line,a.ch+1));return 2==b.length?b:null}function D(b,a){var c=b.getTokenAt(k(a.line,a.ch+1));return/\bstring/.test(c.type)&&c.start==a.ch&&(0==a.ch||!/\bstring/.test(b.getTokenTypeAt(a)))}var x={pairs:"()[]{}''\"\"",closeBefore:")]}'\":;>",triples:"",explode:"[]{}"},k=d.Pos;d.defineOption("autoCloseBrackets",!1,function(b,a,c){c&&c!=d.Init&&(b.removeKeyMap(m),b.state.closeBrackets=
null);a&&(y(p(a,"pairs")),b.state.closeBrackets=a,b.addKeyMap(m))});var m={Backspace:function(b){var a=t(b);if(!a||b.getOption("disableInput"))return d.Pass;var c=p(a,"pairs");a=b.listSelections();for(var f=0;f<a.length;f++){if(!a[f].empty())return d.Pass;var e=z(b,a[f].head);if(!e||0!=c.indexOf(e)%2)return d.Pass}for(f=a.length-1;0<=f;f--)c=a[f].head,b.replaceRange("",k(c.line,c.ch-1),k(c.line,c.ch+1),"+delete")},Enter:function(b){var a=t(b);a=a&&p(a,"explode");if(!a||b.getOption("disableInput"))return d.Pass;
for(var c=b.listSelections(),f=0;f<c.length;f++){if(!c[f].empty())return d.Pass;var e=z(b,c[f].head);if(!e||0!=a.indexOf(e)%2)return d.Pass}b.operation(function(){var a=b.lineSeparator()||"\n";b.replaceSelection(a+a,null);b.execCommand("goCharLeft");c=b.listSelections();for(a=0;a<c.length;a++){var d=c[a].head.line;b.indentLine(d,null,!0);b.indentLine(d+1,null,!0)}})}};y(x.pairs+"`")});
