export { g as getAssetPath, r as render, s as setAssetPath, a as setNonce, b as setPlatformOptions } from './index2.js?scv=0.14.0';
import { n as nanoid } from './searchcraft-ad2.js?scv=0.14.0';
import { D as DEFAULT_CORE_INSTANCE_ID, r as registry } from './CoreInstanceRegistry.js?scv=0.14.0';
import { v as version, n as name } from './searchcraft-popover-footer2.js?scv=0.14.0';
export { SearchcraftAd, defineCustomElement as defineCustomElementSearchcraftAd } from './searchcraft-ad.js?scv=0.14.0';
export { SearchcraftButton, defineCustomElement as defineCustomElementSearchcraftButton } from './searchcraft-button.js?scv=0.14.0';
export { SearchcraftErrorMessage, defineCustomElement as defineCustomElementSearchcraftErrorMessage } from './searchcraft-error-message.js?scv=0.14.0';
export { SearchcraftFacetList, defineCustomElement as defineCustomElementSearchcraftFacetList } from './searchcraft-facet-list.js?scv=0.14.0';
export { SearchcraftFilterPanel, defineCustomElement as defineCustomElementSearchcraftFilterPanel } from './searchcraft-filter-panel.js?scv=0.14.0';
export { SearchcraftInputForm, defineCustomElement as defineCustomElementSearchcraftInputForm } from './searchcraft-input-form.js?scv=0.14.0';
export { SearchcraftInputLabel, defineCustomElement as defineCustomElementSearchcraftInputLabel } from './searchcraft-input-label.js?scv=0.14.0';
export { SearchcraftLoading, defineCustomElement as defineCustomElementSearchcraftLoading } from './searchcraft-loading.js?scv=0.14.0';
export { SearchcraftPagination, defineCustomElement as defineCustomElementSearchcraftPagination } from './searchcraft-pagination.js?scv=0.14.0';
export { SearchcraftPopoverButton, defineCustomElement as defineCustomElementSearchcraftPopoverButton } from './searchcraft-popover-button.js?scv=0.14.0';
export { SearchcraftPopoverFooter, defineCustomElement as defineCustomElementSearchcraftPopoverFooter } from './searchcraft-popover-footer.js?scv=0.14.0';
export { SearchcraftPopoverForm, defineCustomElement as defineCustomElementSearchcraftPopoverForm } from './searchcraft-popover-form.js?scv=0.14.0';
export { SearchcraftPopoverListItem, defineCustomElement as defineCustomElementSearchcraftPopoverListItem } from './searchcraft-popover-list-item.js?scv=0.14.0';
export { SearchcraftPopoverListView, defineCustomElement as defineCustomElementSearchcraftPopoverListView } from './searchcraft-popover-list-view.js?scv=0.14.0';
export { SearchcraftResultsInfo, defineCustomElement as defineCustomElementSearchcraftResultsInfo } from './searchcraft-results-info.js?scv=0.14.0';
export { SearchcraftSearchResult, defineCustomElement as defineCustomElementSearchcraftSearchResult } from './searchcraft-search-result.js?scv=0.14.0';
export { SearchcraftSearchResults, defineCustomElement as defineCustomElementSearchcraftSearchResults } from './searchcraft-search-results.js?scv=0.14.0';
export { SearchcraftSearchResultsPerPage, defineCustomElement as defineCustomElementSearchcraftSearchResultsPerPage } from './searchcraft-search-results-per-page.js?scv=0.14.0';
export { SearchcraftSelect, defineCustomElement as defineCustomElementSearchcraftSelect } from './searchcraft-select.js?scv=0.14.0';
export { SearchcraftSlider, defineCustomElement as defineCustomElementSearchcraftSlider } from './searchcraft-slider.js?scv=0.14.0';
export { SearchcraftSummaryBox, defineCustomElement as defineCustomElementSearchcraftSummaryBox } from './searchcraft-summary-box.js?scv=0.14.0';
export { SearchcraftTheme, defineCustomElement as defineCustomElementSearchcraftTheme } from './searchcraft-theme.js?scv=0.14.0';
export { SearchcraftToggleButton, defineCustomElement as defineCustomElementSearchcraftToggleButton } from './searchcraft-toggle-button.js?scv=0.14.0';

var LogLevel;
(function (LogLevel) {
    LogLevel[LogLevel["DEBUG"] = 0] = "DEBUG";
    LogLevel[LogLevel["INFO"] = 1] = "INFO";
    LogLevel[LogLevel["WARN"] = 2] = "WARN";
    LogLevel[LogLevel["ERROR"] = 3] = "ERROR";
    LogLevel[LogLevel["NONE"] = 4] = "NONE";
})(LogLevel || (LogLevel = {}));
class SearchcraftLogger {
    logLevel;
    logFormatter;
    constructor(options) {
        this.logLevel = options.logLevel || LogLevel.INFO;
        this.logFormatter = options.logFormatter || this.defaultFormatter;
    }
    defaultFormatter(level, message) {
        const levelStr = LogLevel[level];
        return `[${levelStr}] ${new Date().toISOString()}: ${message}`;
    }
    debug(message) {
        if (this.logLevel <= LogLevel.DEBUG) {
            console.log(this.logFormatter(LogLevel.DEBUG, message));
        }
    }
    info(message) {
        if (this.logLevel <= LogLevel.INFO) {
            console.info(this.logFormatter(LogLevel.INFO, message));
        }
    }
    warn(message) {
        if (this.logLevel <= LogLevel.WARN) {
            console.warn(this.logFormatter(LogLevel.WARN, message));
        }
    }
    error(message) {
        if (this.logLevel <= LogLevel.ERROR) {
            console.error(this.logFormatter(LogLevel.ERROR, message));
        }
    }
    log(level, message) {
        switch (level) {
            case LogLevel.DEBUG:
                this.debug(message);
                break;
            case LogLevel.INFO:
                this.info(message);
                break;
            case LogLevel.WARN:
                this.warn(message);
                break;
            case LogLevel.ERROR:
                this.error(message);
                break;
        }
    }
}
const Logger = new SearchcraftLogger({ logLevel: LogLevel.NONE });

