/**
@license
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at
http://polymer.github.io/LICENSE.txt The complete set of authors may be found at
http://polymer.github.io/AUTHORS.txt The complete set of contributors may be
found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by Google as
part of the polymer project is also subject to an additional IP rights grant
found at http://polymer.github.io/PATENTS.txt
*/
import"../polymer/polymer-legacy.js";import"../iron-a11y-keys-behavior/iron-a11y-keys-behavior.js";import{IronResizableBehavior as t}from"../iron-resizable-behavior/iron-resizable-behavior.js";import{IronScrollTargetBehavior as i}from"../iron-scroll-target-behavior/iron-scroll-target-behavior.js";import{OptionalMutableDataBehavior as e}from"../polymer/lib/legacy/mutable-data-behavior.js";import{Polymer as s}from"../polymer/lib/legacy/polymer-fn.js";import{dom as h}from"../polymer/lib/legacy/polymer.dom.js";import{Templatizer as l}from"../polymer/lib/legacy/templatizer-behavior.js";import{animationFrame as r,idlePeriod as o,microTask as n}from"../polymer/lib/utils/async.js";import{Debouncer as a}from"../polymer/lib/utils/debounce.js";import{enqueueDebouncer as c,flush as _}from"../polymer/lib/utils/flush.js";import{html as d}from"../polymer/lib/utils/html-tag.js";import{matches as u,translate as p}from"../polymer/lib/utils/path.js";import{TemplateInstanceBase as m}from"../polymer/lib/utils/templatize.js";var f=navigator.userAgent.match(/iP(?:hone|ad;(?: U;)? CPU) OS (\d+)/),y=f&&f[1]>=8,I="-10000px",g=-100;s({_template:d`
    <style>
      :host {
        display: block;
      }

      @media only screen and (-webkit-max-device-pixel-ratio: 1) {
        :host {
          will-change: transform;
        }
      }

      #items {
        @apply --iron-list-items-container;
        position: relative;
      }

      :host(:not([grid])) #items > ::slotted(*) {
        width: 100%;
      }

      #items > ::slotted(*) {
        box-sizing: border-box;
        margin: 0;
        position: absolute;
        top: 0;
        will-change: transform;
      }
    </style>

    <array-selector id="selector" items="{{items}}" selected="{{selectedItems}}" selected-item="{{selectedItem}}"></array-selector>

    <div id="items">
      <slot></slot>
    </div>
`,is:"iron-list",properties:{items:{type:Array},as:{type:String,value:"item"},indexAs:{type:String,value:"index"},selectedAs:{type:String,value:"selected"},grid:{type:Boolean,value:!1,reflectToAttribute:!0,observer:"_gridChanged"},selectionEnabled:{type:Boolean,value:!1},selectedItem:{type:Object,notify:!0},selectedItems:{type:Object,notify:!0},multiSelection:{type:Boolean,value:!1},scrollOffset:{type:Number,value:0}},observers:["_itemsChanged(items.*)","_selectionEnabledChanged(selectionEnabled)","_multiSelectionChanged(multiSelection)","_setOverflow(scrollTarget, scrollOffset)"],behaviors:[l,t,i,e],_ratio:.5,_scrollerPaddingTop:0,_scrollPosition:0,_physicalSize:0,_physicalAverage:0,_physicalAverageCount:0,_physicalTop:0,_virtualCount:0,_estScrollHeight:0,_scrollHeight:0,_viewportHeight:0,_viewportWidth:0,_physicalItems:null,_physicalSizes:null,_firstVisibleIndexVal:null,_lastVisibleIndexVal:null,_maxPages:2,_focusedItem:null,_focusedVirtualIndex:-1,_focusedPhysicalIndex:-1,_offscreenFocusedItem:null,_focusBackfillItem:null,_itemsPerRow:1,_itemWidth:0,_rowHeight:0,_templateCost:0,_parentModel:!0,get _physicalBottom(){return this._physicalTop+this._physicalSize},get _scrollBottom(){return this._scrollPosition+this._viewportHeight},get _virtualEnd(){return this._virtualStart+this._physicalCount-1},get _hiddenContentSize(){return(this.grid?this._physicalRows*this._rowHeight:this._physicalSize)-this._viewportHeight},get _itemsParent(){return h(h(this._userTemplate).parentNode)},get _maxScrollTop(){return this._estScrollHeight-this._viewportHeight+this._scrollOffset},get _maxVirtualStart(){var t=this._convertIndexToCompleteRow(this._virtualCount);return Math.max(0,t-this._physicalCount)},set _virtualStart(t){t=this._clamp(t,0,this._maxVirtualStart),this.grid&&(t-=t%this._itemsPerRow),this._virtualStartVal=t},get _virtualStart(){return this._virtualStartVal||0},set _physicalStart(t){(t%=this._physicalCount)<0&&(t=this._physicalCount+t),this.grid&&(t-=t%this._itemsPerRow),this._physicalStartVal=t},get _physicalStart(){return this._physicalStartVal||0},get _physicalEnd(){return(this._physicalStart+this._physicalCount-1)%this._physicalCount},set _physicalCount(t){this._physicalCountVal=t},get _physicalCount(){return this._physicalCountVal||0},get _optPhysicalSize(){return 0===this._viewportHeight?1/0:this._viewportHeight*this._maxPages},get _isVisible(){return Boolean(this.offsetWidth||this.offsetHeight)},get firstVisibleIndex(){var t=this._firstVisibleIndexVal;if(null==t){var i=this._physicalTop+this._scrollOffset;t=this._iterateItems((function(t,e){return(i+=this._getPhysicalSizeIncrement(t))>this._scrollPosition?this.grid?e-e%this._itemsPerRow:e:this.grid&&this._virtualCount-1===e?e-e%this._itemsPerRow:void 0}))||0,this._firstVisibleIndexVal=t}return t},get lastVisibleIndex(){var t=this._lastVisibleIndexVal;if(null==t){if(this.grid)t=Math.min(this._virtualCount,this.firstVisibleIndex+this._estRowsInView*this._itemsPerRow-1);else{var i=this._physicalTop+this._scrollOffset;this._iterateItems((function(e,s){i<this._scrollBottom&&(t=s),i+=this._getPhysicalSizeIncrement(e)}))}this._lastVisibleIndexVal=t}return t},get _defaultScrollTarget(){return this},get _virtualRowCount(){return Math.ceil(this._virtualCount/this._itemsPerRow)},get _estRowsInView(){return Math.ceil(this._viewportHeight/this._rowHeight)},get _physicalRows(){return Math.ceil(this._physicalCount/this._itemsPerRow)},get _scrollOffset(){return this._scrollerPaddingTop+this.scrollOffset},ready:function(){this.addEventListener("focus",this._didFocus.bind(this),!0)},attached:function(){this._debounce("_render",this._render,r),this.listen(this,"iron-resize","_resizeHandler"),this.listen(this,"keydown","_keydownHandler")},detached:function(){this.unlisten(this,"iron-resize","_resizeHandler"),this.unlisten(this,"keydown","_keydownHandler")},_setOverflow:function(t){this.style.webkitOverflowScrolling=t===this?"touch":"",this.style.overflowY=t===this?"auto":"",this._lastVisibleIndexVal=null,this._firstVisibleIndexVal=null,this._debounce("_render",this._render,r)},updateViewportBoundaries:function(){var t=window.getComputedStyle(this);this._scrollerPaddingTop=this.scrollTarget===this?0:parseInt(t["padding-top"],10),this._isRTL=Boolean("rtl"===t.direction),this._viewportWidth=this.$.items.offsetWidth,this._viewportHeight=this._scrollTargetHeight,this.grid&&this._updateGridMetrics()},_scrollHandler:function(){var t=Math.max(0,Math.min(this._maxScrollTop,this._scrollTop)),i=t-this._scrollPosition,e=i>=0;if(this._scrollPosition=t,this._firstVisibleIndexVal=null,this._lastVisibleIndexVal=null,Math.abs(i)>this._physicalSize&&this._physicalSize>0){i-=this._scrollOffset;var s=Math.round(i/this._physicalAverage)*this._itemsPerRow;this._virtualStart=this._virtualStart+s,this._physicalStart=this._physicalStart+s,this._physicalTop=Math.min(Math.floor(this._virtualStart/this._itemsPerRow)*this._physicalAverage,this._scrollPosition),this._update()}else if(this._physicalCount>0){var h=this._getReusables(e);e?(this._physicalTop=h.physicalTop,this._virtualStart=this._virtualStart+h.indexes.length,this._physicalStart=this._physicalStart+h.indexes.length):(this._virtualStart=this._virtualStart-h.indexes.length,this._physicalStart=this._physicalStart-h.indexes.length),this._update(h.indexes,e?null:h.indexes),this._debounce("_increasePoolIfNeeded",this._increasePoolIfNeeded.bind(this,0),n)}},_getReusables:function(t){var i,e,s,h=[],l=this._hiddenContentSize*this._ratio,r=this._virtualStart,o=this._virtualEnd,n=this._physicalCount,a=this._physicalTop+this._scrollOffset,c=this._physicalBottom+this._scrollOffset,_=this._scrollPosition,d=this._scrollBottom;for(t?(i=this._physicalStart,this._physicalEnd,e=_-a):(i=this._physicalEnd,this._physicalStart,e=c-d);e-=s=this._getPhysicalSizeIncrement(i),!(h.length>=n||e<=l);)if(t){if(o+h.length+1>=this._virtualCount)break;if(a+s>=_-this._scrollOffset)break;h.push(i),a+=s,i=(i+1)%n}else{if(r-h.length<=0)break;if(a+this._physicalSize-s<=d)break;h.push(i),a-=s,i=0===i?n-1:i-1}return{indexes:h,physicalTop:a-this._scrollOffset}},_update:function(t,i){if(!(t&&0===t.length||0===this._physicalCount)){if(this._manageFocus(),this._assignModels(t),this._updateMetrics(t),i)for(;i.length;){var e=i.pop();this._physicalTop-=this._getPhysicalSizeIncrement(e)}this._positionItems(),this._updateScrollerSize()}},_createPool:function(t){var i,e;this._ensureTemplatized();var s=new Array(t);for(i=0;i<t;i++)e=this.stamp(null),s[i]=e.root.querySelector("*"),this._itemsParent.appendChild(e.root);return s},_isClientFull:function(){return 0!=this._scrollBottom&&this._physicalBottom-1>=this._scrollBottom&&this._physicalTop<=this._scrollPosition},_increasePoolIfNeeded:function(t){var i=this._clamp(this._physicalCount+t,3,this._virtualCount-this._virtualStart);if(i=this._convertIndexToCompleteRow(i),this.grid){var e=i%this._itemsPerRow;e&&i-e<=this._physicalCount&&(i+=this._itemsPerRow),i-=e}var s=i-this._physicalCount,h=Math.round(.5*this._physicalCount);if(!(s<0)){if(s>0){var l=window.performance.now();[].push.apply(this._physicalItems,this._createPool(s));for(var r=0;r<s;r++)this._physicalSizes.push(0);this._physicalCount=this._physicalCount+s,this._physicalStart>this._physicalEnd&&this._isIndexRendered(this._focusedVirtualIndex)&&this._getPhysicalIndex(this._focusedVirtualIndex)<this._physicalEnd&&(this._physicalStart=this._physicalStart+s),this._update(),this._templateCost=(window.performance.now()-l)/s,h=Math.round(.5*this._physicalCount)}this._virtualEnd>=this._virtualCount-1||0===h||(this._isClientFull()?this._physicalSize<this._optPhysicalSize&&this._debounce("_increasePoolIfNeeded",this._increasePoolIfNeeded.bind(this,this._clamp(Math.round(50/this._templateCost),1,h)),o):this._debounce("_increasePoolIfNeeded",this._increasePoolIfNeeded.bind(this,h),n))}},_render:function(){if(this.isAttached&&this._isVisible)if(0!==this._physicalCount){var t=this._getReusables(!0);this._physicalTop=t.physicalTop,this._virtualStart=this._virtualStart+t.indexes.length,this._physicalStart=this._physicalStart+t.indexes.length,this._update(t.indexes),this._update(),this._increasePoolIfNeeded(0)}else this._virtualCount>0&&(this.updateViewportBoundaries(),this._increasePoolIfNeeded(3))},_ensureTemplatized:function(){if(!this.ctor){this._userTemplate=this.queryEffectiveChildren("template"),this._userTemplate||console.warn("iron-list requires a template to be provided in light-dom");var t={__key__:!0};t[this.as]=!0,t[this.indexAs]=!0,t[this.selectedAs]=!0,t.tabIndex=!0,this._instanceProps=t,this.templatize(this._userTemplate,this.mutableData)}},_gridChanged:function(t,i){void 0!==i&&(this.notifyResize(),_(),t&&this._updateGridMetrics())},_itemsChanged:function(t){if("items"===t.path)this._virtualStart=0,this._physicalTop=0,this._virtualCount=this.items?this.items.length:0,this._physicalIndexForKey={},this._firstVisibleIndexVal=null,this._lastVisibleIndexVal=null,this._physicalCount=this._physicalCount||0,this._physicalItems=this._physicalItems||[],this._physicalSizes=this._physicalSizes||[],this._physicalStart=0,this._scrollTop>this._scrollOffset&&this._resetScrollPosition(0),this._removeFocusedItem(),this._debounce("_render",this._render,r);else if("items.splices"===t.path){if(this._adjustVirtualIndex(t.value.indexSplices),this._virtualCount=this.items?this.items.length:0,t.value.indexSplices.some((function(t){return t.addedCount>0||t.removed.length>0}))){var i=this._getActiveElement();this.contains(i)&&i.blur()}var e=t.value.indexSplices.some((function(t){return t.index+t.addedCount>=this._virtualStart&&t.index<=this._virtualEnd}),this);this._isClientFull()&&!e||this._debounce("_render",this._render,r)}else"items.length"!==t.path&&this._forwardItemPath(t.path,t.value)},_forwardItemPath:function(t,i){var e,s,h,l=(t=t.slice(6)).indexOf(".");-1===l&&(l=t.length);var r=this.modelForElement(this._offscreenFocusedItem),o=parseInt(t.substring(0,l),10);(e=this._isIndexRendered(o))?(s=this._getPhysicalIndex(o),h=this.modelForElement(this._physicalItems[s])):r&&(h=r),h&&h[this.indexAs]===o&&(t=t.substring(l+1),t=this.as+(t?"."+t:""),h._setPendingPropertyOrPath(t,i,!1,!0),h._flushProperties&&h._flushProperties(),e&&(this._updateMetrics([s]),this._positionItems(),this._updateScrollerSize()))},_adjustVirtualIndex:function(t){t.forEach((function(t){if(t.removed.forEach(this._removeItem,this),t.index<this._virtualStart){var i=Math.max(t.addedCount-t.removed.length,t.index-this._virtualStart);this._virtualStart=this._virtualStart+i,this._focusedVirtualIndex>=0&&(this._focusedVirtualIndex=this._focusedVirtualIndex+i)}}),this)},_removeItem:function(t){this.$.selector.deselect(t),this._focusedItem&&this.modelForElement(this._focusedItem)[this.as]===t&&this._removeFocusedItem()},_iterateItems:function(t,i){var e,s,h,l;if(2===arguments.length&&i){for(l=0;l<i.length;l++)if(e=i[l],s=this._computeVidx(e),null!=(h=t.call(this,e,s)))return h}else{for(e=this._physicalStart,s=this._virtualStart;e<this._physicalCount;e++,s++)if(null!=(h=t.call(this,e,s)))return h;for(e=0;e<this._physicalStart;e++,s++)if(null!=(h=t.call(this,e,s)))return h}},_computeVidx:function(t){return t>=this._physicalStart?this._virtualStart+(t-this._physicalStart):this._virtualStart+(this._physicalCount-this._physicalStart)+t},_assignModels:function(t){this._iterateItems((function(t,i){var e=this._physicalItems[t],s=this.items&&this.items[i];if(null!=s){var h=this.modelForElement(e);h.__key__=null,this._forwardProperty(h,this.as,s),this._forwardProperty(h,this.selectedAs,this.$.selector.isSelected(s)),this._forwardProperty(h,this.indexAs,i),this._forwardProperty(h,"tabIndex",this._focusedVirtualIndex===i?0:-1),this._physicalIndexForKey[h.__key__]=t,h._flushProperties&&h._flushProperties(!0),e.removeAttribute("hidden")}else e.setAttribute("hidden","")}),t)},_updateMetrics:function(t){_();var i=0,e=0,s=this._physicalAverageCount,h=this._physicalAverage;this._iterateItems((function(t,s){e+=this._physicalSizes[t],this._physicalSizes[t]=this._physicalItems[t].offsetHeight,i+=this._physicalSizes[t],this._physicalAverageCount+=this._physicalSizes[t]?1:0}),t),this.grid?(this._updateGridMetrics(),this._physicalSize=Math.ceil(this._physicalCount/this._itemsPerRow)*this._rowHeight):(e=1===this._itemsPerRow?e:Math.ceil(this._physicalCount/this._itemsPerRow)*this._rowHeight,this._physicalSize=this._physicalSize+i-e,this._itemsPerRow=1),this._physicalAverageCount!==s&&(this._physicalAverage=Math.round((h*s+i)/this._physicalAverageCount))},_updateGridMetrics:function(){this._itemWidth=this._physicalCount>0?this._physicalItems[0].getBoundingClientRect().width:200,this._rowHeight=this._physicalCount>0?this._physicalItems[0].offsetHeight:200,this._itemsPerRow=this._itemWidth?Math.floor(this._viewportWidth/this._itemWidth):this._itemsPerRow},_positionItems:function(){this._adjustScrollPosition();var t=this._physicalTop;if(this.grid){var i=this._itemsPerRow*this._itemWidth,e=(this._viewportWidth-i)/2;this._iterateItems((function(i,s){var h=s%this._itemsPerRow,l=Math.floor(h*this._itemWidth+e);this._isRTL&&(l*=-1),this.translate3d(l+"px",t+"px",0,this._physicalItems[i]),this._shouldRenderNextRow(s)&&(t+=this._rowHeight)}))}else{const i=[];this._iterateItems((function(e,s){const h=this._physicalItems[e];this.translate3d(0,t+"px",0,h),t+=this._physicalSizes[e];const l=h.id;l&&i.push(l)})),i.length&&this.setAttribute("aria-owns",i.join(" "))}},_getPhysicalSizeIncrement:function(t){return this.grid?this._computeVidx(t)%this._itemsPerRow!=this._itemsPerRow-1?0:this._rowHeight:this._physicalSizes[t]},_shouldRenderNextRow:function(t){return t%this._itemsPerRow==this._itemsPerRow-1},_adjustScrollPosition:function(){var t=0===this._virtualStart?this._physicalTop:Math.min(this._scrollPosition+this._physicalTop,0);if(0!==t){this._physicalTop=this._physicalTop-t;var i=this._scrollPosition;!y&&i>0&&this._resetScrollPosition(i-t)}},_resetScrollPosition:function(t){this.scrollTarget&&t>=0&&(this._scrollTop=t,this._scrollPosition=this._scrollTop)},_updateScrollerSize:function(t){this.grid?this._estScrollHeight=this._virtualRowCount*this._rowHeight:this._estScrollHeight=this._physicalBottom+Math.max(this._virtualCount-this._physicalCount-this._virtualStart,0)*this._physicalAverage,((t=(t=(t=t||0===this._scrollHeight)||this._scrollPosition>=this._estScrollHeight-this._physicalSize)||this.grid&&this.$.items.style.height<this._estScrollHeight)||Math.abs(this._estScrollHeight-this._scrollHeight)>=this._viewportHeight)&&(this.$.items.style.height=this._estScrollHeight+"px",this._scrollHeight=this._estScrollHeight)},scrollToItem:function(t){return this.scrollToIndex(this.items.indexOf(t))},scrollToIndex:function(t){if(!("number"!=typeof t||t<0||t>this.items.length-1)&&(_(),0!==this._physicalCount)){t=this._clamp(t,0,this._virtualCount-1),(!this._isIndexRendered(t)||t>=this._maxVirtualStart)&&(this._virtualStart=this.grid?t-2*this._itemsPerRow:t-1),this._manageFocus(),this._assignModels(),this._updateMetrics(),this._physicalTop=Math.floor(this._virtualStart/this._itemsPerRow)*this._physicalAverage;for(var i=this._physicalStart,e=this._virtualStart,s=0,h=this._hiddenContentSize;e<t&&s<=h;)s+=this._getPhysicalSizeIncrement(i),i=(i+1)%this._physicalCount,e++;this._updateScrollerSize(!0),this._positionItems(),this._resetScrollPosition(this._physicalTop+this._scrollOffset+s),this._increasePoolIfNeeded(0),this._firstVisibleIndexVal=null,this._lastVisibleIndexVal=null}},_resetAverage:function(){this._physicalAverage=0,this._physicalAverageCount=0},_resizeHandler:function(){this._debounce("_render",(function(){this._firstVisibleIndexVal=null,this._lastVisibleIndexVal=null,this._isVisible?(this.updateViewportBoundaries(),this.toggleScrollListener(!0),this._resetAverage(),this._render()):this.toggleScrollListener(!1)}),r)},selectItem:function(t){return this.selectIndex(this.items.indexOf(t))},selectIndex:function(t){if(!(t<0||t>=this._virtualCount)){if(!this.multiSelection&&this.selectedItem&&this.clearSelection(),this._isIndexRendered(t)){var i=this.modelForElement(this._physicalItems[this._getPhysicalIndex(t)]);i&&(i[this.selectedAs]=!0),this.updateSizeForIndex(t)}this.$.selector.selectIndex(t)}},deselectItem:function(t){return this.deselectIndex(this.items.indexOf(t))},deselectIndex:function(t){if(!(t<0||t>=this._virtualCount)){if(this._isIndexRendered(t))this.modelForElement(this._physicalItems[this._getPhysicalIndex(t)])[this.selectedAs]=!1,this.updateSizeForIndex(t);this.$.selector.deselectIndex(t)}},toggleSelectionForItem:function(t){return this.toggleSelectionForIndex(this.items.indexOf(t))},toggleSelectionForIndex:function(t){(this.$.selector.isIndexSelected?this.$.selector.isIndexSelected(t):this.$.selector.isSelected(this.items[t]))?this.deselectIndex(t):this.selectIndex(t)},clearSelection:function(){this._iterateItems((function(t,i){this.modelForElement(this._physicalItems[t])[this.selectedAs]=!1})),this.$.selector.clearSelection()},_selectionEnabledChanged:function(t){(t?this.listen:this.unlisten).call(this,this,"tap","_selectionHandler")},_selectionHandler:function(t){var i=this.modelForElement(t.target);if(i){var e,s,l=h(t).path[0],r=this._getActiveElement(),o=this._physicalItems[this._getPhysicalIndex(i[this.indexAs])];"input"!==l.localName&&"button"!==l.localName&&"select"!==l.localName&&(e=i.tabIndex,i.tabIndex=g,s=r?r.tabIndex:-1,i.tabIndex=e,r&&o!==r&&o.contains(r)&&s!==g||this.toggleSelectionForItem(i[this.as]))}},_multiSelectionChanged:function(t){this.clearSelection(),this.$.selector.multi=t},updateSizeForItem:function(t){return this.updateSizeForIndex(this.items.indexOf(t))},updateSizeForIndex:function(t){return this._isIndexRendered(t)?(this._updateMetrics([this._getPhysicalIndex(t)]),this._positionItems(),null):null},_manageFocus:function(){var t=this._focusedVirtualIndex;t>=0&&t<this._virtualCount?this._isIndexRendered(t)?this._restoreFocusedItem():this._createFocusBackfillItem():this._virtualCount>0&&this._physicalCount>0&&(this._focusedPhysicalIndex=this._physicalStart,this._focusedVirtualIndex=this._virtualStart,this._focusedItem=this._physicalItems[this._physicalStart])},_convertIndexToCompleteRow:function(t){return this._itemsPerRow=this._itemsPerRow||1,this.grid?Math.ceil(t/this._itemsPerRow)*this._itemsPerRow:t},_isIndexRendered:function(t){return t>=this._virtualStart&&t<=this._virtualEnd},_isIndexVisible:function(t){return t>=this.firstVisibleIndex&&t<=this.lastVisibleIndex},_getPhysicalIndex:function(t){return(this._physicalStart+(t-this._virtualStart))%this._physicalCount},focusItem:function(t){this._focusPhysicalItem(t)},_focusPhysicalItem:function(t){if(!(t<0||t>=this._virtualCount)){this._restoreFocusedItem(),this._isIndexRendered(t)||this.scrollToIndex(t);var i,e=this._physicalItems[this._getPhysicalIndex(t)],s=this.modelForElement(e);s.tabIndex=g,e.tabIndex===g&&(i=e),i||(i=h(e).querySelector('[tabindex="-100"]')),s.tabIndex=0,this._focusedVirtualIndex=t,i&&i.focus()}},_removeFocusedItem:function(){this._offscreenFocusedItem&&this._itemsParent.removeChild(this._offscreenFocusedItem),this._offscreenFocusedItem=null,this._focusBackfillItem=null,this._focusedItem=null,this._focusedVirtualIndex=-1,this._focusedPhysicalIndex=-1},_createFocusBackfillItem:function(){var t=this._focusedPhysicalIndex;if(!(this._offscreenFocusedItem||this._focusedVirtualIndex<0)){if(!this._focusBackfillItem){var i=this.stamp(null);this._focusBackfillItem=i.root.querySelector("*"),this._itemsParent.appendChild(i.root)}this._offscreenFocusedItem=this._physicalItems[t],this.modelForElement(this._offscreenFocusedItem).tabIndex=0,this._physicalItems[t]=this._focusBackfillItem,this._focusedPhysicalIndex=t,this.translate3d(0,I,0,this._offscreenFocusedItem)}},_restoreFocusedItem:function(){if(this._offscreenFocusedItem&&!(this._focusedVirtualIndex<0)){this._assignModels();var t=this._focusedPhysicalIndex=this._getPhysicalIndex(this._focusedVirtualIndex),i=this._physicalItems[t];if(i){var e=this.modelForElement(i),s=this.modelForElement(this._offscreenFocusedItem);e[this.as]===s[this.as]?(this._focusBackfillItem=i,e.tabIndex=-1,this._physicalItems[t]=this._offscreenFocusedItem,this.translate3d(0,I,0,this._focusBackfillItem)):(this._removeFocusedItem(),this._focusBackfillItem=null),this._offscreenFocusedItem=null}}},_didFocus:function(t){var i=this.modelForElement(t.target),e=this.modelForElement(this._focusedItem),s=null!==this._offscreenFocusedItem,h=this._focusedVirtualIndex;i&&(e===i?this._isIndexVisible(h)||this.scrollToIndex(h):(this._restoreFocusedItem(),e&&(e.tabIndex=-1),i.tabIndex=0,h=i[this.indexAs],this._focusedVirtualIndex=h,this._focusedPhysicalIndex=this._getPhysicalIndex(h),this._focusedItem=this._physicalItems[this._focusedPhysicalIndex],s&&!this._offscreenFocusedItem&&this._update()))},_keydownHandler:function(t){switch(t.keyCode){case 40:this._focusedVirtualIndex<this._virtualCount-1&&t.preventDefault(),this._focusPhysicalItem(this._focusedVirtualIndex+(this.grid?this._itemsPerRow:1));break;case 39:this.grid&&this._focusPhysicalItem(this._focusedVirtualIndex+(this._isRTL?-1:1));break;case 38:this._focusedVirtualIndex>0&&t.preventDefault(),this._focusPhysicalItem(this._focusedVirtualIndex-(this.grid?this._itemsPerRow:1));break;case 37:this.grid&&this._focusPhysicalItem(this._focusedVirtualIndex+(this._isRTL?1:-1));break;case 13:this._focusPhysicalItem(this._focusedVirtualIndex),this.selectionEnabled&&this._selectionHandler(t)}},_clamp:function(t,i,e){return Math.min(e,Math.max(i,t))},_debounce:function(t,i,e){this._debouncers=this._debouncers||{},this._debouncers[t]=a.debounce(this._debouncers[t],e,i.bind(this)),c(this._debouncers[t])},_forwardProperty:function(t,i,e){t._setPendingProperty(i,e)},_forwardHostPropV2:function(t,i){(this._physicalItems||[]).concat([this._offscreenFocusedItem,this._focusBackfillItem]).forEach((function(e){e&&this.modelForElement(e).forwardHostProp(t,i)}),this)},_notifyInstancePropV2:function(t,i,e){if(u(this.as,i)){var s=t[this.indexAs];i==this.as&&(this.items[s]=e),this.notifyPath(p(this.as,"items."+s,i),e)}},_getStampedChildren:function(){return this._physicalItems},_forwardInstancePath:function(t,i,e){0===i.indexOf(this.as+".")&&this.notifyPath("items."+t.__key__+"."+i.slice(this.as.length+1),e)},_forwardParentPath:function(t,i){(this._physicalItems||[]).concat([this._offscreenFocusedItem,this._focusBackfillItem]).forEach((function(e){e&&this.modelForElement(e).notifyPath(t,i)}),this)},_forwardParentProp:function(t,i){(this._physicalItems||[]).concat([this._offscreenFocusedItem,this._focusBackfillItem]).forEach((function(e){e&&(this.modelForElement(e)[t]=i)}),this)},_getActiveElement:function(){var t=this._itemsParent.node.domHost;return h(t?t.root:document).activeElement}});