'use strict';(function(d){"object"==typeof exports&&"object"==typeof module?d(require("../../lib/codemirror"),require("./searchcursor"),require("../dialog/dialog")):"function"==typeof define&&define.amd?define(["../../lib/codemirror","./searchcursor","../dialog/dialog"],d):d(CodeMirror)})(function(d){function C(a){var b=a.query;"string"==typeof b?b=new RegExp(b.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&"),a.caseInsensitive?"gi":"g"):b.global||(b=new RegExp(a.text,a.caseInsensitive?"gi":"g"));
return{token:function(a){b.lastIndex=a.pos;var e=b.exec(a.string);if(e&&e.index==a.pos)return a.pos+=e[0].length||1,"searching";e?a.pos=e.index:a.skipToEnd()}}}function D(){this.overlay=this.posFrom=this.posTo=this.lastPattern=this.pattern=null}function k(a){return a.state.search||(a.state.search=new D)}function n(a,b,c){var e,d="";b&&(e=b.caseInsensitive,d=b.query);return a.getSearchCursor(d,c,{caseFold:e,multiline:!0})}function E(a,b,c,e,d){return a.openDialog(b,e,{value:c,selectValueOnOpen:!0,
closeOnEnter:!1,onClose:function(){p(a)},onKeyDown:d,closeOnBlur:!1,bottom:!0})}function v(a,b,c,e,g){if(a.openDialog)return a.openDialog(b,function(a,b){d.e_stop(b);return g.apply(this,arguments)},{value:e,selectValueOnOpen:!0,closeOnBlur:!1,bottom:!0,closeOnEnter:!1});g(prompt(c,e))}function F(a,b,c,e,d,f){if(a.openConfirm)return a.openConfirm(b,e,{onKeyDown:f,onClose:d,closeOnBlur:!1});if(confirm(c))e[0]()}function m(a,b){var c=$(a.display.wrapper),d=void 0!==b?b:c.find(".CodeMirror-search-field").val(),
g=c.find(".CodeMirror-search-re-field").is(":checked"),c=c.find(".CodeMirror-search-case-field").is(":checked"),f;if(g)try{f={query:new RegExp(d,c?"i":""),text:d,caseInsensitive:c,regex:g}}catch(h){}else f={query:d,text:d,caseInsensitive:c,regex:g};f&&f.query&&("string"==typeof f.query?""!=f.query:!f.query.test(""))||(f={query:/x^/,text:""});return f}function r(a,b,c){b.pattern=c;a.removeOverlay(b.overlay,c.caseInsensitive);b.overlay=C(c);a.addOverlay(b.overlay);a.showMatchesOnScrollbar&&(b.annotate&&
(b.annotate.clear(),b.annotate=null),b.annotate=a.showMatchesOnScrollbar(c.query,c.caseInsensitive))}function y(a,b){return a.query==b.query&&a.regex==b.regex&&a.caseInsensitive==b.caseInsensitive}function w(a,b,c,e){var g,f,h=k(a);if(h.pattern&&(f=m(a))){if(y(h.pattern,f))return t(a,b)}else f=f||a.getSelection()||h.lastPattern||"";"string"==typeof f&&(f=m(a,f));f.query instanceof RegExp&&"x^"==f.query.source&&(f=null);if(c&&a.openDialog){var x=null,n=function(b,c){var e=m(a);d.e_stop(c);h.pattern&&
y(h.pattern,h)||(r(a,h,e),h.posFrom=h.posTo=a.getCursor());x&&(x.style.opacity=1);t(a,c.shiftKey,function(b,c){var d;3>c.line&&document.querySelector&&(d=a.display.wrapper.querySelector(".CodeMirror-dialog"))&&d.getBoundingClientRect().bottom-4>a.cursorCoords(c,"window").top&&((x=d).style.opacity=.4)})};h.pattern||(q(a),g=E(a,z(f),f?f.text:"",n,function(b,c){var e=d.keyName(b),f=a.getOption("extraKeys"),e=f&&f[e]||d.keyMap[a.getOption("keyMap")][e],f=m(a);if(!(f.query instanceof RegExp&&"x^"==f.query.source))if("findNext"==
e||"findPrev"==e||"findPersistentNext"==e||"findPersistentPrev"==e)d.e_stop(b),r(a,k(a),f),a.execCommand(e);else if("find"==e||"findPersistent"==e)d.e_stop(b),n(c,b)}));e&&f&&(r(a,h,f),t(a,b))}else q(a),g=v(a,z(),d.I18N.getMessage("Search_for")+":",f,function(c){c&&!h.pattern&&a.operation(function(){r(a,h,m(a));h.posFrom=h.posTo=a.getCursor();t(a,b)})});g&&u(a,{close:function(){g();h.dialog=null}})}function t(a,b,c){a.operation(function(){var e=k(a),g=n(a,e.pattern,b?e.posFrom:e.posTo);if(!g.find(b)&&
(g=n(a,e.pattern,b?d.Pos(a.lastLine()):d.Pos(a.firstLine(),0)),!g.find(b)))return;a.setSelection(g.from(),g.to());a.scrollIntoView({from:g.from(),to:g.to()},20);e.posFrom=g.from();e.posTo=g.to();c&&c(g.from(),g.to())})}function p(a){return a.operation(function(){var b,c=k(a);if(!c.closing){c.dialog&&(c.closing=!0,c.dialog.close(),c.closing=!1,b=!0);c.lastPattern=c.pattern;if(!c.pattern)return b;c.pattern=null;a.removeOverlay(c.overlay);c.annotate&&(c.annotate.clear(),c.annotate=null);return!0}})}
function A(a,b,c){a.operation(function(){for(var d=n(a,b);d.findNext();)if("string"!=typeof query){var g=a.getRange(d.from(),d.to()).match(b.query);d.replace(c.replace(/\$(\d)/g,function(a,b){return g[b]}))}else d.replace(c)})}function B(a,b){if(!a.getOption("readOnly")){var c=a.getSelection()||k(a).lastPattern||"";"string"==typeof c&&(c=m(a,c));var e=k(a),g=(b?d.I18N.getMessage("Replace_all_with"):d.I18N.getMessage("Replace"))+":";q(a);var f=v(a,g+' <input type="text" style="width: 40em" class="CodeMirror-search-field"/> <input type="checkbox" style="width: auto" class="CodeMirror-search-re-field"/> <span style="color: #888" class="CodeMirror-search-hint">.*</span> <input type="checkbox" style="width: auto" class="CodeMirror-search-case-field"/> <span style="color: #888" class="CodeMirror-search-hint">Aa</span>',
g,c.text,function(c){if(c){var f=m(a);q(a);var g=v(a,'<span class="CodeMirror-search-label">'+d.I18N.getMessage("Replace_with")+':</span> <input type="text" style="width: 40em" class="CodeMirror-search-field"/>',d.I18N.getMessage("Replace_with")+":","",function(g){if(b)A(a,f,g);else{p(a);var l=n(a,f,a.getCursor("from")),k=function(){var b=l.from(),c;if(!(c=l.findNext())&&(l=n(a,f),!(c=l.findNext())||b&&l.from().line==b.line&&l.from().ch==b.ch))return;a.setSelection(l.from(),l.to());a.showInCenter?
a.showInCenter():a.scrollIntoView({from:l.from(),to:l.to()});window.setTimeout(function(){q(a);var b=F(a,'<span class="CodeMirror-search-label">'+d.I18N.getMessage("Replace_")+"</span> <button>"+d.I18N.getMessage("Yes")+"</button> <button>"+d.I18N.getMessage("No")+"</button> <button>"+d.I18N.getMessage("Replace_All")+"</button> <button>"+d.I18N.getMessage("Stop")+"</button>",d.I18N.getMessage("Replace_"),[function(){m(c)},k,function(){A(a,f,g)}],function(a){e.dialog=null},function(b){if("Esc"==d.keyName(b))return d.e_stop(b),
p(a)});b&&u(a,{close:function(){b();e.dialog=null}})},1)},m=function(a){l.replace("string"==typeof c?g:g.replace(/\$(\d)/g,function(b,c){return a[c]}));k()};k()}});g&&u(a,{close:function(){g();e.dialog=null}})}});f&&u(a,{close:function(){f();e.dialog=null}})}}var z=function(a){return'<span class="CodeMirror-search-label">'+d.I18N.getMessage("Search_for")+':</span> <input type="text" style="width: 40em" class="CodeMirror-search-field"/> <input type="checkbox" style="width: auto" class="CodeMirror-search-re-field" '+
(a&&a.regex?"checked":"")+' /> <span style="color: #888" class="CodeMirror-search-hint">.*</span> <input type="checkbox" style="width: auto" class="CodeMirror-search-case-field" '+(a&&a.caseInsensitive?"checked":"")+' /> <span style="color: #888" class="CodeMirror-search-hint">Aa</span>'},q=function(a){a=k(a);a.dialog&&(a.closing=!0,a.dialog.close(),a.closing=!1)},u=function(a,b){k(a).dialog=b};d.commands.find=d.commands.findPersistent=function(a){p(a);w(a,!1,!0)};d.commands.findNext=d.commands.findPersistentNext=
function(a){w(a,!1,!0,!0)};d.commands.findPrev=d.commands.findPersistentPrev=function(a){w(a,!0,!0,!0)};d.commands.clearSearch=p;d.commands.replace=B;d.commands.replaceAll=function(a){B(a,!0)}});
