/**
@license
Copyright (c) 2017 Vaadin Ltd.
This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import"../../../@polymer/polymer/polymer-legacy.js";import{microTask as e}from"../../../@polymer/polymer/lib/utils/async.js";import{FlattenedNodesObserver as i}from"../../../@polymer/polymer/lib/utils/flattened-nodes-observer.js";import{ColumnBaseMixin as t}from"./vaadin-grid-column.js";import{GridHelper as o}from"./vaadin-grid-helpers.js";import{PolymerElement as r}from"../../../@polymer/polymer/polymer-element.js";class GridColumnGroupElement extends(t(r)){static get is(){return"vaadin-grid-column-group"}static get properties(){return{_childColumns:{value:function(){return this._getChildColumns(this)}},flexGrow:{type:Number,readOnly:!0},width:{type:String,readOnly:!0},_visibleChildColumns:Array,_colSpan:Number,_rootColumns:Array}}static get observers(){return["_groupFrozenChanged(frozen, _rootColumns)","_groupHiddenChanged(hidden)","_colSpanChanged(_colSpan, _headerCell, _footerCell)","_groupOrderChanged(_order, _rootColumns)","_groupReorderStatusChanged(_reorderStatus, _rootColumns)","_groupResizableChanged(resizable, _rootColumns)"]}connectedCallback(){super.connectedCallback(),this._addNodeObserver(),this._updateFlexAndWidth()}disconnectedCallback(){super.disconnectedCallback(),this._observer&&this._observer.disconnect()}_columnPropChanged(e,i){"hidden"===e&&(this._preventHiddenSynchronization=!0,this._updateVisibleChildColumns(this._childColumns),this._preventHiddenSynchronization=!1),/flexGrow|width|hidden|_childColumns/.test(e)&&this._updateFlexAndWidth(),"frozen"===e&&(this.frozen=this.frozen||i),"lastFrozen"===e&&(this._lastFrozen=this._lastFrozen||i)}_groupOrderChanged(e,i){if(i){const t=i.slice(0);if(!e)return void t.forEach((e=>e._order=0));const r=/(0+)$/.exec(e).pop().length,n=1+~~(Math.log(i.length)/Math.LN10),d=Math.pow(10,r-n);t[0]&&t[0]._order&&t.sort(((e,i)=>e._order-i._order)),o._updateColumnOrders(t,d,e)}}_groupReorderStatusChanged(e,i){void 0!==e&&void 0!==i&&i.forEach((i=>i._reorderStatus=e))}_groupResizableChanged(e,i){void 0!==e&&void 0!==i&&i.forEach((i=>i.resizable=e))}_updateVisibleChildColumns(e){this._visibleChildColumns=Array.prototype.filter.call(e,(e=>!e.hidden)),this._colSpan=this._visibleChildColumns.length,this._updateAutoHidden()}_updateFlexAndWidth(){this._visibleChildColumns&&(this._visibleChildColumns.length?this._setWidth("calc("+Array.prototype.reduce.call(this._visibleChildColumns,((e,i)=>e+" + "+(i.width||"0px").replace("calc","")),"").substring(3)+")"):this._setWidth("0px"),this._setFlexGrow(Array.prototype.reduce.call(this._visibleChildColumns,((e,i)=>e+i.flexGrow),0)))}_groupFrozenChanged(e,i){void 0!==i&&void 0!==e&&!1!==e&&Array.from(i).forEach((i=>i.frozen=e))}_groupHiddenChanged(e){(e||this.__groupHiddenInitialized)&&this._synchronizeHidden(),this.__groupHiddenInitialized=!0}_updateAutoHidden(){const e=this._autoHidden;this._autoHidden=0===(this._visibleChildColumns||[]).length,(e||this._autoHidden)&&(this.hidden=this._autoHidden)}_synchronizeHidden(){this._childColumns&&!this._preventHiddenSynchronization&&this._childColumns.forEach((e=>e.hidden=this.hidden))}_colSpanChanged(e,i,t){i&&(i.setAttribute("colspan",e),this._grid&&this._grid._a11yUpdateCellColspan(i,e)),t&&(t.setAttribute("colspan",e),this._grid&&this._grid._a11yUpdateCellColspan(t,e))}_getChildColumns(e){return i.getFlattenedNodes(e).filter(this._isColumnElement)}_addNodeObserver(){this._observer=new i(this,(i=>{(i.addedNodes.filter(this._isColumnElement).length>0||i.removedNodes.filter(this._isColumnElement).length>0)&&(this._preventHiddenSynchronization=!0,this._rootColumns=this._getChildColumns(this),this._childColumns=this._rootColumns,this._updateVisibleChildColumns(this._childColumns),this._preventHiddenSynchronization=!1,e.run((()=>{this._grid&&this._grid._updateColumnTree&&this._grid._updateColumnTree()})))})),this._observer.flush()}_isColumnElement(e){return e.nodeType===Node.ELEMENT_NODE&&/\bcolumn\b/.test(e.localName)}}customElements.define(GridColumnGroupElement.is,GridColumnGroupElement);export{GridColumnGroupElement};