/**
 * Copyright 2020 The Pennsylvania State University
 * @license Apache-2.0, see License.md for full text.
 */
import{LitElement as e}from"../../lit/index.js";export const SimpleFilterMixin=function(e){return class extends e{constructor(){super(),this.caseSensitive=!1,this.filtered=[],this.multiMatch=!1,this.resetList()}resetList(e=[]){this.items=[...e],this.where="title",this.value="",this.like=""}static get properties(){return{...super.properties,items:{type:Array},like:{type:String},where:{type:String},caseSensitive:{type:Boolean,attribute:"case-sensitive",reflect:!0},multiMatch:{type:Boolean,attribute:"multi-match"},filtered:{type:Array}}}update(e){super.update(e),e.forEach(((e,t)=>{["items","where","like","caseSensitive","multiMatch"].includes(t)&&this.shadowRoot&&(clearTimeout(this.__debounce),this.__debounce=setTimeout((()=>{this.filtered=this._computeFiltered(this.items,this.where,this.like,this.caseSensitive,this.multiMatch)}),250)),"filtered"==t&&this.shadowRoot&&(this.dispatchEvent(new CustomEvent("filter",{bubbles:!0,cancelable:!0,composed:!0,detail:!0})),this.dispatchEvent(new CustomEvent("filtered-changed",{bubbles:!0,cancelable:!0,composed:!0,detail:{value:this.filtered}})))}))}filter(){this.where=""}escapeRegExp(e){return e.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&")}_computeFiltered(e,t,i,s,r){i=this.escapeRegExp(i);let l=null;if(r){const e=i.split("\\ ");var n=[];e.forEach((e=>{(e=e.replace("\\","").replace("\\?","").replace("\\.","").replace("\\!","")).length>0&&n.push(`(\\s${e}|^${e})`)})),i=n.join("|")}return e.filter((e=>{if(l=null,l=s?new RegExp(i,"g"):new RegExp(i,"ig"),"object"==typeof e){var r=this._decomposeWhere(t,e);return void 0===r&&""!=t&&console.warn("simple-filter was unable to find a property in '"+t+"'"),l.test(r)}return"string"==typeof e?l.test(e):"number"==typeof e?l.test(e.toString()):void 0}))}_decomposeWhere(e,t){return e.split(".").reduce((function(e,t){return e&&e[t]}),t)}}};class SimpleFilter extends(SimpleFilterMixin(e)){constructor(){super()}static get tag(){return"simple-filter"}}export{SimpleFilter};