/**
@license
Copyright (c) 2017 Vaadin Ltd.
This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import{microTask as r}from"../../../@polymer/polymer/lib/utils/async.js";import{PolymerElement as t}from"../../../@polymer/polymer/polymer-element.js";export const SortMixin=e=>class SortMixin extends e{static get properties(){return{multiSort:{type:Boolean,value:!1},_sorters:{type:Array,value:function(){return[]}},_previousSorters:{type:Array,value:function(){return[]}}}}ready(){super.ready(),this.addEventListener("sorter-changed",this._onSorterChanged),window.ShadyDOM&&r.run((()=>{const r=this.querySelectorAll("vaadin-grid-sorter");Array.from(r).forEach((r=>{r instanceof t&&r.dispatchEvent(new CustomEvent("sorter-changed",{bubbles:!0,composed:!0}))}))}))}_onSorterChanged(r){const t=r.target;r.stopPropagation(),this.__updateSorter(t),this.__applySorters()}__removeSorters(r){0!=r.length&&(this._sorters=this._sorters.filter((t=>r.indexOf(t)<0)),this.multiSort&&this.__updateSortOrders(),this.__applySorters())}__updateSortOrders(){this._sorters.forEach(((r,t)=>r._order=this._sorters.length>1?t:null),this)}__updateSorter(r){if(r.direction||-1!==this._sorters.indexOf(r))if(r._order=null,this.multiSort)this._removeArrayItem(this._sorters,r),r.direction&&this._sorters.unshift(r),this.__updateSortOrders(),this._sorters.forEach(((r,t)=>r._order=this._sorters.length>1?t:null),this);else if(r.direction){const t=this._sorters.filter((t=>t!=r));this._sorters=[r],t.forEach((r=>{r._order=null,r.direction=null}))}}__applySorters(){this.dataProvider&&this.isAttached&&JSON.stringify(this._previousSorters)!==JSON.stringify(this._mapSorters())&&this.clearCache(),this._a11yUpdateSorters(),this._previousSorters=this._mapSorters()}_mapSorters(){return this._sorters.map((r=>({path:r.path,direction:r.direction})))}_removeArrayItem(r,t){const e=r.indexOf(t);e>-1&&r.splice(e,1)}};