function e(e,r,n,t){return new(n||(n=Promise))((function(o,a){function i(e){try{c(t.next(e));}catch(e){a(e);}}function u(e){try{c(t.throw(e));}catch(e){a(e);}}function c(e){var r;e.done?o(e.value):(r=e.value,r instanceof n?r:new n((function(e){e(r);}))).then(i,u);}c((t=t.apply(e,[])).next());}))}function r(e,r){var n,t,o,a,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return a={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function u(u){return function(c){return function(u){if(n)throw new TypeError("Generator is already executing.");for(;a&&(a=0,u[0]&&(i=0)),i;)try{if(n=1,t&&(o=2&u[0]?t.return:u[0]?t.throw||((o=t.return)&&o.call(t),0):t.next)&&!(o=o.call(t,u[1])).done)return o;switch(t=0,o&&(u=[2&u[0],o.value]),u[0]){case 0:case 1:o=u;break;case 4:return i.label++,{value:u[1],done:false};case 5:i.label++,t=u[1],u=[0];continue;case 7:u=i.ops.pop(),i.trys.pop();continue;default:if(!(o=i.trys,(o=o.length>0&&o[o.length-1])||6!==u[0]&&2!==u[0])){i=0;continue}if(3===u[0]&&(!o||u[1]>o[0]&&u[1]<o[3])){i.label=u[1];break}if(6===u[0]&&i.label<o[1]){i.label=o[1],o=u;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(u);break}o[2]&&i.ops.pop(),i.trys.pop();continue}u=r.call(e,i);}catch(e){u=[6,e],t=0;}finally{n=o=0;}if(5&u[0])throw u[1];return {value:u[0]?u[1]:void 0,done:true}}([u,c])}}}"function"==typeof SuppressedError&&SuppressedError;var n={exclude:[]};var o={},a={timeout:"true"},i=function(e,r){"undefined"!=typeof window&&(o[e]=r);},u=function(){return Object.fromEntries(Object.entries(o).filter((function(e){var r,t=e[0];return !(null===(r=null==n?void 0:n.exclude)||void 0===r?void 0:r.includes(t))})).map((function(e){return [e[0],(0, e[1])()]})))};function c(e){return e^=e>>>16,e=Math.imul(e,2246822507),e^=e>>>13,e=Math.imul(e,3266489909),(e^=e>>>16)>>>0}var s=new Uint32Array([597399067,2869860233,951274213,2716044179]);function l(e,r){return e<<r|e>>>32-r}function f(e,r){var n;if(void 0===r&&(r=0),r=r?0|r:0,"string"==typeof e&&(n=e,e=(new TextEncoder).encode(n).buffer),!(e instanceof ArrayBuffer))throw new TypeError("Expected key to be ArrayBuffer or string");var t=new Uint32Array([r,r,r,r]);!function(e,r){for(var n=e.byteLength/16|0,t=new Uint32Array(e,0,4*n),o=0;o<n;o++){var a=t.subarray(4*o,4*(o+1));a[0]=Math.imul(a[0],s[0]),a[0]=l(a[0],15),a[0]=Math.imul(a[0],s[1]),r[0]=r[0]^a[0],r[0]=l(r[0],19),r[0]=r[0]+r[1],r[0]=Math.imul(r[0],5)+1444728091,a[1]=Math.imul(a[1],s[1]),a[1]=l(a[1],16),a[1]=Math.imul(a[1],s[2]),r[1]=r[1]^a[1],r[1]=l(r[1],17),r[1]=r[1]+r[2],r[1]=Math.imul(r[1],5)+197830471,a[2]=Math.imul(a[2],s[2]),a[2]=l(a[2],17),a[2]=Math.imul(a[2],s[3]),r[2]=r[2]^a[2],r[2]=l(r[2],15),r[2]=r[2]+r[3],r[2]=Math.imul(r[2],5)+2530024501,a[3]=Math.imul(a[3],s[3]),a[3]=l(a[3],18),a[3]=Math.imul(a[3],s[0]),r[3]=r[3]^a[3],r[3]=l(r[3],13),r[3]=r[3]+r[0],r[3]=Math.imul(r[3],5)+850148119;}}(e,t),function(e,r){var n=e.byteLength/16|0,t=e.byteLength%16,o=new Uint32Array(4),a=new Uint8Array(e,16*n,t);switch(t){case 15:o[3]=o[3]^a[14]<<16;case 14:o[3]=o[3]^a[13]<<8;case 13:o[3]=o[3]^a[12]<<0,o[3]=Math.imul(o[3],s[3]),o[3]=l(o[3],18),o[3]=Math.imul(o[3],s[0]),r[3]=r[3]^o[3];case 12:o[2]=o[2]^a[11]<<24;case 11:o[2]=o[2]^a[10]<<16;case 10:o[2]=o[2]^a[9]<<8;case 9:o[2]=o[2]^a[8]<<0,o[2]=Math.imul(o[2],s[2]),o[2]=l(o[2],17),o[2]=Math.imul(o[2],s[3]),r[2]=r[2]^o[2];case 8:o[1]=o[1]^a[7]<<24;case 7:o[1]=o[1]^a[6]<<16;case 6:o[1]=o[1]^a[5]<<8;case 5:o[1]=o[1]^a[4]<<0,o[1]=Math.imul(o[1],s[1]),o[1]=l(o[1],16),o[1]=Math.imul(o[1],s[2]),r[1]=r[1]^o[1];case 4:o[0]=o[0]^a[3]<<24;case 3:o[0]=o[0]^a[2]<<16;case 2:o[0]=o[0]^a[1]<<8;case 1:o[0]=o[0]^a[0]<<0,o[0]=Math.imul(o[0],s[0]),o[0]=l(o[0],15),o[0]=Math.imul(o[0],s[1]),r[0]=r[0]^o[0];}}(e,t),function(e,r){r[0]=r[0]^e.byteLength,r[1]=r[1]^e.byteLength,r[2]=r[2]^e.byteLength,r[3]=r[3]^e.byteLength,r[0]=r[0]+r[1]|0,r[0]=r[0]+r[2]|0,r[0]=r[0]+r[3]|0,r[1]=r[1]+r[0]|0,r[2]=r[2]+r[0]|0,r[3]=r[3]+r[0]|0,r[0]=c(r[0]),r[1]=c(r[1]),r[2]=c(r[2]),r[3]=c(r[3]),r[0]=r[0]+r[1]|0,r[0]=r[0]+r[2]|0,r[0]=r[0]+r[3]|0,r[1]=r[1]+r[0]|0,r[2]=r[2]+r[0]|0,r[3]=r[3]+r[0]|0;}(e,t);var o=new Uint8Array(t.buffer);return Array.from(o).map((function(e){return e.toString(16).padStart(2,"0")})).join("")}function d(e,r){return new Promise((function(n){setTimeout((function(){return n(r)}),e);}))}function m(e,r,n){return Promise.all(e.map((function(e){return Promise.race([e,d(r,n)])})))}function v(){return e(this,void 0,void 0,(function(){var e,t,o,i,c;return r(this,(function(r){switch(r.label){case 0:return r.trys.push([0,2,,3]),e=u(),t=Object.keys(e),[4,m(Object.values(e),(null==n?void 0:n.timeout)||1e3,a)];case 1:return o=r.sent(),i=o.filter((function(e){return void 0!==e})),c={},i.forEach((function(e,r){c[t[r]]=e;})),[2,p(c,n.exclude||[])];case 2:throw r.sent();case 3:return [2]}}))}))}function p(e,r){var n={},t=function(t){if(e.hasOwnProperty(t)){var o=e[t];if("object"!=typeof o||Array.isArray(o))r.includes(t)||(n[t]=o);else {var a=p(o,r.map((function(e){return e.startsWith(t+".")?e.slice(t.length+1):e})));Object.keys(a).length>0&&(n[t]=a);}}};for(var o in e)t(o);return n}function g(n){return e(this,void 0,void 0,(function(){var e,t;return r(this,(function(r){switch(r.label){case 0:return r.trys.push([0,2,,3]),[4,v()];case 1:return e=r.sent(),t=f(JSON.stringify(e)),[2,t.toString()];case 2:throw r.sent();case 3:return [2]}}))}))}function y(e){for(var r=0,n=0;n<e.length;++n)r+=Math.abs(e[n]);return r}function b(e,r,n){for(var t=[],o=0;o<e[0].data.length;o++){for(var a=[],i=0;i<e.length;i++)a.push(e[i].data[o]);t.push(S(a));}var u=new Uint8ClampedArray(t);return new ImageData(u,r,n)}function S(e){if(0===e.length)return 0;for(var r={},n=0,t=e;n<t.length;n++){r[a=t[n]]=(r[a]||0)+1;}var o=e[0];for(var a in r)r[a]>r[o]&&(o=parseInt(a,10));return o}function M(){if("undefined"==typeof navigator)return {name:"unknown",version:"unknown"};for(var e=navigator.userAgent,r={Edg:"Edge",OPR:"Opera"},n=0,t=[/(?<name>Edge|Edg)\/(?<version>\d+(?:\.\d+)?)/,/(?<name>(?:Chrome|Chromium|OPR|Opera|Vivaldi|Brave))\/(?<version>\d+(?:\.\d+)?)/,/(?<name>(?:Firefox|Waterfox|Iceweasel|IceCat))\/(?<version>\d+(?:\.\d+)?)/,/(?<name>Safari)\/(?<version>\d+(?:\.\d+)?)/,/(?<name>MSIE|Trident|IEMobile).+?(?<version>\d+(?:\.\d+)?)/,/(?<name>[A-Za-z]+)\/(?<version>\d+(?:\.\d+)?)/,/(?<name>SamsungBrowser)\/(?<version>\d+(?:\.\d+)?)/];n<t.length;n++){var o=t[n],a=e.match(o);if(a&&a.groups)return {name:r[a.groups.name]||a.groups.name,version:a.groups.version}}return {name:"unknown",version:"unknown"}}i("audio",(function(){return e(this,void 0,void 0,(function(){return r(this,(function(e){return [2,new Promise((function(e,r){try{var n=new(window.OfflineAudioContext||window.webkitOfflineAudioContext)(1,5e3,44100),t=n.createBufferSource(),o=n.createOscillator();o.frequency.value=1e3;var a,i=n.createDynamicsCompressor();i.threshold.value=-50,i.knee.value=40,i.ratio.value=12,i.attack.value=0,i.release.value=.2,o.connect(i),i.connect(n.destination),o.start(),n.oncomplete=function(r){a=r.renderedBuffer.getChannelData(0),e({sampleHash:y(a),oscillator:o.type,maxChannels:n.destination.maxChannelCount,channelCountMode:t.channelCountMode});},n.startRendering();}catch(e){console.error("Error creating audio fingerprint:",e),r(e);}}))]}))}))}));var E="SamsungBrowser"!==M().name?1:3,P=280,A=20;"Firefox"!=M().name&&i("canvas",(function(){return document.createElement("canvas").getContext("2d"),new Promise((function(e){var r=Array.from({length:E},(function(){return function(){var e=document.createElement("canvas"),r=e.getContext("2d");if(!r)return new ImageData(1,1);e.width=P,e.height=A;var n=r.createLinearGradient(0,0,e.width,e.height);n.addColorStop(0,"red"),n.addColorStop(1/6,"orange"),n.addColorStop(2/6,"yellow"),n.addColorStop(.5,"green"),n.addColorStop(4/6,"blue"),n.addColorStop(5/6,"indigo"),n.addColorStop(1,"violet"),r.fillStyle=n,r.fillRect(0,0,e.width,e.height);var t="Random Text WMwmil10Oo";r.font="23.123px Arial",r.fillStyle="black",r.fillText(t,-5,15),r.fillStyle="rgba(0, 0, 255, 0.5)",r.fillText(t,-3.3,17.7),r.beginPath(),r.moveTo(0,0),r.lineTo(2*e.width/7,e.height),r.strokeStyle="white",r.lineWidth=2,r.stroke();var o=r.getImageData(0,0,e.width,e.height);return o}()}));e({commonImageDataHash:f(b(r,P,A).data.toString()).toString()});}))}));var C,x=["Arial","Arial Black","Arial Narrow","Arial Rounded MT","Arimo","Archivo","Barlow","Bebas Neue","Bitter","Bookman","Calibri","Cabin","Candara","Century","Century Gothic","Comic Sans MS","Constantia","Courier","Courier New","Crimson Text","DM Mono","DM Sans","DM Serif Display","DM Serif Text","Dosis","Droid Sans","Exo","Fira Code","Fira Sans","Franklin Gothic Medium","Garamond","Geneva","Georgia","Gill Sans","Helvetica","Impact","Inconsolata","Indie Flower","Inter","Josefin Sans","Karla","Lato","Lexend","Lucida Bright","Lucida Console","Lucida Sans Unicode","Manrope","Merriweather","Merriweather Sans","Montserrat","Myriad","Noto Sans","Nunito","Nunito Sans","Open Sans","Optima","Orbitron","Oswald","Pacifico","Palatino","Perpetua","PT Sans","PT Serif","Poppins","Prompt","Public Sans","Quicksand","Rajdhani","Recursive","Roboto","Roboto Condensed","Rockwell","Rubik","Segoe Print","Segoe Script","Segoe UI","Sora","Source Sans Pro","Space Mono","Tahoma","Taviraj","Times","Times New Roman","Titillium Web","Trebuchet MS","Ubuntu","Varela Round","Verdana","Work Sans"],T=["monospace","sans-serif","serif"];function k(e,r){if(!e)throw new Error("Canvas context not supported");return e.font="72px ".concat(r),e.measureText("WwMmLli0Oo").width}function R(){var e,r=document.createElement("canvas"),n=null!==(e=r.getContext("webgl"))&&void 0!==e?e:r.getContext("experimental-webgl");if(n&&"getParameter"in n)try{var t=(n.getParameter(n.VENDOR)||"").toString(),o=(n.getParameter(n.RENDERER)||"").toString(),a={vendor:t,renderer:o,version:(n.getParameter(n.VERSION)||"").toString(),shadingLanguageVersion:(n.getParameter(n.SHADING_LANGUAGE_VERSION)||"").toString()};if(!o.length||!t.length){var i=n.getExtension("WEBGL_debug_renderer_info");if(i){var u=(n.getParameter(i.UNMASKED_VENDOR_WEBGL)||"").toString(),c=(n.getParameter(i.UNMASKED_RENDERER_WEBGL)||"").toString();u&&(a.vendorUnmasked=u),c&&(a.rendererUnmasked=c);}}return a}catch(e){}return "undefined"}function I(){var e=new Float32Array(1),r=new Uint8Array(e.buffer);return e[0]=1/0,e[0]=e[0]-e[0],r[3]}function O(e,r){var n={};return r.forEach((function(r){var t=function(e){if(0===e.length)return null;var r={};e.forEach((function(e){var n=String(e);r[n]=(r[n]||0)+1;}));var n=e[0],t=1;return Object.keys(r).forEach((function(e){r[e]>t&&(n=e,t=r[e]);})),n}(e.map((function(e){return r in e?e[r]:void 0})).filter((function(e){return void 0!==e})));t&&(n[r]=t);})),n}function L(){var e=[],r={"prefers-contrast":["high","more","low","less","forced","no-preference"],"any-hover":["hover","none"],"any-pointer":["none","coarse","fine"],pointer:["none","coarse","fine"],hover:["hover","none"],update:["fast","slow"],"inverted-colors":["inverted","none"],"prefers-reduced-motion":["reduce","no-preference"],"prefers-reduced-transparency":["reduce","no-preference"],scripting:["none","initial-only","enabled"],"forced-colors":["active","none"]};return Object.keys(r).forEach((function(n){r[n].forEach((function(r){matchMedia("(".concat(n,": ").concat(r,")")).matches&&e.push("".concat(n,": ").concat(r));}));})),e}function _(){if("https:"===window.location.protocol&&"function"==typeof window.ApplePaySession)try{for(var e=window.ApplePaySession.supportsVersion,r=15;r>0;r--)if(e(r))return r}catch(e){return 0}return 0}"Firefox"!=M().name&&i("fonts",(function(){var n=this;return new Promise((function(t,o){try{!function(n){var t;e(this,void 0,void 0,(function(){var e,o,a;return r(this,(function(r){switch(r.label){case 0:return document.body?[3,2]:[4,(i=50,new Promise((function(e){return setTimeout(e,i,u)})))];case 1:return r.sent(),[3,0];case 2:if((e=document.createElement("iframe")).setAttribute("frameBorder","0"),(o=e.style).setProperty("position","fixed"),o.setProperty("display","block","important"),o.setProperty("visibility","visible"),o.setProperty("border","0"),o.setProperty("opacity","0"),e.src="about:blank",document.body.appendChild(e),!(a=e.contentDocument||(null===(t=e.contentWindow)||void 0===t?void 0:t.document)))throw new Error("Iframe document is not accessible");return n({iframe:a}),setTimeout((function(){document.body.removeChild(e);}),0),[2]}var i,u;}))}));}((function(o){var a=o.iframe;return e(n,void 0,void 0,(function(){var e,n,o,i;return r(this,(function(r){return e=a.createElement("canvas"),n=e.getContext("2d"),o=T.map((function(e){return k(n,e)})),i={},x.forEach((function(e){var r=k(n,e);o.includes(r)||(i[e]=r);})),t(i),[2]}))}))}));}catch(e){o({error:"unsupported"});}}))})),i("hardware",(function(){return new Promise((function(e,r){var n=void 0!==navigator.deviceMemory?navigator.deviceMemory:0,t=window.performance&&window.performance.memory?window.performance.memory:0;e({videocard:R(),architecture:I(),deviceMemory:n.toString()||"undefined",jsHeapSizeLimit:t.jsHeapSizeLimit||0});}))})),i("locales",(function(){return new Promise((function(e){e({languages:navigator.language,timezone:Intl.DateTimeFormat().resolvedOptions().timeZone});}))})),i("permissions",(function(){return e(this,void 0,void 0,(function(){var t;return r(this,(function(o){return C=(null==n?void 0:n.permissions_to_check)||["accelerometer","accessibility","accessibility-events","ambient-light-sensor","background-fetch","background-sync","bluetooth","camera","clipboard-read","clipboard-write","device-info","display-capture","gyroscope","geolocation","local-fonts","magnetometer","microphone","midi","nfc","notifications","payment-handler","persistent-storage","push","speaker","storage-access","top-level-storage-access","window-management","query"],t=Array.from({length:(null==n?void 0:n.retries)||3},(function(){return function(){return e(this,void 0,void 0,(function(){var e,n,t,o,a;return r(this,(function(r){switch(r.label){case 0:e={},n=0,t=C,r.label=1;case 1:if(!(n<t.length))return [3,6];o=t[n],r.label=2;case 2:return r.trys.push([2,4,,5]),[4,navigator.permissions.query({name:o})];case 3:return a=r.sent(),e[o]=a.state.toString(),[3,5];case 4:return r.sent(),[3,5];case 5:return n++,[3,1];case 6:return [2,e]}}))}))}()})),[2,Promise.all(t).then((function(e){return O(e,C)}))]}))}))})),i("plugins",(function(){var e=[];if(navigator.plugins)for(var r=0;r<navigator.plugins.length;r++){var n=navigator.plugins[r];e.push([n.name,n.filename,n.description].join("|"));}return new Promise((function(r){r({plugins:e});}))})),i("screen",(function(){return new Promise((function(e){e({is_touchscreen:navigator.maxTouchPoints>0,maxTouchPoints:navigator.maxTouchPoints,colorDepth:screen.colorDepth,mediaMatches:L()});}))})),i("system",(function(){return new Promise((function(e){var r=M();e({platform:window.navigator.platform,cookieEnabled:window.navigator.cookieEnabled,productSub:navigator.productSub,product:navigator.product,useragent:navigator.userAgent,hardwareConcurrency:navigator.hardwareConcurrency,browser:{name:r.name,version:r.version},applePayVersion:_()});}))}));var D,B="SamsungBrowser"!==M().name?1:3,U=null;i("webgl",(function(){return e(this,void 0,void 0,(function(){var e;return r(this,(function(r){"undefined"!=typeof document&&((D=document.createElement("canvas")).width=200,D.height=100,U=D.getContext("webgl"));try{if(!U)throw new Error("WebGL not supported");return e=Array.from({length:B},(function(){return function(){try{if(!U)throw new Error("WebGL not supported");var e="\n          attribute vec2 position;\n          void main() {\n              gl_Position = vec4(position, 0.0, 1.0);\n          }\n      ",r="\n          precision mediump float;\n          void main() {\n              gl_FragColor = vec4(0.812, 0.195, 0.553, 0.921); // Set line color\n          }\n      ",n=U.createShader(U.VERTEX_SHADER),t=U.createShader(U.FRAGMENT_SHADER);if(!n||!t)throw new Error("Failed to create shaders");if(U.shaderSource(n,e),U.shaderSource(t,r),U.compileShader(n),!U.getShaderParameter(n,U.COMPILE_STATUS))throw new Error("Vertex shader compilation failed: "+U.getShaderInfoLog(n));if(U.compileShader(t),!U.getShaderParameter(t,U.COMPILE_STATUS))throw new Error("Fragment shader compilation failed: "+U.getShaderInfoLog(t));var o=U.createProgram();if(!o)throw new Error("Failed to create shader program");if(U.attachShader(o,n),U.attachShader(o,t),U.linkProgram(o),!U.getProgramParameter(o,U.LINK_STATUS))throw new Error("Shader program linking failed: "+U.getProgramInfoLog(o));U.useProgram(o);for(var a=137,i=new Float32Array(4*a),u=2*Math.PI/a,c=0;c<a;c++){var s=c*u;i[4*c]=0,i[4*c+1]=0,i[4*c+2]=Math.cos(s)*(D.width/2),i[4*c+3]=Math.sin(s)*(D.height/2);}var l=U.createBuffer();U.bindBuffer(U.ARRAY_BUFFER,l),U.bufferData(U.ARRAY_BUFFER,i,U.STATIC_DRAW);var f=U.getAttribLocation(o,"position");U.enableVertexAttribArray(f),U.vertexAttribPointer(f,2,U.FLOAT,false,0,0),U.viewport(0,0,D.width,D.height),U.clearColor(0,0,0,1),U.clear(U.COLOR_BUFFER_BIT),U.drawArrays(U.LINES,0,2*a);var d=new Uint8ClampedArray(D.width*D.height*4);return U.readPixels(0,0,D.width,D.height,U.RGBA,U.UNSIGNED_BYTE,d),new ImageData(d,D.width,D.height)}catch(e){return new ImageData(1,1)}finally{U&&(U.bindBuffer(U.ARRAY_BUFFER,null),U.useProgram(null),U.viewport(0,0,U.drawingBufferWidth,U.drawingBufferHeight),U.clearColor(0,0,0,0));}}()})),[2,{commonImageHash:f(b(e,D.width,D.height).data.toString()).toString()}]}catch(e){return [2,{webgl:"unsupported"}]}return [2]}))}))}));var F=function(e,r,n,t){for(var o=(n-r)/t,a=0,i=0;i<t;i++){a+=e(r+(i+.5)*o);}return a*o};i("math",(function(){return e(void 0,void 0,void 0,(function(){return r(this,(function(e){return [2,{acos:Math.acos(.5),asin:F(Math.asin,-1,1,97),atan:F(Math.atan,-1,1,97),cos:F(Math.cos,0,Math.PI,97),cosh:Math.cosh(9/7),e:Math.E,largeCos:Math.cos(1e20),largeSin:Math.sin(1e20),largeTan:Math.tan(1e20),log:Math.log(1e3),pi:Math.PI,sin:F(Math.sin,-Math.PI,Math.PI,97),sinh:F(Math.sinh,-9/7,7/9,97),sqrt:Math.sqrt(2),tan:F(Math.tan,0,2*Math.PI,97),tanh:F(Math.tanh,-9/7,7/9,97)}]}))}))}));

class AdClient {
    config;
    constructor(config) {
        this.config = config;
    }
    async getAds(_properties) {
        return [];
    }
    async onQuerySubmitted(_properties) { }
    async onQueryFetched(_properties, _response) { }
    async onInputCleared() { }
    async onAdContainerRendered(_data) { }
    async onAdContainerViewed(_data) { }
}

class AdMarketplaceClient extends AdClient {
    /**
     * Gets ads from the adMarketplace API based on the search term.
     */
    async getAds(_properties) {
        const admConfig = this.config.admAdConfig;
        if (!admConfig?.sub) {
            console.error('No admSub specified in config.');
            return [];
        }
        const paramString = new URLSearchParams({
            partner: 'demofeed',
            sub1: admConfig.sub,
            qt: _properties.searchTerm,
            v: '2.0',
            rfr: 'searchcraft.io',
            'results-ta': `${admConfig.textAdQuantity || 0}`,
            'results-pa': `${admConfig.productAdQuantity || 0}`,
        }).toString();
        const path = `https://demofeed.is.ampfeed.com/is?${paramString}`;
        const response = await fetch(path, { method: 'GET' });
        const admResponse = (await response.json());
        const productAds = admResponse.product_ads.map((ad) => ({
            id: nanoid(),
            adSource: 'adMarketplace',
            admAdType: 'adm-product-ad',
            admAd: ad,
        }));
        const textAds = admResponse.text_ads.map((ad) => ({
            id: nanoid(),
            adSource: 'adMarketplace',
            admAdType: 'adm-text-ad',
            admAd: ad,
        }));
        const allAds = productAds.concat(textAds);
        return allAds;
    }
    async onAdContainerViewed(data) {
        // Calls fetch on the impression url, to record an impression for adMarketplace
        const item = data.adClientResponseItem;
        if (item.admAd?.impression_url) {
            fetch(item.admAd.impression_url);
        }
    }
}

class CustomAdClient extends AdClient {
    async getAds(_properties) {
        return [];
    }
}

const AD_CALL_AFTER_FETCH_DELAY = 1000;
class NativoClient extends AdClient {
    adCallTimeout;
    constructor(config) {
        super(config);
        this.addScriptTagToDocument();
    }
    async onQuerySubmitted(_properties) {
        this.addScriptTagToDocument();
    }
    async onQueryFetched(_properties, response) {
        if ((response.data.hits?.length || 0) === 0) {
            this.removeScriptTagFromDocument();
            return;
        }
        this.performAdCall(AD_CALL_AFTER_FETCH_DELAY);
    }
    async onInputCleared() {
        this.removeScriptTagFromDocument();
    }
    performAdCall(delay) {
        if (this.adCallTimeout) {
            clearTimeout(this.adCallTimeout);
        }
        this.adCallTimeout = setTimeout(() => {
            this.addScriptTagToDocument();
            try {
                // @ts-ignore
                PostRelease?.Start({ ptd: [this.config.nativoPlacementId] });
            }
            catch (error) {
                console.error(error);
            }
            // console.log('Performing ad call');
        }, delay);
    }
    async getAds(_properties) {
        return [];
    }
    addScriptTagToDocument() {
        if (!document.head.querySelector('#nativo-tag')) {
            const scriptTag = document.createElement('script');
            scriptTag.type = 'text/javascript';
            scriptTag.src = 'https://s.ntv.io/serve/load.js';
            scriptTag.id = 'nativo-tag';
            scriptTag.setAttribute('data-ntv-set-no-auto-start', 'true');
            document.head.appendChild(scriptTag);
        }
    }
    removeScriptTagFromDocument() {
        const scriptTag = document.head.querySelector('#nativo-tag');
        if (scriptTag) {
            document.head.removeChild(scriptTag);
        }
    }
}

const MEASURE_REQUEST_DEBOUNCE = 600;
class MeasureClient {
    measureRequestTimeout;
    measureRequestsBatched = [];
    config;
    sdkInfo;
    userId;
    userType;
    sessionId;
    constructor(config, sdkInfo, userId, userType) {
        this.config = config;
        this.sdkInfo = sdkInfo;
        this.userId = userId;
        this.userType = userType;
        this.sessionId = nanoid();
        this.sendMeasureEvent('sdk_initialized').catch((error) => {
            console.error('Error sending sdk_initialized event:', error);
        });
    }
    /**
     * Getter for the base url used by the /measure endpoints.
     */
    get baseMeasureUrl() {
        return `${this.config.endpointURL}/measure`;
    }
    /**
     * Getter for the measure request user. Uses config values + navigator values.
     */
    get measureRequestUser() {
        return {
            user_id: this.userId,
            locale: navigator.language || 'en-US',
            os: navigator.userAgent.includes('Windows')
                ? 'Windows'
                : navigator.userAgent.includes('Mac')
                    ? 'Mac'
                    : navigator.userAgent.includes('Linux')
                        ? 'Linux'
                        : 'Unknown',
            platform: navigator.platform || 'Unknown',
            sdk_name: this.sdkInfo.sdkName,
            sdk_version: this.sdkInfo.sdkVersion,
            user_agent: navigator.userAgent || 'Unknown',
        };
    }
    /**
     * Sends a measure event to the `/measure/event` endpoint for analytics purposes.
     *
     * @param {MeasureEventName} eventName - Name of the event.
     * @param {Partial<MeasureRequestProperties>} properties - Additional properties to send with the event.
     * @param {Partial<MeasureRequestUser>} user - Additional user properites to send with the event.
     */
    sendMeasureEvent = async (eventName, properties = {}, user = {}) => {
        /**
         * Builds the request object based on config values + provided arguments.
         */
        const request = {
            event_name: eventName,
            properties: {
                searchcraft_index_names: this.config.indexName
                    ? [this.config.indexName]
                    : [],
                searchcraft_federation_name: this.config.federationName,
                session_id: this.sessionId,
                ...properties,
            },
            user: {
                ...this.measureRequestUser,
                ...user,
            },
        };
        // Send document_clicked events immediately
        if (eventName === 'document_clicked') {
            const body = JSON.stringify(request);
            const url = `${this.baseMeasureUrl}/event`;
            try {
                const response = await fetch(url, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        Authorization: this.config.readKey,
                        'X-Sc-User-Id': this.userId,
                        'X-Sc-Session-Id': this.sessionId,
                        'X-Sc-User-Type': this.userType,
                    },
                    body,
                    keepalive: true,
                });
                if (!response.ok) {
                    console.error(`Error sending MeasureRequest: ${response.status} ${response.statusText}`);
                }
            }
            catch (error) {
                console.error('Error sending MeasureRequest:', error);
            }
        }
        else {
            // Otherwise send in batches
            this.measureRequestsBatched.push(request);
            clearTimeout(this.measureRequestTimeout);
            this.measureRequestTimeout = setTimeout(() => {
                const payload = JSON.stringify({ items: this.measureRequestsBatched });
                const url = `${this.baseMeasureUrl}/batch`;
                (async () => {
                    try {
                        const response = await fetch(url, {
                            method: 'POST',
                            headers: {
                                'Content-Type': 'application/json',
                                Authorization: this.config.readKey,
                                'X-Sc-User-Id': this.userId,
                                'X-Sc-Session-Id': this.sessionId,
                                'X-Sc-User-Type': this.userType,
                            },
                            body: payload,
                            keepalive: true,
                        });
                        this.measureRequestsBatched = [];
                        if (!response.ok) {
                            console.error(`Error sending MeasureRequest: ${response.status} ${response.statusText}`);
                        }
                    }
                    catch (error) {
                        this.measureRequestsBatched = [];
                        console.error('Error sending MeasureRequest:', error);
                    }
                })().catch((error) => {
                    console.error('Unhandled error in MeasureRequest batch:', error);
                });
            }, MEASURE_REQUEST_DEBOUNCE);
        }
    };
}

