text: /**
   * Inspired by https://github.com/olivernn/lunr.js
   * Copyright 2019 The Pennsylvania State University
   * @license Apache-2.0, see License.md for full text.
   */
  import{LitElement as t,html as e,css as r}from"../../lit-element/lit-element.js";import"../es-global-bridge/es-global-bridge.js";class LunrSearch extends t{static get styles(){return[r`
          :host {
            display: block;
          }
  
          :host([hidden]) {
            display: none;
          }
        `]}render(){return e` <slot></slot>`}static get properties(){return{...super.properties,dataSource:{type:String,attribute:"data-source"},__auto:{type:Boolean},data:{type:Array},method:{type:String},search:{type:String},results:{type:Array},noStopWords:{type:Boolean,attribute:"no-stop-words"},fields:{type:Array},indexNoStopWords:{type:Object},index:{type:Object},__lunrLoaded:{type:Boolean},limit:{type:Number},minScore:{type:Number},log:{type:Boolean}}}constructor(){super(),this.method="GET",this.noStopWords=!1,this.dataSource=null,this.fields=[],this.limit=500,this.__auto=!1,this.minScore=0,this.log=!1,window.WCGlobalBasePath?this.basePath=window.WCGlobalBasePath:this.basePath=this.pathFromUrl(decodeURIComponent(import.meta.url))+"../../";const t=`${this.basePath}lunr/lunr.js`;window.addEventListener("es-bridge-lunr-loaded",this._lunrLoaded.bind(this)),window.ESGlobalBridge.requestAvailability(),window.ESGlobalBridge.instance.load("lunr",t),window.ESGlobalBridge.imports&&!0===window.ESGlobalBridge.imports.lunr&&setTimeout((()=>{this.__lunrLoaded=!0}),0)}updated(t){t.forEach(((t,e)=>{["dataSource","__auto","method"].includes(e)&&this.dataSource&&this.method&&(clearTimeout(this.__debounce),this.__debounce=setTimeout((()=>{fetch(this.dataSource,{method:this.method}).then((t=>{if(t.ok)return t.json()})).then((t=>{this._dataResponse(t)}))}),0));if(["data","search","results","noStopWords"].includes(e)){let t=`${e.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g,"$1-$2").toLowerCase()}-changed`;this.dispatchEvent(new CustomEvent(t,{detail:{value:this[e]}}))}"dataSource"==e&&this[e]&&(this.__auto=!0),["data","search","index","minScore","limit"].includes(e)&&(this.results=this.searched(this.data,this.search,this.index,this.minScore,this.limit)),["data","fields","noStopWords","__lunrLoaded"].includes(e)&&(this.index=this._createIndex(this.data,this.fields,this.noStopWords,this.__lunrLoaded))}))}pathFromUrl(t){return t.substring(0,t.lastIndexOf("/")+1)}disconnectedCallback(){window.removeEventListener("es-bridge-lunr-loaded",this._lunrLoaded.bind(this)),super.disconnectedCallback()}_lunrLoaded(t){this.__lunrLoaded=!0,window.removeEventListener("es-bridge-lunr-loaded",this._lunrLoaded.bind(this))}static get tag(){return"lunr-search"}_dataResponse(t){if(t)try{this.data=[...t]}catch(t){console.warn(t)}}searched(t,e,r,i,o){if(t&&r&&e){var s=[];if(""+e!="")for(var n=r.search(e),a=0;a<n.length&&!(a===o||n[a].score<i);a++){let e=t.find((t=>t.id==n[a].ref));s.push(e)}if(0===s.length&&!this.noStopWords&&""+e!=""){this.indexNoStopWords||(this.indexNoStopWords=this._createIndex(t,this.fields,!0,this.__lunrLoaded)),n=this.indexNoStopWords.search(e);for(s=[],a=0;a<n.length&&!(a===o||n[a].score<i);a++){let e=t.find((t=>t.id==n[a].ref));s.push(e)}}return s}}_createIndex(t,e,r,i){if(i){let i=this;if(Array.isArray(t)&&t.length>0){if(Array.isArray(e)&&e.length>0)return lunr((function(){for(var i=0;i<e.length;i++)e[i].charAt(0)===e[i].charAt(0).toUpperCase()?this.field(e[i],{boost:10}):this.field(e[i]);for(i=0;i<t.length;i++){for(var o={id:i},s=0;s<e.length;s++)t[i].hasOwnProperty(e[s])&&null!==t[i][e[s]]&&"function"==typeof t[i][e[s]].toString&&(t[i][e[s]].toString().split(" ").length>2||t[i][e[s]].toString().length<30)?o[e[s]]=t[i][e[s]].toString():o[e[s]]="";this.add(o)}r&&this.pipeline.remove(lunr.stopWordFilter)}));e=[];var o={};return lunr((function(){for(var s=0;s<t.length;s++)for(var n in t[s])"_"!==n.charAt(0)&&!o.hasOwnProperty(n)&&(n.toString().split(" ").length>2||n.toString().length<30)&&(e.push(n),n.charAt(0)===n.charAt(0).toUpperCase()?this.field(n,{boost:10}):this.field(n),o[n]=1);e.length>0&&(i.fields=e);for(var a=0;a<t.length;a++){for(var d={id:a},l=0;l<e.length;l++)t[a].hasOwnProperty(e[l])&&null!==t[a][e[l]]&&"function"==typeof t[a][e[l]].toString&&(t[a][e[l]].toString().split(" ").length>2||t[a][e[l]].toString().length<30)?d[e[l]]=t[a][e[l]].toString():d[e[l]]="";this.add(d)}r&&this.pipeline.remove(lunr.stopWordFilter)}))}}}}window.customElements.define(LunrSearch.tag,LunrSearch);export{LunrSearch};