text: define(["jquery","core/log","core/str","core/templates","core/notification"],function(a,b,c,d,e){var f={DOWN:40,ENTER:13,SPACE:32,ESCAPE:27,COMMA:44,UP:38},g=a.now(),h=function(b,c){var d=a(document.getElementById(c.selectionId)),e=d.children("[aria-selected=true]").length;for(b%=e;b<0;)b+=e;var f=a(d.children("[aria-selected=true]").get(b)),g=c.selectionId+"-"+b;d.children().attr("data-active-selection",!1).attr("id",""),f.attr("data-active-selection",!0).attr("id",g),d.attr("aria-activedescendant",g)},i=function(b,c,f){var g=[],i=a(document.getElementById(c.selectionId)),j=i.attr("aria-activedescendant"),k=!1;j&&(k=a(document.getElementById(j)).attr("data-value")),f.children("option").each(function(b,c){if(a(c).prop("selected")){var d;d=a(c).data("html")?a(c).data("html"):a(c).html(),g.push({label:d,value:a(c).attr("value")})}});var l=a.extend({items:g},b,c);d.render("core/form_autocomplete_selection",l).done(function(b){i.empty().append(a(b).html()),k!==!1&&i.children("[aria-selected=true]").each(function(b,d){a(d).attr("data-value")===k&&h(b,c)})}).fail(e.exception)},j=function(a){"undefined"!=typeof M.core_formchangechecker&&M.core_formchangechecker.set_form_changed(),a.change()},k=function(b,c,d,e){var f=a(d).attr("data-value");b.multiple&&e.children("option").each(function(b,c){a(c).attr("value")==f&&(a(c).prop("selected",!1),a(c).attr("data-iscustom")&&a(c).remove())}),i(b,c,e),j(e)},l=function(b,c){var d=a(document.getElementById(c.inputId)),e=a(document.getElementById(c.suggestionsId)),f=e.children("[aria-hidden=false]").length;for(b%=f;b<0;)b+=f;var g=a(e.children("[aria-hidden=false]").get(b)),h=a(e.children("[role=option]")).index(g),i=c.suggestionsId+"-"+h;e.children().attr("aria-selected",!1).attr("id",""),g.attr("aria-selected",!0).attr("id",i),d.attr("aria-activedescendant",i);var j=g.offset().top-e.offset().top+e.scrollTop()-e.height()/2;e.animate({scrollTop:j},100)},m=function(b){var c=a(document.getElementById(b.suggestionsId)),d=c.children("[aria-selected=true]"),e=c.children("[aria-hidden=false]").index(d);l(e+1,b)},n=function(b){var c=a(document.getElementById(b.selectionId)),d=c.children("[data-active-selection=true]");if(!d)return void h(0,b);var e=c.children("[aria-selected=true]").index(d);h(e-1,b)},o=function(b){var c=a(document.getElementById(b.selectionId)),d=c.children("[data-active-selection=true]");if(!d)return void h(0,b);var e=c.children("[aria-selected=true]").index(d);h(e+1,b)},p=function(b){var c=a(document.getElementById(b.suggestionsId)),d=c.children("[aria-selected=true]"),e=c.children("[aria-hidden=false]").index(d);l(e-1,b)},q=function(b){var c=a(document.getElementById(b.inputId)),d=a(document.getElementById(b.suggestionsId));c.attr("aria-expanded",!1).attr("aria-activedescendant",b.selectionId),d.hide().attr("aria-hidden",!0)},r=function(b,f,g,h){var i=a(document.getElementById(f.inputId)),j=a(document.getElementById(f.suggestionsId)),k=!1,m=[];h.children("option").each(function(b,c){a(c).prop("selected")!==!0&&(m[m.length]={label:c.innerHTML,value:a(c).attr("value")})});var n=f.caseSensitive?g:g.toLocaleLowerCase(),o=a.extend({options:m},b,f);d.render("core/form_autocomplete_suggestions",o).done(function(d){j.replaceWith(d),j=a(document.getElementById(f.suggestionsId)),j.show().attr("aria-hidden",!1),j.children().each(function(c,d){d=a(d),b.caseSensitive&&d.text().indexOf(n)>-1||!b.caseSensitive&&d.text().toLocaleLowerCase().indexOf(n)>-1?(d.show().attr("aria-hidden",!1),k=!0):d.hide().attr("aria-hidden",!0)}),i.attr("aria-expanded",!0),h.attr("data-notice")?j.html(h.attr("data-notice")):k?b.tags||l(0,f):c.get_string("nosuggestions","form").done(function(a){j.html(a)})}).fail(e.exception)},s=function(b,c,d){var e=a(document.getElementById(c.inputId)),f=e.val(),g=f.split(","),h=!1;a.each(g,function(c,e){if(e=e.trim(),""!==e&&(b.multiple||d.children("option").prop("selected",!1),d.children("option").each(function(b,c){a(c).attr("value")==e&&(h=!0,a(c).prop("selected",!0))}),!h)){var f=a("<option>");f.append(document.createTextNode(e)),f.attr("value",e),d.append(f),f.prop("selected",!0),f.attr("data-iscustom",!0)}}),i(b,c,d),j(d),e.val(""),q(c)},t=function(b,c,d){var e=a(document.getElementById(c.inputId)),f=a(document.getElementById(c.suggestionsId)),g=f.children("[aria-selected=true]").attr("data-value");b.multiple||d.children("option").prop("selected",!1),d.children("option").each(function(b,c){a(c).attr("value")==g&&a(c).prop("selected",!0)}),i(b,c,d),j(d),b.closeSuggestionsOnSelect?(e.val(""),q(c)):(e.focus(),r(b,c,e.val(),d))},u=function(b,c,d,f,g){var h="form-autocomplete-updateajax";M.util.js_pending(h);var i=a(b.currentTarget).val();g.transport(c.selector,i,function(b){var e=g.processResults(c.selector,b),i=[];if(f.children("option").each(function(b,c){c=a(c),c.prop("selected")?i.push(String(c.attr("value"))):c.remove()}),!c.multiple&&0===f.children("option").length){var j=a("<option>");f.append(j)}a.isArray(e)?(a.each(e,function(b,c){if(i.indexOf(String(c.value))===-1){var d=a("<option>");d.append(c.label),d.attr("value",c.value),f.append(d)}}),f.attr("data-notice","")):f.attr("data-notice",e),r(c,d,"",f),M.util.js_complete(h)},function(a){M.util.js_complete(h),e.exception(a)})},v=function(b,c,d){var e=a(document.getElementById(c.inputId));if(e.on("keydown",function(g){var h="form-autocomplete-addnav-"+c.inputId+"-"+g.keyCode;switch(M.util.js_pending(h),g.keyCode){case f.DOWN:return b.showSuggestions?("true"===e.attr("aria-expanded")?m(c):!e.val()&&b.ajax?require([b.ajax],function(a){u(g,b,c,d,a)}):r(b,c,e.val(),d),g.preventDefault(),M.util.js_complete(h),!1):(M.util.js_complete(h),!0);case f.UP:return p(c),g.preventDefault(),M.util.js_complete(h),!1;case f.ENTER:var i=a(document.getElementById(c.suggestionsId));return"true"===e.attr("aria-expanded")&&i.children("[aria-selected=true]").length>0?t(b,c,d):b.tags&&s(b,c,d),g.preventDefault(),M.util.js_complete(h),!1;case f.ESCAPE:return"true"===e.attr("aria-expanded")&&q(c),g.preventDefault(),M.util.js_complete(h),!1}return M.util.js_complete(h),!0}),e.on("keypress",function(a){var e="form-autocomplete-keypress-"+a.keyCode;return M.util.js_pending(e),a.keyCode===f.COMMA?(b.tags&&s(b,c,d),a.preventDefault(),M.util.js_complete(e),!1):(M.util.js_complete(e),!0)}),e.on("behat:set-value",function(){var f=a(document.getElementById(c.suggestionsId)),g="form-autocomplete-behat";M.util.js_pending(g),"true"===e.attr("aria-expanded")&&f.children("[aria-selected=true]").length>0?t(b,c,d):b.tags&&s(b,c,d),M.util.js_complete(g)}),e.on("blur",function(){var f="form-autocomplete-blur";M.util.js_pending(f),window.setTimeout(function(){var g=a(document.activeElement);g.attr("id")!=e.attr("id")&&(b.tags&&s(b,c,d),q(c)),M.util.js_complete(f)},500)}),b.showSuggestions){var g=a(document.getElementById(c.downArrowId));g.on("click",function(a){var f="form-autocomplete-show-suggestions";M.util.js_pending(f),e.focus(),!e.val()&&b.ajax?require([b.ajax],function(e){u(a,b,c,d,e)}):r(b,c,e.val(),d),M.util.js_complete(f)})}var h=a(document.getElementById(c.suggestionsId));h.parent().on("click","[role=option]",function(e){var f="form-autocomplete-parent";M.util.js_pending(f);var g=a(e.currentTarget).closest("[role=option]"),h=a(document.getElementById(c.suggestionsId)),i=h.children("[aria-hidden=false]").index(g);l(i,c),t(b,c,d),M.util.js_complete(f)});var i=a(document.getElementById(c.selectionId));i.on("click","[role=listitem]",function(e){var f="form-autocomplete-clicks";M.util.js_pending(f);var g=a(e.currentTarget);k(b,c,g,d),M.util.js_complete(f)}),i.on("keydown",function(e){var g="form-autocomplete-keydown-"+e.keyCode;switch(M.util.js_pending(g),e.keyCode){case f.DOWN:return o(c),e.preventDefault(),M.util.js_complete(g),!1;case f.UP:return n(c),e.preventDefault(),M.util.js_complete(g),!1;case f.SPACE:case f.ENTER:var h=a(document.getElementById(c.selectionId)).children("[data-active-selection=true]");return h&&(k(b,c,h,d),e.preventDefault()),M.util.js_complete(g),!1}return M.util.js_complete(g),!0}),b.showSuggestions&&(b.ajax?require([b.ajax],function(a){var f=null,g="autocomplete-throttledhandler",h=function(e){u(e,b,c,d,a),M.util.js_complete(g)},i=function(a){null!==f?(window.clearTimeout(f),f=null):M.util.js_pending(g),f=window.setTimeout(h.bind(this,a),300)};e.on("input",i)}):e.on("input",function(e){var f=a(e.currentTarget).val(),g=a(e.currentTarget).data("last-value");g!==f&&r(b,c,f,d),a(e.currentTarget).data("last-value",f)}))};return{enhance:function(f,h,j,k,l,m,n,o){var p={selector:f,tags:!1,ajax:!1,placeholder:k,caseSensitive:!1,showSuggestions:!0,noSelectionString:n},q="autocomplete-setup-"+f;M.util.js_pending(q),"undefined"!=typeof h&&(p.tags=h),"undefined"!=typeof j&&(p.ajax=j),"undefined"!=typeof l&&(p.caseSensitive=l),"undefined"!=typeof m&&(p.showSuggestions=m),"undefined"==typeof n&&c.get_string("noselection","form").done(function(a){p.noSelectionString=a}).fail(e.exception);var r=a(f);if(!r)return b.debug("Selector not found: "+f),M.util.js_complete(q),!1;r.css("visibility","hidden").attr("aria-hidden",!0);var s={selectId:r.attr("id"),inputId:"form_autocomplete_input-"+g,suggestionsId:"form_autocomplete_suggestions-"+g,selectionId:"form_autocomplete_selection-"+g,downArrowId:"form_autocomplete_downarrow-"+g};g++,p.multiple=r.attr("multiple"),"undefined"!=typeof o?p.closeSuggestionsOnSelect=o:p.closeSuggestionsOnSelect=!p.multiple;var t=a("[for="+s.selectId+"]"),u=[];r.children("option").each(function(b,c){u[b]={label:c.innerHTML,value:a(c).attr("value")}});var w=a.extend({},p,s);w.options=u,w.items=[];var x="",y=d.render("core/form_autocomplete_input",w).then(function(a,b){return x+=b,a}),z=d.render("core/form_autocomplete_suggestions",w).then(function(a,b){return x+=b,a}),A=d.render("core/form_autocomplete_selection",w).then(function(a,b){return x+=b,a});return a.when(y,z,A).then(function(b,c,e){r.hide(),r.after(c),r.after(b),r.after(e),d.runTemplateJS(x),t.attr("for",s.inputId),v(p,s,r);var f=a(document.getElementById(s.suggestionsId));return f.hide().attr("aria-hidden",!0),i(p,s,r),M.util.js_complete(q),!0}).fail(function(a){M.util.js_complete(q),e.exception(a)})}}});