const removeTrailingSlashFromURL = (endpointURL) => endpointURL.endsWith('/') ? endpointURL.slice(0, -1) : endpointURL;

const sanitize = (str) => {
    let sanitizedStr = '';
    // Trim whitespace
    sanitizedStr = str.trim();
    // Replace fancy quotes
    sanitizedStr = sanitizedStr.replace(/[‘’“”]/g, (match) => {
        const map = {
            '‘': "'",
            '’': "'",
            '“': '"',
            '”': '"',
        };
        return map[match] || match;
    });
    // Whether there are an equal number of quotes
    const quoteCount = (sanitizedStr.match(/"/g) || []).length;
    if (quoteCount % 2 !== 0) {
        throw new Error('The search term contains an uneven number of quote characters.');
    }
    return sanitizedStr;
};

const SEARCH_COMPLETED_EVENT_DEBOUNCE = 500;
class SearchClient {
    config;
    userId;
    parent;
    searchCompletedEventTimeout;
    abortController;
    supplementalAbortController;
    constructor(parent, config, userId) {
        this.parent = parent;
        this.config = config;
        this.userId = userId;
    }
    /**
     * Getter for the base url used by the /search endpoint.
     * Supports both index and federation search endpoints.
     */
    get baseSearchUrl() {
        return this.config.federationName
            ? `${this.config.endpointURL}/federation/${this.config.federationName}/search`
            : `${this.config.endpointURL}/index/${this.config.indexName}/search`;
    }
    /**
     * Immediately cancels all pending search requests.
     */
    abortRequests = () => {
        this.abortController?.abort('The pending search request has been cancelled.');
        this.supplementalAbortController?.abort('The pending search request has been cancelled.');
    };
    /**
     * Make the request to get the search results.
     * @param {properties} properties - The properties for the search.
     * @param isSupplemental - Whether or not this is a supplemental search request (for the purpose of getting top-level facet counts)
     * @returns
     */
    getSearchResponseItems = async (properties, isSupplemental = false) => {
        let response;
        let searchTerm = '';
        let abortController;
        if (isSupplemental) {
            this.supplementalAbortController?.abort('A newer search request has replaced this one.');
            abortController = new AbortController();
            this.supplementalAbortController = abortController;
        }
        else {
            this.abortController?.abort('A newer search request has replaced this one.');
            abortController = new AbortController();
            this.abortController = abortController;
        }
        // Sanitize the search term prior to any request
        // The function will throw if it is not valid
        if (typeof properties === 'string') {
            searchTerm = sanitize(properties);
        }
        else {
            properties.searchTerm = sanitize(properties.searchTerm);
            searchTerm = properties.searchTerm;
        }
        this.parent.measureClient?.sendMeasureEvent('search_requested', {
            search_term: searchTerm,
        });
        this.parent.emitEvent('query_submitted', {
            name: 'query_submitted',
            data: {
                searchTerm,
            },
        });
        this.parent.adClient?.onQuerySubmitted(typeof properties === 'string'
            ? { searchTerm, mode: 'exact' }
            : properties);
        if (typeof properties === 'string') {
            response = await this.handleGetSearchResponseItemsWithString(searchTerm, abortController);
        }
        else {
            response = await this.handleGetSearchResponseItemsWithObject(properties, abortController);
        }
        if (!isSupplemental) {
            this.parent.measureClient?.sendMeasureEvent('search_response_received', {
                search_term: searchTerm,
                number_of_documents: response.data.count,
            });
            clearTimeout(this.searchCompletedEventTimeout);
            this.searchCompletedEventTimeout = setTimeout(() => {
                this.parent.measureClient?.sendMeasureEvent('search_completed', {
                    search_term: searchTerm,
                    number_of_documents: response.data.count,
                });
            }, SEARCH_COMPLETED_EVENT_DEBOUNCE);
            this.parent.emitEvent('query_fetched', {
                name: 'query_fetched',
                data: {
                    searchTerm,
                },
            });
            if ((response.data.hits?.length || 0) === 0) {
                this.parent.emitEvent('no_results_returned', {
                    name: 'no_results_returned',
                });
            }
            this.parent.adClient?.onQueryFetched(typeof properties === 'string'
                ? { searchTerm, mode: 'exact' }
                : properties, response);
        }
        return response;
    };
    handleGetSearchResponseItemsWithString = async (str, abortController) => {
        let searchClientRequest;
        try {
            searchClientRequest = JSON.parse(str);
            searchClientRequest = {
                limit: this.config.searchResultsPerPage,
                ...searchClientRequest,
            };
        }
        catch {
            throw new Error('Error: Query string is not valid json.');
        }
        this.parent.store.setState({ searchClientRequest });
        const response = await fetch(this.baseSearchUrl, {
            method: 'POST',
            headers: {
                Authorization: this.config.readKey,
                'Content-Type': 'application/json',
                'X-Sc-User-Id': this.userId,
                'X-Sc-Session-Id': this.parent.measureClient?.sessionId || nanoid(),
                'X-Sc-User-Type': this.parent.userType,
            },
            body: JSON.stringify(searchClientRequest),
            signal: abortController.signal,
        });
        if (!response.ok) {
            throw new Error(`Error: ${response.statusText} (Status: ${response.status})`);
        }
        return (await response.json());
    };
    handleGetSearchResponseItemsWithObject = async (properties, abortController) => {
        const searchClientRequest = {
            query: this.formatParamsForRequest(properties),
            offset: properties.offset || 0,
            limit: properties.limit || this.config.searchResultsPerPage || 20,
            ...(properties.order_by && {
                order_by: properties.order_by,
            }),
            ...(properties.sort && {
                sort: properties.sort,
            }),
        };
        this.parent.store.setState({ searchClientRequest });
        const response = await fetch(this.baseSearchUrl, {
            method: 'POST',
            headers: {
                Authorization: this.config.readKey,
                'Content-Type': 'application/json',
                'X-Sc-User-Id': this.userId,
                'X-Sc-Session-Id': this.parent.measureClient?.sessionId || nanoid(),
                'X-Sc-User-Type': this.parent.userType,
            },
            body: JSON.stringify(searchClientRequest),
            signal: abortController.signal,
        });
        if (!response.ok) {
            throw new Error(`Error: ${response.statusText} (Status: ${response.status})`);
        }
        return (await response.json());
    };
    /**
     * Builds a query object for the SearchClient request.
     * @param {properties} properties - The properties for the search.
     * @returns {SearchClientQuery} - A properly formatted SearchClient query object.
     */
    formatParamsForRequest(properties) {
        const queries = [];
        let occur = 'should';
        if (properties.facetPathsForIndexFields) {
            Object.keys(properties.facetPathsForIndexFields).forEach((fieldName) => {
                const item = properties.facetPathsForIndexFields?.[fieldName];
                if (item) {
                    occur = 'must';
                    queries.push({
                        occur: 'must',
                        exact: {
                            ctx: sanitize(item.value),
                        },
                    });
                }
            });
        }
        if (properties.rangeValueForIndexFields) {
            Object.keys(properties.rangeValueForIndexFields).forEach((fieldName) => {
                const item = properties.rangeValueForIndexFields?.[fieldName];
                if (item) {
                    occur = 'must';
                    queries.push({
                        occur: 'must',
                        exact: {
                            ctx: sanitize(item.value),
                        },
                    });
                }
            });
        }
        const searchTerm = properties.searchTerm;
        const query = properties.mode === 'fuzzy'
            ? { fuzzy: { ctx: searchTerm } }
            : {
                exact: {
                    ctx: `${searchTerm.startsWith('"') ? searchTerm : `"${searchTerm}"`}`,
                },
            };
        queries.push({
            occur: properties.mode === 'exact' ? 'must' : occur, // Valid, as 'occur' is a required property in SearchClientQuery
            ...query,
        });
        return queries;
    }
}

const createStoreImpl = (createState) => {
  let state;
  const listeners = /* @__PURE__ */ new Set();
  const setState = (partial, replace) => {
    const nextState = typeof partial === "function" ? partial(state) : partial;
    if (!Object.is(nextState, state)) {
      const previousState = state;
      state = (replace != null ? replace : typeof nextState !== "object" || nextState === null) ? nextState : Object.assign({}, state, nextState);
      listeners.forEach((listener) => listener(state, previousState));
    }
  };
  const getState = () => state;
  const getInitialState = () => initialState;
  const subscribe = (listener) => {
    listeners.add(listener);
    return () => listeners.delete(listener);
  };
  const api = { setState, getState, getInitialState, subscribe };
  const initialState = state = createState(setState, getState, api);
  return api;
};
const createStore = ((createState) => createState ? createStoreImpl(createState) : createStoreImpl);

const DEBOUNCE_DELAY = 1000;
class SummaryClient {
    set;
    get;
    abortController;
    timeout;
    constructor(get, set) {
        this.get = get;
        this.set = set;
    }
    streamSummaryData() {
        const begin = async () => {
            const state = this.get();
            const config = state.core?.config;
            if (!config) {
                console.error('Could not stream summary data, no config found.');
                return;
            }
            if (!config.cortexURL) {
                console.error('Could not stream summary data, cortexURL was not specified in the config.');
                return;
            }
            const indexName = state.core?.config.indexName;
            if (!state.hasSummaryBox || !indexName) {
                return;
            }
            this.abortController?.abort('A newer request has replaced this one.');
            this.abortController = new AbortController();
            this.set({
                isSummaryLoading: true,
                summary: '',
            });
            const endpointUrl = `${config.cortexURL.replace(/\/$/, '')}/api/search/summary`;
            try {
                const fetchResponse = await fetch(endpointUrl, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        Authorization: state.core?.config.readKey || '',
                        'X-Sc-User-Id': state.core?.userId || '',
                        'X-Sc-Session-Id': state.core?.measureClient?.sessionId || '',
                        'X-Sc-User-Type': state.core?.userType || 'anonymous',
                    },
                    body: JSON.stringify({
                        searchQuery: state.searchClientRequest,
                        summaryInstructionsPrompt: state.core?.config.summaryInstructionsPrompt,
                        indexName: indexName,
                        endpointUrl: state.core?.config.endpointURL,
                    }),
                    signal: this.abortController.signal,
                });
                if (!fetchResponse) {
                    throw new Error('Invalid fetch response');
                }
                if (!fetchResponse.body) {
                    throw new Error('Invalid fetch response');
                }
                if (!fetchResponse.ok) {
                    if (fetchResponse.status === 403) {
                        console.warn('Please contact Searchcraft to enable AI summaries with your account');
                        this.set({
                            isSummaryLoading: false,
                            isSummaryNotEnabled: true,
                        });
                        return;
                    }
                    throw new Error(`HTTP ${fetchResponse.status}`);
                }
                const reader = fetchResponse.body.getReader();
                const decoder = new TextDecoder();
                let finishedReading = false;
                do {
                    const { done, value } = await reader.read();
                    finishedReading = done;
                    const chunk = decoder.decode(value, { stream: true });
                    this.set((state) => ({
                        isSummaryLoading: false,
                        summary: `${state.summary}${chunk}`,
                    }));
                } while (!finishedReading);
            }
            catch (error) {
                if (error instanceof Error) {
                    console.error(error.message);
                }
                this.set({
                    isSummaryLoading: false,
                });
            }
        };
        const delay = this.get().core?.config.summaryDebounceDelay || DEBOUNCE_DELAY;
        clearTimeout(this.timeout);
        this.timeout = setTimeout(() => begin(), delay);
    }
}

