!function(){"use strict";!function(e){const t=window.searchwpForms?.i18n||{},o=(e,o)=>t[e]||o,i={forms:{},getDefaultFormState:()=>({state:"idle",recognition:null,timeoutId:null,error:null,lastTranscript:"",speechStarted:!1,intentionalStop:!1,tooltipClickHandler:null,tooltipTimerId:null}),ensureFormEntry(e){return this.forms[e]||(this.forms[e]=this.getDefaultFormState()),this.forms[e]},getState(e){return this.forms[e]?this.forms[e].state:"idle"},getActiveFormId(){for(const e in this.forms)if("listening"===this.forms[e].state||"processing"===this.forms[e].state)return e;return null},hasSpeechStarted(e){return!!this.forms[e]&&this.forms[e].speechStarted},setSpeechStarted(e,t){this.ensureFormEntry(e).speechStarted=t},setState(e,t,o={}){const i=this.ensureFormEntry(e);i.state=t,void 0!==o.recognition&&(i.recognition=o.recognition),void 0!==o.error&&(i.error=o.error),void 0!==o.lastTranscript&&(i.lastTranscript=o.lastTranscript),this.updateButtonUI(e)},getRecognition(e){return this.forms[e]?this.forms[e].recognition:null},setRecognition(e,t){this.ensureFormEntry(e).recognition=t},clearTimeout(e){const t=this.forms[e];t&&t.timeoutId&&(clearTimeout(t.timeoutId),t.timeoutId=null)},stopRecognition(e,t=!0){const o=this.forms[e];if(o&&(o.intentionalStop=t,o.recognition))try{const e=o.recognition.abort||o.recognition.stop;"function"==typeof e&&e.call(o.recognition)}catch(e){}},resetToIdle(e){this.stopRecognition(e,!0),this.setSpeechStarted(e,!1),this.setState(e,"idle",{error:null})},setTimeout(e,t){this.ensureFormEntry(e).timeoutId=t},resetTimeout(e,t){this.clearTimeout(e);const o=setTimeout(()=>{this.stopRecognition(e,!0),this.clearTimeout(e)},t);this.setTimeout(e,o)},cleanup(e){this.clearTimeout(e),this.hideTooltip(e),this.stopRecognition(e,!0);const t=this.forms[e];t&&(t.recognition=null),this.setState(e,"idle",{error:null})},updateButtonUI(t){const i=e("#"+t).find(".swp-voice-search-button");if(!i.length)return;const r=this.getState(t);i.removeClass("swp-voice-listening swp-voice-processing").attr("aria-label",i.data("aria-label-idle")||o("voiceSearch","Voice Search")),"listening"===r?i.addClass("swp-voice-listening").attr("aria-label",o("listening","Listening…")):"processing"===r&&i.addClass("swp-voice-processing").attr("aria-label",o("processing","Processing…"))},getTooltipMessage:e=>({"no-speech":o("noSpeech","No speech detected. Please try speaking again."),"audio-capture":o("audioCapture","Microphone not found or unavailable."),"not-allowed":o("notAllowed","Microphone permission denied. Please allow access."),network:o("network","Network error. Please check your connection."),aborted:o("aborted","Voice search cancelled."),"service-not-allowed":o("serviceNotAllowed","Speech recognition service unavailable."),"empty-transcript":o("noSpeech","No speech detected. Please try speaking again.")}[e]||o("defaultError","Voice search error. Please try again.")),createTooltipElement(t){const o=e("#"+t);let i=o.find(".swp-voice-search-tooltip");return i.length||(i=e('<div class="swp-voice-search-tooltip" role="alert" aria-live="polite"></div>'),o.find(".searchwp-form-input-container").append(i),i.on("click",()=>{this.hideTooltip(t)})),i},showTooltip(t,o,i="error"){const r=this.createTooltipElement(t),s=e("#"+t),n=this.forms[t];n&&n.tooltipTimerId&&(clearTimeout(n.tooltipTimerId),n.tooltipTimerId=null);const c=s.find(".searchwp-form-input-container");if(c.length){const e=50,t=c[0].getBoundingClientRect().top<e+16;r.toggleClass("swp-voice-search-tooltip--below",t)}r.text(o).removeClass("swp-voice-search-tooltip--error swp-voice-search-tooltip--warning swp-voice-search-tooltip--info").addClass("swp-voice-search-tooltip--"+i).addClass("swp-voice-search-tooltip--visible");const a=e("#"+t).find(".swp-voice-search-button"),l="swp-voice-tooltip-"+t;r.attr("id",l),a.attr("aria-describedby",l),n&&(n.tooltipTimerId=setTimeout(()=>{this.hideTooltip(t)},4e3),n.tooltipClickHandler||(n.tooltipClickHandler=o=>{e(o.target).closest(".swp-voice-search-tooltip").length||this.hideTooltip(t)},setTimeout(()=>{e(document).on("click.swpVoiceTooltip-"+t,n.tooltipClickHandler)},0)))},hideTooltip(t){const o=e("#"+t),i=o.find(".swp-voice-search-tooltip"),r=this.forms[t];r&&r.tooltipTimerId&&(clearTimeout(r.tooltipTimerId),r.tooltipTimerId=null),r&&r.tooltipClickHandler&&(e(document).off("click.swpVoiceTooltip-"+t,r.tooltipClickHandler),r.tooltipClickHandler=null),i.length&&i.removeClass("swp-voice-search-tooltip--visible");o.find(".swp-voice-search-button").removeAttr("aria-describedby")}},r={init:()=>{e(r.ready)},ready:()=>{r.events()},events:()=>{e(".swp-toggle-checkbox").removeAttr("disabled"),e(".swp-toggle-checkbox").on("change",function(){const t=e(this).closest("form"),o=t.find(".searchwp-form-advanced-filters"),i=o.find("select"),r=t.find(".swp-toggle-switch"),s=e(this).is(":checked");r.attr("aria-checked",s),s?(o.css("display","flex"),i.prop("disabled",!1)):(o.hide(),i.prop("disabled",!0))}),r.initVoiceSearch()},initVoiceSearch:()=>{const t=e(".swp-voice-search-button");if(!t.length)return;if(!("webkitSpeechRecognition"in window)&&!("SpeechRecognition"in window))return;const o=window.SpeechRecognition||window.webkitSpeechRecognition;!(!window.opr||!window.opr.addons)||navigator.userAgent.indexOf("OPR")>-1||"Opera Software ASA"===navigator.vendor||(t.addClass("swp-voice-search-button--supported"),t.each(function(){const t=e(this);t.closest("form").attr("id")&&t.data("aria-label-idle",t.attr("aria-label"))}),t.on("click",function(t){t.preventDefault();const r=e(this).closest("form"),s=r.attr("id");if(!s)return;const n=i.getActiveFormId();if(n&&n!==s&&i.resetToIdle(n),"listening"===i.getState(s))return void i.resetToIdle(s);i.setSpeechStarted(s,!1),i.cleanup(s);const c=r.find(".swp-input--search"),a=1===Number(r.data("voiceSearchAutoSubmit")),l=1===Number(r.data("voiceSearchInterimResults")),p=parseInt(r.data("voiceSearchTimeout"),10)||5e3,d=new o;d.lang=document.documentElement.lang||"en-US",d.interimResults=l,d.maxAlternatives=1,d.continuous=!0,i.setRecognition(s,d),i.setState(s,"listening",{recognition:d}),d.onresult=e=>{let t="",o="";for(let i=0;i<e.results.length;i++){const r=e.results[i];r.isFinal?t+=r[0].transcript:o+=r[0].transcript}const n=(t+o).trim();if(e.results[e.results.length-1].isFinal){if(!n.length)return i.showTooltip(s,i.getTooltipMessage("empty-transcript"),"error"),void i.resetToIdle(s);i.setState(s,"processing",{lastTranscript:n}),c.val(n),c.trigger("input"),a&&r.submit(),setTimeout(()=>{i.setSpeechStarted(s,!1),i.cleanup(s)},100)}else i.hasSpeechStarted(s)||i.setSpeechStarted(s,!0),i.resetTimeout(s,p),l&&c.val(n)},d.onerror=e=>{const t=i.forms[s];if("aborted"===e.error&&t&&t.intentionalStop)return void(t.intentionalStop=!1);const o=i.getState(s);"processing"!==o&&"error"!==o&&(i.clearTimeout(s),i.showTooltip(s,i.getTooltipMessage(e.error),"error"),i.resetToIdle(s))},d.onend=()=>{i.clearTimeout(s),i.resetToIdle(s)},d.start();const h=setTimeout(()=>{i.hasSpeechStarted(s)||(i.showTooltip(s,i.getTooltipMessage("no-speech"),"error"),i.clearTimeout(s),i.resetToIdle(s))},p);i.setTimeout(s,h)}))}};r.init(),window.searchwp=window.searchwp||{},window.searchwp.searchForms=r,window.searchwp.voiceSearchState=i}(jQuery)}();