define(["browser","dom","layoutManager"],function(browser,dom,layoutManager){"use strict";var mainAnimatedPages,onBeforeChange,allPages=[],currentUrls=[],selectedPageIndex=-1;function beforeAnimate(allPages,newPageIndex,oldPageIndex){for(var i=0,length=allPages.length;i<length;i++)newPageIndex===i||oldPageIndex===i||allPages[i].classList.add("hide")}function afterAnimate(allPages,newPageIndex){for(var i=0,length=allPages.length;i<length;i++)newPageIndex===i||allPages[i].classList.add("hide")}function animate(newAnimatedPage,oldAnimatedPage,transition,isBack){if(layoutManager.tv&&!browser.tv&&!browser.edge&&browser.supportsCssAnimation()&&dom.supportsEventListenerOnce()&&oldAnimatedPage){if("slide"===transition)return function(newAnimatedPage,oldAnimatedPage,transition,isBack){return new Promise(function(resolve,reject){oldAnimatedPage&&setAnimation(oldAnimatedPage,isBack?"view-slideright-r 400ms ease-in-out normal both":"view-slideleft-r 400ms ease-in-out normal both"),setAnimation(newAnimatedPage,isBack?"view-slideright 400ms ease-in-out normal both":"view-slideleft 400ms ease-in-out normal both"),dom.addEventListener(newAnimatedPage,dom.whichAnimationEvent(),resolve,{once:!0})})}(newAnimatedPage,oldAnimatedPage,0,isBack);clearAnimation(newAnimatedPage),oldAnimatedPage&&clearAnimation(oldAnimatedPage)}return Promise.resolve()}function clearAnimation(elem){setAnimation(elem,"none")}function setAnimation(elem,value){requestAnimationFrame(function(){elem.style.animation=value})}return{loadView:function(options){var selected=selectedPageIndex,previousAnimatable=-1===selected?null:allPages[selected],pageIndex=selected+1;3<=pageIndex&&(pageIndex=0);var viewHtml=options.view,properties=[];options.fullscreen&&properties.push("fullscreen");var view,currentPage=allPages[pageIndex];return mainAnimatedPages||((mainAnimatedPages=document.querySelector(".mainAnimatedPages")).innerHTML=""),currentPage?(function(view){view.dispatchEvent(new CustomEvent("viewdestroy",{cancelable:!1}))}(currentPage),currentPage.insertAdjacentHTML("beforebegin",viewHtml),view=function(elem,className){for(var node=elem.previousSibling;node;){var classList=node.classList;if(classList&&classList.contains(className))return node;node=node.previousSibling}}(currentPage,"view"),mainAnimatedPages.removeChild(currentPage)):(mainAnimatedPages.insertAdjacentHTML("beforeend",viewHtml),view=function(parent,className){for(var nodes=parent.childNodes,i=nodes.length-1;0<=i;i--){var node=nodes[i],classList=node.classList;if(classList&&classList.contains(className))return node}}(mainAnimatedPages,"view")),view.classList.add("page"),function(detail){var windowScroll=detail.windowScroll;return!(2!==windowScroll||layoutManager.tv||!browser.iOS)||!0===windowScroll}(options)&&view.classList.add("page-windowScroll"),properties.length&&view.setAttribute("data-properties",properties.join(",")),options.type&&view.setAttribute("data-type",options.type),allPages[pageIndex]=view,onBeforeChange&&onBeforeChange(view,!1,options),beforeAnimate(allPages,pageIndex,selected),animate(view,previousAnimatable,options.transition,options.isBack).then(function(){return currentUrls[selectedPageIndex=pageIndex]=options.url,!options.cancel&&previousAnimatable&&afterAnimate(allPages,pageIndex),view})},tryRestoreView:function(options){var url=options.url,index=currentUrls.indexOf(url);if(-1!==index){var animatable=allPages[index],view=animatable;if(view){if(options.cancel)return;var selected=selectedPageIndex,previousAnimatable=-1===selected?null:allPages[selected];return onBeforeChange&&onBeforeChange(view,!0,options),beforeAnimate(allPages,index,selected),animatable.classList.remove("hide"),animate(animatable,previousAnimatable,options.transition,options.isBack).then(function(){return selectedPageIndex=index,!options.cancel&&previousAnimatable&&afterAnimate(allPages,index),view})}}return Promise.reject()},setOnBeforeChange:function(fn){onBeforeChange=fn}}});