const initialSearchcraftStateValues = {
    adClientResponseItems: [],
    cachedAdClientResponseItems: [],
    core: undefined,
    hotkey: 'k',
    hotkeyModifier: 'meta',
    facetPathsForIndexFields: {},
    isFilterPanelVisible: true,
    isPopoverVisible: false,
    isSearchInProgress: false,
    rangeValueForIndexFields: {},
    searchTerm: '',
    searchMode: 'fuzzy',
    searchClientRequest: undefined,
    searchClientRequestProperties: undefined,
    searchClientResponseItems: [],
    cachedSearchClientResponseItems: [],
    cachedSearchResponseFacetPrime: undefined,
    cachedSupplementalFacetPrime: undefined,
    cachedSearchResponseTimeTaken: undefined,
    cachedSearchResultsCount: undefined,
    cachedSearchClientRequestProperties: undefined,
    searchResponseTimeTaken: undefined,
    searchResponseFacetPrime: undefined,
    supplementalFacetPrime: undefined,
    searchResultsCount: 0,
    searchResultsPerPage: 20,
    searchResultsPage: 1,
    sortType: undefined,
    orderByField: undefined,
    summary: '',
    hasSummaryBox: false,
    summaryClient: undefined,
    isSummaryLoading: false,
    isSummaryNotEnabled: false,
};
// const logger = new Logger({ logLevel: LogLevel.NONE });
const existingStores = {};
/**
 * This is a factory function for creating new searchcraft stores.
 *
 * Searchcraft Stores contain the state information used by a SearchcraftCore instance.
 *
 * This factory function only needs to be called when a new SearchcraftCore is instantiated.
 * @returns
 */
