define(["visibleinviewport","browser","connectionManager","focusManager","cardBuilder"],function(visibleinviewport,browser,connectionManager,focusManager,cardBuilder){"use strict";function trimResultByIndex(result,index){var items=result.Items.slice(index);return items.length=1,{Items:items,TotalRecordCount:1}}function spotlight(itemsContainer,getItemsFn,getDefaultItemFn){(this.itemsContainer=itemsContainer).fetchData=function(){var instance=this;return instance._itemsResult?Promise.resolve(trimResultByIndex(instance._itemsResult,instance._currentIndex)):instance.getItemsFn({Limit:20}).then(function(result){return result.Items.length||(result.Items.push(instance._defaultItem),result.TotalRecordCount=1),trimResultByIndex(instance._itemsResult=result,instance._currentIndex)})}.bind(this),itemsContainer.getListOptions=function(){return{renderer:cardBuilder,options:{shape:"backdrop",multiSelect:!1,contextMenu:!1,overlayText:!0,fields:["Name"],cardClass:"wideSpotlightCard"},virtualScrollLayout:"vertical-grid"}}.bind(this),this.getItemsFn=getItemsFn,this._currentIndex=0,this._defaultItem=getDefaultItemFn()}return spotlight.prototype.resume=function(options){this.paused=!1;var instance=this;return this.itemsContainer.resume(options).then(function(){instance.startInterval(),options.autoFocus&&focusManager.autoFocus(instance.itemsContainer)})},spotlight.prototype.onInterval=function(){var itemsResult,itemsContainer,newIndex;this.paused||(itemsResult=this._itemsResult)&&(itemsContainer=this.itemsContainer,document.body.contains(itemsContainer)?visibleinviewport(itemsContainer,!1,0)&&((newIndex=this._currentIndex+1)>=itemsResult.TotalRecordCount&&(newIndex=0),this._currentIndex=newIndex,itemsContainer.refreshItems()):this.clearInterval())},spotlight.prototype.startInterval=function(){var intervalMs;this.interval||(intervalMs=browser.animate?1e4:3e4,this.interval=setInterval(this.onInterval.bind(this),intervalMs))},spotlight.prototype.clearInterval=function(){this.interval&&(clearInterval(this.interval),this.interval=null)},spotlight.prototype.pause=function(){this.paused=!0,this.clearInterval()},spotlight.prototype.destroy=function(){this.clearInterval(),this.itemsContainer=null,this._itemsResult=null,this._currentIndex=null,this._defaultItem=null},spotlight});