/**
@license
Copyright (c) 2017 Vaadin Ltd.
This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import{Base as t}from"../../../@polymer/polymer/polymer-legacy.js";export const ArrayDataProviderMixin=e=>class ArrayDataProviderMixin extends e{static get properties(){return{items:Array}}static get observers(){return["_itemsChanged(items, items.*, isAttached)"]}_itemsChanged(t,e,r){if(r){if(!Array.isArray(t))return null==t&&(this.size=0),void(this.dataProvider===this._arrayDataProvider&&(this.dataProvider=void 0));this.size=t.length,this.dataProvider=this.dataProvider||this._arrayDataProvider,this.clearCache(),this._ensureFirstPageLoaded()}}_arrayDataProvider(t,e){let r=(Array.isArray(this.items)?this.items:[]).slice(0);this._filters&&this._checkPaths(this._filters,"filtering",r)&&(r=this._filter(r)),this.size=r.length,t.sortOrders.length&&this._checkPaths(this._sorters,"sorting",r)&&(r=r.sort(this._multiSort.bind(this)));const i=t.page*t.pageSize,s=i+t.pageSize;e(r.slice(i,s),r.length)}_checkPaths(e,r,i){if(!i.length)return!1;let s=!0;for(var a in e){const o=e[a].path;if(!o||-1===o.indexOf("."))continue;const h=o.replace(/\.[^\.]*$/,"");void 0===t.get(h,i[0])&&(console.warn(`Path "${o}" used for ${r} does not exist in all of the items, ${r} is disabled.`),s=!1)}return s}_multiSort(e,r){return this._sorters.map((i=>"asc"===i.direction?this._compare(t.get(i.path,e),t.get(i.path,r)):"desc"===i.direction?this._compare(t.get(i.path,r),t.get(i.path,e)):0)).reduce(((t,e)=>t||e),0)}_normalizeEmptyValue(t){return[void 0,null].indexOf(t)>=0?"":isNaN(t)?t.toString():t}_compare(t,e){return(t=this._normalizeEmptyValue(t))<(e=this._normalizeEmptyValue(e))?-1:t>e?1:0}_filter(e){return e.filter(((e,r)=>0===this._filters.filter((r=>{const i=this._normalizeEmptyValue(t.get(r.path,e)),s=this._normalizeEmptyValue(r.value).toString().toLowerCase();return-1===i.toString().toLowerCase().indexOf(s)})).length))}};