const createSearchcraftStore = (searchcraftId, initialState = {}) => {
    const id = searchcraftId || DEFAULT_CORE_INSTANCE_ID;
    if (existingStores[id]) {
        existingStores[id].setState(initialState);
        return existingStores[id];
    }
    const newStore = createStore((set, get) => {
        const functions = {
            addFacetPathsForIndexField: (facetPaths) => {
                set((state) => ({
                    facetPathsForIndexFields: {
                        ...state.facetPathsForIndexFields,
                        [facetPaths.fieldName]: facetPaths,
                    },
                    searchResultsPage: 1, // Reset to page 1 when filters change
                }));
            },
            addRangeValueForIndexField: (rangeValue) => set((state) => ({
                rangeValueForIndexFields: {
                    ...state.rangeValueForIndexFields,
                    [rangeValue.fieldName]: rangeValue,
                },
            })),
            removeFacetPathsForIndexField: (fieldName) => {
                console.log('[FACET] removeFacetPathsForIndexField', {
                    fieldName,
                    currentPage: get().searchResultsPage,
                    stackTrace: new Error().stack,
                });
                set((state) => {
                    const currentPaths = state.facetPathsForIndexFields;
                    delete currentPaths[fieldName];
                    return {
                        facetPathsForIndexFields: {
                            ...currentPaths,
                        },
                        searchResultsPage: 1, // Reset to page 1 when filters change
                    };
                });
            },
            removeRangeValueForIndexField: (fieldName) => set((state) => {
                const currentValues = state.rangeValueForIndexFields;
                delete currentValues[fieldName];
                return {
                    rangeValueForIndexFields: {
                        ...currentValues,
                    },
                };
            }),
            resetSearchValues: () => {
                const state = get();
                state.core?.searchClient?.abortRequests();
                set({
                    searchTerm: '',
                    searchResultsPage: 1,
                    searchClientResponseItems: [...state.cachedSearchClientResponseItems],
                    adClientResponseItems: [...state.cachedAdClientResponseItems],
                });
            },
            search: async (options) => {
                const state = get();
                if (!state.core) {
                    throw new Error('Searchcraft instance is not initialized.');
                }
                if (state.core.config.cortexURL && !options?.skipSummary) {
                    state.summaryClient?.streamSummaryData();
                }
                // Check if this is an initialQuery case (string requestProperties with empty searchTerm)
                const isInitialQuery = typeof state.cachedSearchClientRequestProperties === 'string' &&
                    state.searchTerm.trim() === '';
                // Check if there are any active filters or pagination changes
                const hasActiveFilters = Object.keys(state.facetPathsForIndexFields).length > 0 ||
                    Object.keys(state.rangeValueForIndexFields).length > 0;
                const hasNonDefaultPagination = state.searchResultsPage !== 1;
                const hasNonDefaultSearchMode = state.searchMode !== 'fuzzy';
                if (!state.searchTerm.trim()) {
                    // If it's initialQuery and there are filters, pagination, or mode changes, perform a search
                    if (isInitialQuery &&
                        (hasActiveFilters ||
                            hasNonDefaultPagination ||
                            hasNonDefaultSearchMode)) {
                        // Parse the initialQuery
                        const initialQueryObj = JSON.parse(state.cachedSearchClientRequestProperties);
                        set({ isSearchInProgress: true });
                        // Build the modified query array starting with the base query
                        const baseQuery = Array.isArray(initialQueryObj.query)
                            ? initialQueryObj.query.filter((q) => !q.occur)
                            : [initialQueryObj.query];
                        const queries = [...baseQuery];
                        // Add facet filters
                        if (state.facetPathsForIndexFields) {
                            Object.keys(state.facetPathsForIndexFields).forEach((fieldName) => {
                                const item = state.facetPathsForIndexFields?.[fieldName];
                                if (item) {
                                    queries.push({
                                        occur: 'must',
                                        exact: {
                                            ctx: item.value,
                                        },
                                    });
                                }
                            });
                        }
                        // Add range filters
                        if (state.rangeValueForIndexFields) {
                            Object.keys(state.rangeValueForIndexFields).forEach((fieldName) => {
                                const item = state.rangeValueForIndexFields?.[fieldName];
                                if (item) {
                                    queries.push({
                                        occur: 'must',
                                        exact: {
                                            ctx: item.value,
                                        },
                                    });
                                }
                            });
                        }
                        // Build the modified request with filters and pagination
                        const modifiedRequest = {
                            ...initialQueryObj,
                            query: queries,
                            offset: state.searchResultsPerPage
                                ? state.searchResultsPerPage * (state.searchResultsPage - 1)
                                : 0,
                            limit: state.searchResultsPerPage,
                        };
                        state.core.getResponseItems({
                            requestProperties: JSON.stringify(modifiedRequest),
                            shouldCacheResultsForEmptyState: false,
                        });
                        return;
                    }
                    // Otherwise, restore cached results
                    state.core?.searchClient?.abortRequests();
                    set({
                        searchClientResponseItems: [
                            ...state.cachedSearchClientResponseItems,
                        ],
                        adClientResponseItems: [...state.cachedAdClientResponseItems],
                        searchResponseFacetPrime: state.cachedSearchResponseFacetPrime,
                        supplementalFacetPrime: state.cachedSupplementalFacetPrime,
                        searchResponseTimeTaken: state.cachedSearchResponseTimeTaken,
                        searchResultsCount: state.cachedSearchResultsCount || 0,
                        searchClientRequestProperties: state.cachedSearchClientRequestProperties,
                        searchResultsPage: 1,
                        searchTerm: '',
                    });
                    return;
                }
                set({ isSearchInProgress: true });
                const searchClientRequestProperites = {
                    searchTerm: state.searchTerm,
                    mode: state.searchMode,
                    sort: state.sortType,
                    order_by: state.orderByField,
                    facetPathsForIndexFields: state.facetPathsForIndexFields,
                    rangeValueForIndexFields: state.rangeValueForIndexFields,
                    offset: state.searchResultsPerPage
                        ? state.searchResultsPerPage * (state.searchResultsPage - 1)
                        : 0,
                    limit: state.searchResultsPerPage,
                };
                state.core.getResponseItems({
                    requestProperties: searchClientRequestProperites,
                    shouldCacheResultsForEmptyState: false,
                });
            },
            setFilterPanelVisibility: (isVisible) => set({
                isFilterPanelVisible: isVisible,
            }),
            setPopoverVisibility: (isVisible) => set({
                isPopoverVisible: isVisible,
            }),
            setSearchMode: (mode) => set({ searchMode: mode }),
            setSortOrder: ({ orderByField, sortType }) => set({ sortType, orderByField }),
            setSearchTerm: (searchTerm) => {
                const state = get();
                if (searchTerm.length === 0) {
                    state.core?.handleInputCleared();
                }
                /**
                 * When a new searchTerm is set, also reset the sort type, search mode
                 */
                set({
                    searchTerm,
                    searchResultsPage: 1,
                    ...(searchTerm.trim().length === 0 && {
                        searchMode: 'fuzzy',
                        sortType: null,
                        orderByField: null,
                        searchClientResponseItems: [
                            ...state.cachedSearchClientResponseItems,
                        ],
                        adClientResponseItems: [...state.cachedAdClientResponseItems],
                        searchResponseFacetPrime: state.cachedSearchResponseFacetPrime,
                        supplementalFacetPrime: state.cachedSupplementalFacetPrime,
                        searchResponseTimeTaken: state.cachedSearchResponseTimeTaken,
                        searchResultsCount: state.cachedSearchResultsCount || 0,
                        searchClientRequestProperties: state.cachedSearchClientRequestProperties,
                    }),
                });
            },
            setSearchResultsCount: (count) => set({ searchResultsCount: count }),
            setSearchResultsPage: async (page) => {
                set({ searchResultsPage: page });
                await functions.search({ skipSummary: true });
            },
            setSearchResultsPerPage: async (perPage) => {
                set({ searchResultsPerPage: perPage });
                await functions.search({ skipSummary: true });
            },
            setHotKeyAndHotKeyModifier: (hotkey, hotkeyModifier) => {
                const { hotkey: initialHotkey, hotkeyModifier: initialHotkeyModifier } = initialSearchcraftStateValues;
                set({
                    hotkey: hotkey || initialHotkey,
                    hotkeyModifier: hotkeyModifier || initialHotkeyModifier,
                });
            },
        };
        const stateObject = {
            ...initialSearchcraftStateValues,
            ...initialState,
            ...functions,
            ...{
                summaryClient: new SummaryClient(get, set),
            },
        };
        return stateObject;
    });
    existingStores[id] = newStore;
    return newStore;
};

