/**
@license
Copyright (c) 2017 Vaadin Ltd.
This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import{FlattenedNodesObserver as e}from"../../../@polymer/polymer/lib/utils/flattened-nodes-observer.js";import{Debouncer as r}from"../../../@polymer/polymer/lib/utils/debounce.js";import{timeOut as t}from"../../../@polymer/polymer/lib/utils/async.js";import{PolymerElement as o}from"../../../@polymer/polymer/polymer-element.js";export const DynamicColumnsMixin=l=>class DynamicColumnsMixin extends l{ready(){super.ready(),this._addNodeObserver()}_hasColumnGroups(e){for(let r=0;r<e.length;r++)if("vaadin-grid-column-group"===e[r].localName)return!0;return!1}_getChildColumns(r){return e.getFlattenedNodes(r).filter(this._isColumnElement)}_flattenColumnGroups(e){return e.map((e=>"vaadin-grid-column-group"===e.localName?this._getChildColumns(e):[e])).reduce(((e,r)=>e.concat(r)),[])}_getColumnTree(){for(var r=[],t=e.getFlattenedNodes(this).filter(this._isColumnElement);r.push(t),this._hasColumnGroups(t);)t=this._flattenColumnGroups(t);return r}_updateColumnTree(){var e=this._getColumnTree();this._arrayEquals(e,this._columnTree)||(this._columnTree=e)}_addNodeObserver(){this._observer=new e(this,(e=>{const o=e.addedNodes.filter((e=>"template"===e.localName&&e.classList.contains("row-details")))[0];o&&this._rowDetailsTemplate!==o&&(this._rowDetailsTemplate=o);const hasColumnElements=e=>e.filter(this._isColumnElement).length>0;if(hasColumnElements(e.addedNodes)||hasColumnElements(e.removedNodes)){const r=e.removedNodes.reduce(((e,r)=>e.concat(r._allCells)),[]),filterNotConnected=e=>r.filter((r=>r._content.contains(e))).length;this.__removeSorters(this._sorters.filter(filterNotConnected)),this.__removeFilters(this._filters.filter(filterNotConnected)),this._updateColumnTree()}this._debouncerCheckImports=r.debounce(this._debouncerCheckImports,t.after(2e3),this._checkImports.bind(this)),this._ensureFirstPageLoaded()}))}_arrayEquals(e,r){if(!e||!r||e.length!=r.length)return!1;for(var t=0,o=e.length;t<o;t++)if(e[t]instanceof Array&&r[t]instanceof Array){if(!this._arrayEquals(e[t],r[t]))return!1}else if(e[t]!=r[t])return!1;return!0}_checkImports(){["vaadin-grid-column-group","vaadin-grid-filter","vaadin-grid-filter-column","vaadin-grid-tree-toggle","vaadin-grid-selection-column","vaadin-grid-sort-column","vaadin-grid-sorter"].forEach((e=>{var r=this.querySelector(e);!r||r instanceof o||console.warn(`Make sure you have imported the required module for <${e}> element.`)}))}_updateFirstAndLastColumn(){Array.from(this.shadowRoot.querySelectorAll("tr")).forEach((e=>this._updateFirstAndLastColumnForRow(e)))}_updateFirstAndLastColumnForRow(e){Array.from(e.querySelectorAll('[part~="cell"]:not([part~="details-cell"])')).sort(((e,r)=>e._column._order-r._column._order)).forEach(((e,r,t)=>{this._toggleAttribute("first-column",0===r,e),this._toggleAttribute("last-column",r===t.length-1,e)}))}_isColumnElement(e){return e.nodeType===Node.ELEMENT_NODE&&/\bcolumn\b/.test(e.localName)}};