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