/**
 * Javascript Class providing the functionality to interact with the Searchcraft BE
 */
class SearchcraftCore {
    store;
    config;
    measureClient;
    searchClient;
    adClient;
    userId;
    userType;
    requestTimeout;
    subscriptionEvents = {};
    /**
     * @param config The SearchcraftConfig object for this Searchcraft instance.
     * @param sdkInfo The SDK info object for this searchcraft instance
     * @param searchcraftId The identifier to use to reference this instance of SearchcraftCore.
     */
    constructor(config, sdkInfo, searchcraftId) {
        if (!config.endpointURL) {
            throw new Error('SDK Configuration Error: endpointURL not specified.');
        }
        if (!config.readKey) {
            throw new Error('SDK Configuration Error: readKey not specified.');
        }
        if (!config.indexName && !config.federationName) {
            throw new Error('SDK Configuration Error: Either indexName or federationName must be specified.');
        }
        if (config.indexName && config.federationName) {
            throw new Error('SDK Configuration Error: Cannot specify both indexName and federationName. Please specify only one.');
        }
        this.config = {
            ...config,
            // Strips off the trailing '/' from an endpointURL if one is accidentally added
            endpointURL: removeTrailingSlashFromURL(config.endpointURL),
        };
        this.userId = '';
        this.userType = 'anonymous';
        if (typeof window !== 'undefined' &&
            typeof customElements !== 'undefined' &&
            sdkInfo.sdkName === '@searchcraft/javascript-sdk' &&
            globalThis.__scDefineCustomElements__) {
            globalThis.__scDefineCustomElements__();
        }
        this.store = createSearchcraftStore(searchcraftId, {
            core: this,
            searchResultsPerPage: config.searchResultsPerPage || 20,
        });
        registry.addCoreInstance(this, searchcraftId);
        (async (config) => {
            if (typeof window !== 'undefined') {
                await this.initClients(config, sdkInfo);
            }
            if (config.initialQuery) {
                this.getResponseItems({
                    requestProperties: config.initialQuery,
                    shouldCacheResultsForEmptyState: true,
                });
            }
        })(this.config);
    }
    async initClients(config, sdkInfo) {
        let userId = this.config.measureUserIdentifier;
        if (!userId) {
            const fingerprint = await g();
            userId = fingerprint;
            this.userType = 'anonymous';
        }
        else {
            this.userType = 'authenticated';
            await Promise.resolve();
        }
        this.measureClient = new MeasureClient(config, sdkInfo, userId, this.userType);
        this.searchClient = new SearchClient(this, config, userId);
        if (config.customAdConfig) {
            this.adClient = new CustomAdClient(config);
        }
        else if (config.nativoConfig) {
            this.adClient = new NativoClient(config);
        }
        else if (config.admAdConfig) {
            this.adClient = new AdMarketplaceClient(config);
        }
        this.emitEvent('initialized', {
            name: 'initialized',
        });
    }
    emitEvent(eventName, event) {
        this.subscriptionEvents[eventName]?.forEach((callback) => {
            callback(event);
        });
    }
    subscribe(eventName, callback) {
        if (!this.subscriptionEvents[eventName]) {
            this.subscriptionEvents[eventName] = [];
        }
        this.subscriptionEvents[eventName].push(callback);
        return () => {
            this.subscriptionEvents[eventName] = this.subscriptionEvents[eventName].filter((cb) => cb !== callback);
        };
    }
    /**
     * Called when a `<searchcraft-ad>` component is rendered
     */
    handleAdContainerRendered(data) {
        this.adClient?.onAdContainerRendered(data);
        // Emits ad_container_rendered event.
        this.emitEvent('ad_container_rendered', {
            name: 'ad_container_rendered',
            data: {
                adContainerId: data.adContainerId,
                searchTerm: data.searchTerm,
            },
        });
    }
    /**
     * Called when a `<searchcraft-ad>` is viewed
     */
    handleAdContainerViewed(data) {
        this.adClient?.onAdContainerViewed(data);
        // Emits ad_container_rendered event.
        this.emitEvent('ad_container_viewed', {
            name: 'ad_container_viewed',
            data: {
                adContainerId: data.adContainerId,
                searchTerm: data.searchTerm,
            },
        });
    }
    /**
     * Perform various actions when the input is cleared
     */
    handleInputCleared() {
        this.emitEvent('input_cleared', {
            name: 'input_cleared',
        });
        this.adClient?.onInputCleared();
    }
    getResponseItems = (props) => {
        const getResponseItemsDebounced = async () => {
            /**
             * Handles search response from the search client.
             */
            (async () => {
                if (!this.searchClient) {
                    console.error('Search client was not initialized.');
                    return;
                }
                let response;
                try {
                    response = await this.searchClient.getSearchResponseItems(props.requestProperties, false);
                }
                catch (error) {
                    Logger.info(`Search request error: ${error}`);
                    return;
                }
                if (!response) {
                    Logger.info('Search request error: Search response was undefined');
                    return;
                }
                const items = (response.data.hits || [])
                    .filter((hit) => !!hit.doc)
                    .map((hit) => ({
                    id: nanoid(),
                    document: hit.doc || { id: -1 },
                    source_index: hit.source_index,
                    type: 'SearchDocument',
                }));
                /**
                 * Handles sending a supplemental search request (For getting top-level facet counts)
                 */
                let supplementalResponse;
                if (typeof props.requestProperties === 'object') {
                    if (props.requestProperties.facetPathsForIndexFields &&
                        Object.keys(props.requestProperties.facetPathsForIndexFields)
                            .length > 0) {
                        const { facetPathsForIndexFields: _, ...supplementalRequestProperties } = props.requestProperties;
                        try {
                            supplementalResponse =
                                await this?.searchClient?.getSearchResponseItems(supplementalRequestProperties, true);
                        }
                        catch (error) {
                            Logger.info(`Search request error: ${error}`);
                            return;
                        }
                    }
                }
                else if (typeof props.requestProperties === 'string') {
                    // Handle supplemental search for initialQuery with filters
                    const requestObj = JSON.parse(props.requestProperties);
                    // Check if there are any filter queries (queries beyond the base query)
                    const queryArray = Array.isArray(requestObj.query)
                        ? requestObj.query
                        : [requestObj.query];
                    if (queryArray.length > 1) {
                        // Remove filter queries to get base query for supplemental search
                        const baseQuery = queryArray.filter((q) => q.occur !== 'must');
                        const supplementalRequest = {
                            ...requestObj,
                            query: baseQuery.length > 0 ? baseQuery : queryArray.slice(0, 1),
                        };
                        try {
                            supplementalResponse =
                                await this?.searchClient?.getSearchResponseItems(JSON.stringify(supplementalRequest), true);
                        }
                        catch (error) {
                            Logger.info(`Search request error: ${error}`);
                            return;
                        }
                    }
                }
                this.store.setState({
                    isSearchInProgress: false,
                    searchClientResponseItems: items,
                    searchResponseTimeTaken: response.data.time_taken,
                    searchResultsCount: response.data.count,
                    searchResponseFacetPrime: response.data.facets,
                    supplementalFacetPrime: supplementalResponse?.data.facets,
                    searchClientRequestProperties: props.requestProperties,
                    ...(props.shouldCacheResultsForEmptyState && {
                        cachedSearchClientResponseItems: items,
                        cachedSearchResponseFacetPrime: response.data.facets,
                        cachedSupplementalFacetPrime: supplementalResponse?.data.facets,
                        cachedSearchResponseTimeTaken: response.data.time_taken,
                        cachedSearchResultsCount: response.data.count,
                        cachedSearchClientRequestProperties: props.requestProperties,
                    }),
                });
            })();
            /**
             * Handles ad response from the ad client.
             */
            (async () => {
                if (this.adClient && typeof props.requestProperties !== 'string') {
                    const items = await this.adClient.getAds(props.requestProperties);
                    this.store.setState({
                        adClientResponseItems: items,
                        ...(props.shouldCacheResultsForEmptyState && {
                            cachedAdClientResponseItems: items,
                        }),
                    });
                }
            })();
        };
        clearTimeout(this.requestTimeout);
        if (this.config.searchDebounceDelay) {
            this.requestTimeout = setTimeout(getResponseItemsDebounced, this.config.searchDebounceDelay);
        }
        else {
            getResponseItemsDebounced();
        }
    };
}

/**
 * @fileoverview entry point for your component library
 *
 * This is the entry point for your component library. Use this file to export utilities,
 * constants or data structure that accompany your components.
 *
 * DO NOT use this file to export your components. Instead, use the recommended approaches
 * to consume components of this package as outlined in the `README.md`.
 */
/**
 * The consumer-facing `Searchcraft` class.
 */
class Searchcraft extends SearchcraftCore {
    constructor(config, searchcraftId = undefined) {
        super(config, {
            sdkName: name,
            sdkVersion: version,
        }, searchcraftId);
    }
}

export { Searchcraft, SearchcraftCore };
//# sourceMappingURL=index.js.map

//# sourceMappingURL=index.js.map
export { defineCustomElements } from './defineCustomElements.js?scv=0.14.0';
