require(["jquery","handlebars","Magento_Catalog/js/price-utils","mage/translate","Codilar_ProductListing/js/product_label","mage/url"],function(e,n,t,o,s,a){function i(){require(["jquery"],function(e){e('label[data-role="minisearch-label"]').click(function(n){var t=!0,o=e(n.target);o.toggleClass("button-clicked"),o.hasClass("active")&&(t=!1),o.toggleClass("active"),e("form.unbxd-search-form-js").toggleClass("active"),t&&e("form.unbxd-search-form-js #search").focus()}),e("form.unbxd-search-form-js #search").focusout(function(n){setTimeout(function(){e('label[data-role="minisearch-label"]').hasClass("button-clicked")||(e("form.unbxd-search-form-js").removeClass("active"),e('label[data-role="minisearch-label"]').removeClass("active button-clicked"),e(".unbxd-as-wrapper").hide())},1e3)})})}function r(){if(!h){var n=e("#search_bar_container .unbxd-as-wrapper.unbxd-as-overall-autosuggest");if(0!==n.length){var t=[];n.find("[data-product-sku], [data-sku]").each(function(){var n=e(this).data("product-sku")||e(this).data("sku");n&&-1===t.indexOf(n)&&t.push(n)});var o=e("#search"),i=o.val().trim(),r=JSON.stringify(t.sort()),l=JSON.stringify(g.sort());if(!(t.length>0&&r===l&&i===d)&&t.length>0)try{h=!0,g=t.slice(),d=i,s({productData:t,ajaxActionUrl:a.build("/product_listing/productlabel/index"),isAutosuggest:!0}),setTimeout(function(){h=!1},2e3)}catch(e){console.error("[Autosuggest] Error fetching product labels:",e),h=!1}}}}function l(){u&&clearTimeout(u),u=setTimeout(function(){r()},300)}function c(){"unbxdAutoSuggestFunction"in window?(unbxdAutoSuggestFunction(jQuery,Handlebars),"unbxdMagentoConfig"in window&&unbxdMagentoConfig.autoSuggest.customTemplate?e(unbxdMagentoConfig.autoSuggest.searchInputSelector).unbxdautocomplete(magento_unbxd_asconfig):jQuery(".unbxd-search-form-js input#search").unbxdautocomplete(magento_unbxd_asconfig)):this.counter>5?console.error("UnbxdAutosuggest SDK not loaded"):setTimeout(c.bind({counter:f+1}),200)}e(document).ready(function(){function o(){return e("#search_bar_container .unbxd-as-wrapper.unbxd-as-overall-autosuggest")}function s(e,n){n=n||0,b=o(),b.length>0?e():n<20&&setTimeout(function(){s(e,n+1)},200)}function a(){var n=o();0!==n.length&&(n.empty(),e.get("/popularsearch/index/template").done(function(e){n.html(e).show(),n.addClass("showing-placeholder-searches"),g()}).fail(function(t,o,s){var a='<div class="placeholder-search-container">'+e.mage.__("Template is not loading")+"</div>";n.html(a).show(),n.addClass("showing-placeholder-searches"),g()}))}function r(){var e=o();0!==e.length&&(e.find(".placeholder-search-container").remove(),e.removeClass("showing-placeholder-searches"),e.children().length||(e.hide(),d()))}function u(){clearTimeout(m),m=setTimeout(function(){var n=o(),t=v.val().trim();if(t.length>=3&&n.is(":visible")){if(n.find(".browse-all-results-container").length>0)return;if(n.find(".no-result-message").length>0)return;if(n.children(":not(.cel-ac-icon-cancel, .no-result-message, .browse-all-results-container)").length>0){var s='<div class="browse-all-results-container"><a href="/catalogsearch/result/?q='+encodeURIComponent(t)+'" class="browse-all-btn browse-all-results"><span class="browse-all-text">'+e.mage.__("Browse All Products")+"</span></a></div>";n.append(s)}}},100)}function g(){var n=o();if(0!==n.length&&n.is(":visible")&&0===n.find(".autosuggest-close-btn").length){var t=e('<button class="autosuggest-close-btn" type="button" aria-label="Close autosuggest"><span class="close-icon">×</span></button>');n.prepend(t),console.log("[Autosuggest] Close button added to container")}}function d(){var e=o();0!==e.length&&(e.find(".autosuggest-close-btn").remove(),e.find(".cel-ac-icon-cancel").trigger("click"),console.log("[Autosuggest] Close button removed from container"))}function h(){var n=o();if(0!==n.length){var t=v.val();if(!n.hasClass("showing-popular-searches")&&!n.hasClass("showing-browse-all-search"))if(n.find(".no-result-message").remove(),t.length>=3){0===n.find(".cel-ac-icon-cancel").length&&e('<span class="cel-ac-icon-cancel"></span>').appendTo(n);var s=n.children(":not(.cel-ac-icon-cancel, .browse-all-results-container, .autosuggest-close-btn)").length;0===s?(n.show(),e('<div class="no-result-message">'+e.mage.__("No results found")+"</div>").appendTo(n)):setTimeout(u,300),g()}else n.find(".cel-ac-icon-cancel, .no-result-message, .browse-all-results-container").remove()}}function f(){if(0===window.location.pathname.indexOf("/catalogsearch/result"))return void v.val("");setTimeout(function(){var e=v.val().trim();0===e.length||e.length>=3&&setTimeout(function(){h();var e=o(),n=e.is(":visible"),t=e.children(":not(.cel-ac-icon-cancel, .browse-all-results-container)").length;n&&t>0&&(u(),l())},500)},300)}if((/iPad|iPhone|iPod/.test(navigator.userAgent)||"MacIntel"===navigator.platform&&navigator.maxTouchPoints>1)&&e("body").addClass("IOS-device"),"magento_unbxd_asconfig"in window){n&&n.registerHelper&&n.registerHelper("formatPrice",function(e){if(!e||isNaN(e))return"";var n=e;return window.magento_unbxd_currencyConfig&&window.magento_unbxd_currencyConfig.exchangeRate&&(n=e*window.magento_unbxd_currencyConfig.exchangeRate),void 0!==t&&window.magento_unbxd_priceFormat?t.formatPrice(n,window.magento_unbxd_priceFormat):n.toFixed(2)}),c(),i();var b,m,v=e("#search");v.on("focus",function(){setTimeout(function(){0===v.val().trim().length?a():o().is(":visible")&&g()},100)}),v.on("input",function(){var e=v.val().trim(),n=o();0===e.length?(n.removeClass("showing-browse-all-search"),n.find(".browse-all-results-container").remove(),setTimeout(a,100)):e.length>0&&e.length<3?(r(),n.find(".browse-all-results-container").remove()):(r(),n.removeClass("showing-browse-all-search"),setTimeout(function(){h(),setTimeout(u,800),setTimeout(l,1200)},1e3))}),v.on("click",function(){setTimeout(function(){var e=v.val().trim();if(0===e.length)a();else if(e.length>=3)setTimeout(function(){h(),setTimeout(u,500),l()},300);else{var n=o();n.is(":visible")&&g()}},150)}),e(document).on("click",".browse-all-btn",function(e){e.preventDefault();var n=v.val().trim(),t="/catalogsearch/result/";n.length>0&&(t+="?q="+encodeURIComponent(n)),window.location.href=t});var p=new MutationObserver(function(e){var n=!1,t=!1,s=o();e.forEach(function(e){"childList"!==e.type&&"attributes"!==e.type||s.is(":visible")&&s.children().length>0&&g(),"childList"===e.type&&s.is(":visible")&&v.val().trim().length>=3&&!s.hasClass("showing-placeholder-searches")&&!s.hasClass("showing-browse-all-search")&&[].slice.call(e.addedNodes).some(function(e){return 1===e.nodeType&&!e.classList.contains("browse-all-results-container")&&!e.classList.contains("cel-ac-icon-cancel")&&!e.classList.contains("autosuggest-close-btn")})&&(n=!0,t=!0)}),n&&u(),t&&l()}),w=o();w.length>0?p.observe(w[0],{childList:!0,subtree:!0,attributes:!0,attributeFilter:["style","class"]}):console.warn("[Autosuggest] Cannot set up MutationObserver - container not found"),s(function(){f()}),e(document).on("click",function(n){e(n.target).closest("#search_bar_container").length||e(n.target).closest(".search-item").length||r()}),e(document).on("click","#search_bar_container",function(e){b.hasClass("showing-placeholder-searches")&&e.stopPropagation()}),e(document).on("click",".cel-ac-icon-cancel ",function(){v.val(""),e("#search_bar_container .unbxd-as-wrapper.unbxd-as-overall-autosuggest").hide(),setTimeout(a,100)}),e(document).on("click",".autosuggest-close-btn",function(e){e.preventDefault(),e.stopPropagation(),console.log("[Autosuggest] Close button clicked"),o().hide(),d(),v.blur()})}else console.error("[Autosuggest] ERROR: Unbxd Autosuggest config not found in window")});var u=null,g=[],d="",h=!1,f=0});
//# sourceMappingURL=autosuggest.js.map