/**
@license
Copyright (c) 2017 Vaadin Ltd.
This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import{Templatizer as e}from"./vaadin-grid-templatizer.js";import{flush as t}from"../../../@polymer/polymer/lib/utils/flush.js";export const RowDetailsMixin=i=>class RowDetailsMixin extends i{static get properties(){return{detailsOpenedItems:{type:Array,value:function(){return[]}},_rowDetailsTemplate:Object,rowDetailsRenderer:Function,_detailsCells:{type:Array}}}static get observers(){return["_detailsOpenedItemsChanged(detailsOpenedItems.*, _rowDetailsTemplate, rowDetailsRenderer)","_rowDetailsTemplateOrRendererChanged(_rowDetailsTemplate, rowDetailsRenderer)"]}_rowDetailsTemplateOrRendererChanged(t,i){if(t&&i)throw new Error("You should only use either a renderer or a template for row details");if(t||i){if(t&&!t.templatizer){var s=new e;s._grid=this,s.dataHost=this.dataHost,s.template=t,t.templatizer=s}this._columnTree&&Array.from(this.$.items.children).forEach((e=>{e.querySelector("[part~=details-cell]")||(this._updateRow(e,this._columnTree[this._columnTree.length-1]),this._a11yUpdateRowDetailsOpened(e,!1)),delete e.querySelector("[part~=details-cell]")._instance})),this.detailsOpenedItems.length&&(Array.from(this.$.items.children).forEach(this._toggleDetailsCell,this),this._update())}}_detailsOpenedItemsChanged(e,t,i){"detailsOpenedItems.length"!==e.path&&e.value&&Array.from(this.$.items.children).forEach((e=>{this._toggleDetailsCell(e,e._item),this._a11yUpdateRowDetailsOpened(e,this._isDetailsOpened(e._item)),this._toggleAttribute("details-opened",this._isDetailsOpened(e._item),e)}))}_configureDetailsCell(e){e.setAttribute("part","cell details-cell"),this._toggleAttribute("frozen",!0,e)}_toggleDetailsCell(e,i){const s=e.querySelector('[part~="details-cell"]');if(!s)return;const r=!this._isDetailsOpened(i),a=!!s.hidden!==r;(s._instance||s._renderer)&&s.hidden===r||(s.hidden=r,r?e.style.removeProperty("padding-bottom"):(this.rowDetailsRenderer?(s._renderer=this.rowDetailsRenderer,s._renderer.call(this,s._content,this,{index:e.index,item:i})):this._rowDetailsTemplate&&!s._instance&&(s._instance=this._rowDetailsTemplate.templatizer.createInstance(),s._content.innerHTML="",s._content.appendChild(s._instance.root),this._updateItem(e,i)),t(),e.style.setProperty("padding-bottom",`${s.offsetHeight}px`),requestAnimationFrame((()=>this.notifyResize())))),a&&(this._updateMetrics(),this._positionItems())}_updateDetailsCellHeights(){Array.from(this.$.items.querySelectorAll('[part~="details-cell"]:not([hidden])')).forEach((e=>{e.parentElement.style.setProperty("padding-bottom",`${e.offsetHeight}px`)}))}_isDetailsOpened(e){return this.detailsOpenedItems&&-1!==this._getItemIndexInArray(e,this.detailsOpenedItems)}openItemDetails(e){this._isDetailsOpened(e)||this.push("detailsOpenedItems",e)}closeItemDetails(e){this._isDetailsOpened(e)&&this.splice("detailsOpenedItems",this._getItemIndexInArray(e,this.detailsOpenedItems),1)}_detailsOpenedInstanceChangedCallback(e,t){super._detailsOpenedInstanceChangedCallback&&super._detailsOpenedInstanceChangedCallback(e,t),t?this.openItemDetails(e.item):this.closeItemDetails(e.item)}};