jQuery(document).ready((function(e){"use strict";function t(){var t=e('.mf-searchable-select[data-name="mf_post_submission_author"]');if(t.length){var s=t.find(".mf-author-search-input"),a=t.find('input[name="mf_post_submission_author"]'),o=t.find(".mf-select-dropdown"),n=o.find(".mf-select-loading"),i=o.find(".mf-select-no-results"),l=o.find(".mf-select-hint"),f=null,d=!1,c=0,r=null;(a.val()||s.val())&&(l.hide(),t.addClass("has-value"),a.val()&&s.val()&&(r={id:a.val(),text:s.val()})),s.on("click",(function(e){e.stopPropagation(),d||u()})),t.find(".mf-select-clear").on("click",(function(e){e.stopPropagation(),a.val(""),s.val(""),o.find(".mf-select-option").removeClass("mf-selected"),t.removeClass("has-value"),r=null,o.find(".mf-select-option.mf-dynamic").remove(),i.hide(),l.show(),c=0,m(),s.focus()})),s.on("input",(function(){var m=e(this).val().trim(),h=m.length,p=h<c;if(f&&clearTimeout(f),d||u(),!m)return t.removeClass("has-value"),a.val(""),o.find(".mf-select-option.mf-dynamic").remove(),l.show(),i.hide(),void(c=h);if(m.length<3){if(o.find(".mf-select-option.mf-dynamic").remove(),i.hide(),r){l.hide();var x=e('<li class="mf-select-option mf-dynamic mf-selected" data-value="'+r.id+'" data-text="'+v(r.text)+'">'+v(r.text)+"</li>");n.before(x)}else l.show();c=h}else{if(r||a.val()){var _=r?r.id:a.val(),g=r?r.text:s.val();if(g&&(-1!==g.indexOf(m)||-1!==m.indexOf(g)||p)){o.find(".mf-select-option.mf-dynamic").remove(),i.hide(),l.hide();x=e('<li class="mf-select-option mf-dynamic mf-selected" data-value="'+_+'" data-text="'+v(g)+'">'+v(g)+"</li>");return n.before(x),void(c=h)}}c=h,f=setTimeout((function(){!function(t){if(t.length<3)return o.find(".mf-select-option.mf-dynamic").remove(),i.hide(),void l.show();i.hide(),l.hide(),e.ajax({url:mf_author_search.resturl+"xs/post/search-users",type:"GET",data:{search:t},beforeSend:function(e){e.setRequestHeader("X-WP-Nonce",mf_author_search.nonce)},success:function(t){if(o.find(".mf-select-option.mf-dynamic").remove(),t.users&&t.users.length>0){i.hide();var s=a.val();t.users.forEach((function(t){var a=t.id==s?" mf-selected":"",o=e('<li class="mf-select-option mf-dynamic'+a+'" data-value="'+t.id+'" data-text="'+v(t.text)+'">'+v(t.text)+"</li>");n.before(o)}))}else i.show()},error:function(){l.show(),console.error("Failed to search users")}})}(m)}),300)}})),s.on("focus",(function(){u()})),o.on("click",".mf-select-option",(function(t){t.stopPropagation();var s=e(this);h(s.data("value"),s.data("text"))})),e(document).on("click",(function(t){e(t.target).closest(".mf-searchable-select").length||m()})),s.on("keydown",(function(e){var t=o.find(".mf-select-option:visible"),s=o.find(".mf-select-option.mf-focused"),a=t.index(s);switch(e.keyCode){case 40:e.preventDefault(),d||u(),a<t.length-1&&(t.removeClass("mf-focused"),t.eq(a+1).addClass("mf-focused"),p(t.eq(a+1)));break;case 38:e.preventDefault(),a>0&&(t.removeClass("mf-focused"),t.eq(a-1).addClass("mf-focused"),p(t.eq(a-1)));break;case 13:if(e.preventDefault(),s.length)h(s.data("value"),s.data("text"));break;case 27:m()}})),t.data("setValue",(function(t){if(t){var s=o.find('.mf-select-option[data-value="'+t+'"]');s.length?h(t,s.data("text")):e.ajax({url:mf_author_search.resturl+"xs/post/search-users",type:"GET",data:{search:"",per_page:1,user_id:t},beforeSend:function(e){e.setRequestHeader("X-WP-Nonce",mf_author_search.nonce)},success:function(s){if(s.users&&s.users.length>0){var a=s.users.find((function(e){return e.id==t}));if(a){var o=e('<li class="mf-select-option mf-dynamic mf-selected" data-value="'+a.id+'" data-text="'+v(a.text)+'">'+v(a.text)+"</li>");n.before(o),h(a.id,a.text)}}}})}}))}function u(){o.addClass("mf-select-open"),t.addClass("mf-dropdown-open"),d=!0}function m(){o.removeClass("mf-select-open"),t.removeClass("mf-dropdown-open"),d=!1}function h(e,n){a.val(e),s.val(n),m(),r=e?{id:e,text:n}:null,o.find(".mf-select-option").removeClass("mf-selected"),o.find('.mf-select-option[data-value="'+e+'"]').addClass("mf-selected"),e?(t.addClass("has-value"),l.hide()):(t.removeClass("has-value"),l.show())}function v(e){var t=document.createElement("div");return t.appendChild(document.createTextNode(e)),t.innerHTML}function p(e){if(e.length){var t=e.position().top,s=e.outerHeight(),a=o.height(),n=o.scrollTop();t<0?o.scrollTop(n+t):t+s>a&&o.scrollTop(n+t+s-a)}}}t(),e(document).on("mf-form-settings-loaded",(function(){t()})),e(document).ajaxComplete((function(t,s,a){if(a.url&&-1!==a.url.indexOf("xs/post/settings"))try{var o=JSON.parse(s.responseText);if(o.fields_settings&&o.fields_settings.mf_post_submission_author){var n=e('.mf-searchable-select[data-name="mf_post_submission_author"]').data("setValue");n&&n(o.fields_settings.mf_post_submission_author)}}catch(i){}}))}));