/**
@license
Copyright (c) 2017 Vaadin Ltd.
This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import{PolymerElement as t}from"../../../@polymer/polymer/polymer-element.js";import{templatize as e}from"../../../@polymer/polymer/lib/utils/templatize.js";class GridTemplatizer extends class extends t{}{static get is(){return"vaadin-grid-templatizer"}static get properties(){return{dataHost:Object,template:Object,_templateInstances:{type:Array,value:function(){return[]}},_parentPathValues:{value:function(){return{}}},_grid:Object}}static get observers(){return["_templateInstancesChanged(_templateInstances.*, _parentPathValues.*)"]}constructor(){super(),this._instanceProps={detailsOpened:!0,index:!0,item:!0,selected:!0,expanded:!0,level:!0}}createInstance(){this._ensureTemplatized();const t=new this._TemplateClass({});return this.addInstance(t),t}addInstance(t){-1===this._templateInstances.indexOf(t)&&(this._templateInstances.push(t),requestAnimationFrame((()=>this.notifyPath("_templateInstances.*",this._templateInstances))))}removeInstance(t){const e=this._templateInstances.indexOf(t);this.splice("_templateInstances",e,1)}_ensureTemplatized(){this._TemplateClass||(this._TemplateClass=e(this.template,this,{instanceProps:this._instanceProps,parentModel:!0,forwardHostProp:function(t,e){this._forwardParentProp(t,e),this._templateInstances&&this._templateInstances.forEach((s=>s.notifyPath(t,e)))},notifyInstanceProp:function(t,e,s){if("index"===e||"item"===e)return;const a=`__${e}__`;if(t[a]===s)return;t[a]=s;const n=Array.from(this._grid.$.items.children).filter((e=>this._grid._itemsEqual(e._item,t.item)))[0];n&&Array.from(n.children).forEach((t=>{t._instance&&(t._instance[a]=s,t._instance.notifyPath(e,s))}));const i="item.";if(Array.isArray(this._grid.items)&&0===e.indexOf(i)){const a=this._grid.items.indexOf(t.item),n=e.slice(5);this._grid.notifyPath(`items.${a}.${n}`,s)}const r=`_${e}InstanceChangedCallback`;this._grid&&this._grid[r]&&this._grid[r](t,s)}}))}_forwardParentProp(t,e){this._parentPathValues[t]=e,this._templateInstances.forEach((s=>s.notifyPath(t,e)))}_templateInstancesChanged(t,e){let s,a;if("_templateInstances"===t.path)s=0,a=this._templateInstances.length;else{if("_templateInstances.splices"!==t.path)return;s=t.value.index,a=t.value.addedCount}Object.keys(this._parentPathValues||{}).forEach((t=>{for(var e=s;e<s+a;e++)this._templateInstances[e].set(t,this._parentPathValues[t])}))}}customElements.define(GridTemplatizer.is,GridTemplatizer);export{GridTemplatizer as Templatizer};