define(["connectionManager", "cardBuilder", "registrationServices", "appSettings", "dom", "apphost", "layoutManager", "globalize", "appRouter", "emby-button", "paper-icon-button-light", "emby-itemscontainer", "emby-scroller", "emby-linkbutton"], function(connectionManager, cardBuilder, registrationServices, appSettings, dom, appHost, layoutManager, globalize, appRouter) {
	"use strict";

	function getDefaultSection(index) {
		switch (index) {
		case 0:
			return "smalllibrarytiles";
		case 1:
			return "resume";
		case 2:
			return "resumeaudio";
		case 3:
			return "livetv";
		case 4:
			return "nextup";
		case 5:
			return "latestmedia";
		case 6:
			return "none";
		default:
			return ""
		}
	}
	function resume(elem, options) {
		for (var elems = elem.querySelectorAll(".itemsContainer"), promises = [], i = 0, length = elems.length; i < length; i++) promises.push(elems[i].resume(options));
		var promise = Promise.all(promises);
		return options && !1 === options.returnPromise ? promises[0] : promise
	}
	function getUserViews(apiClient, userId) {
		return apiClient.getUserViews({}, userId || apiClient.getCurrentUserId()).then(function(result) {
			return result.Items
		})
	}
	function getLibraryButtonItemsHtml(items) {
		return cardBuilder.getCardsHtml({
			items: items,
			showTitle: !0,
			centerText: !0,
			overlayText: !1,
			lazy: !0,
			transition: !1,
			hoverPlayButton: !1,
			sideFooter: !0,
			image: !1,
			smallSideFooter: !0,
			multiSelect: !1
		})
	}
	function getCard(img, shape) {
		var html = '<div class="card scalableCard ' + (shape = shape || "backdropCard") + " " + shape + '-scalable"><div class="cardBox"><div class="cardScalable"><div class="cardPadder cardPadder-backdrop"></div>';
		return html += '<div class="cardContent">', html += '<div class="cardImage" loading="lazy" style="background-image:url(' + img + ');"></div>', html += "</div>", html += "</div></div></div>"
	}
	function getDownloadItemsHtml(items) {
		return cardBuilder.getCardsHtml({
			items: items,
			preferThumb: "auto",
			inheritThumb: !1,
			shape: "autooverflow",
			overlayText: !1,
			showTitle: !0,
			showParentTitle: !0,
			lazy: !0,
			showDetailsMenu: !0,
			overlayPlayButton: !0,
			context: "home",
			centerText: !0,
			allowBottomPadding: !1,
			showYear: !0,
			lines: 2
		})
	}
	function getLibraryTilesHtml(items) {
		return cardBuilder.getCardsHtml({
			items: items,
			shape: "smallBackdrop",
			showTitle: !0,
			centerText: !0,
			overlayText: !1,
			lazy: !0,
			transition: !1,
			hoverPlayButton: !1
		})
	}
	function loadLibraryTiles(elem, apiClient, userSettings, index, useSmallButtons) {
		var html = "";
		html += '<div class="sectionTitleContainer sectionTitleContainer-cards">', html += '<h2 class="sectionTitle sectionTitle-cards padded-left">' + globalize.translate("HeaderMyMedia") + "</h2>", layoutManager.tv || (html += '<button type="button" is="paper-icon-button-light" class="sectionTitleIconButton btnHomeScreenSettings noautofocus"><i class="md-icon button-icon">&#xE5D3;</i></button>'), html += "</div>";
		var itemsContainerClass = "itemsContainer scrollSlider focuscontainer-x padded-left padded-right";
		!layoutManager.tv && index < 2 && (itemsContainerClass += " itemsContainer-finepointerwrap"), useSmallButtons && (itemsContainerClass += " itemsContainer-sideFooters itemsContainer-smallSideFooters"), html += '<div is="emby-scroller" class="padded-top-focusscale padded-bottom-focusscale" data-mousewheel="false" data-centerfocus="true"><div is="emby-itemscontainer" class="' + itemsContainerClass + '">', html += "</div>", html += "</div>", elem.classList.add("hide"), elem.innerHTML = html, useSmallButtons && elem.classList.add("verticalSection-extrabottompadding"), function(elem, userId, serverId) {
			var btnHomeScreenSettings = elem.querySelector(".btnHomeScreenSettings");
			btnHomeScreenSettings && btnHomeScreenSettings.addEventListener("click", function() {
				appRouter.show("settings/homescreen.html?userId=" + userId + "&serverId=" + serverId)
			})
		}(elem, apiClient.getCurrentUserId(), apiClient.serverId());
		var serverId, itemsContainer = elem.querySelector(".itemsContainer");
		itemsContainer.fetchData = (serverId = apiClient.serverId(), function() {
			var apiClient = connectionManager.getApiClient(serverId);
			return getUserViews(apiClient, apiClient.getCurrentUserId())
		}), itemsContainer.getItemsHtml = useSmallButtons ? getLibraryButtonItemsHtml : getLibraryTilesHtml, itemsContainer.parentContainer = elem
	}
	function getContinueWatchingListOptions(items) {
		return {
			renderer: cardBuilder,
			options: {
				preferThumb: !0,
				shape: "backdrop",
				overlayText: !1,
				showTitle: !0,
				showParentTitle: !0,
				lazy: !0,
				showDetailsMenu: !0,
				overlayPlayButton: !0,
				context: "home",
				centerText: !0,
				allowBottomPadding: !1,
				cardLayout: !1,
				showYear: !0,
				lines: 2,
				horizontal: !0
			},
			virtualScrollLayout: "horizontal-grid"
		}
	}
	function getContinueListeningListOptions(items) {
		return {
			renderer: cardBuilder,
			options: {
				preferThumb: !0,
				shape: "backdrop",
				overlayText: !1,
				showTitle: !0,
				showParentTitle: !0,
				lazy: !0,
				showDetailsMenu: !0,
				overlayPlayButton: !0,
				context: "home",
				centerText: !0,
				allowBottomPadding: !1,
				cardLayout: !1,
				showYear: !0,
				lines: 2,
				horizontal: !0
			},
			virtualScrollLayout: "horizontal-grid"
		}
	}
	function getOnNowItemsHtml(items) {
		return cardBuilder.getCardsHtml({
			items: items,
			preferThumb: "auto",
			inheritThumb: !1,
			shape: "autooverflow",
			centerText: !0,
			overlayText: !1,
			lines: 3,
			showTitle: !1,
			showCurrentProgramParentTitle: !0,
			showCurrentProgramTitle: !0,
			showCurrentProgramTime: !0,
			showCurrentProgramImage: !0,
			showAirDateTime: !1,
			showParentTitle: !1,
			overlayPlayButton: !0,
			defaultShape: "portrait",
			action: "programlink",
			multiSelect: !1
		})
	}
	function getNextUpItemsHtml(items) {
		return cardBuilder.getCardsHtml({
			items: items,
			preferThumb: !0,
			shape: "backdrop",
			overlayText: !1,
			showTitle: !0,
			showParentTitle: !0,
			lazy: !0,
			overlayPlayButton: !0,
			context: "home",
			centerText: !0,
			cardLayout: !1
		})
	}
	return {
		getDefaultSection: getDefaultSection,
		loadSections: function(elem, apiClient, user, userSettings) {
			for (var html = "", i = 0, length = 7; i < length; i++) html += '<div class="verticalSection section' + i + ' focusable" data-focusabletype="nearest"></div>', 0 === i && (html += '<div class="verticalSection section-downloads hide focusable" data-focusabletype="nearest"></div>', html += '<div class="verticalSection section-appinfo hide focusable" data-focusabletype="nearest"></div>');
			elem.innerHTML = html, elem.classList.add("homeSectionsContainer");
			var promises = [],
				sections = function(userSettings, sectionCount) {
					for (var sections = [], i = 0, length = sectionCount; i < length; i++) {
						var section = userSettings.get("homesection" + i) || getDefaultSection(i);
						"folders" === section && (section = getDefaultSection(0)), sections.push(section)
					}
					return sections
				}(userSettings, 7);
			for (i = 0, length = sections.length; i < length; i++) promises.push(function(page, apiClient, user, userSettings, allSections, index) {
				var section = allSections[index],
					elem = (apiClient.getCurrentUserId(), page.querySelector(".section" + index)); {
					if ("latestmedia" === section) return function(elem, apiClient, user) {
						return getUserViews(apiClient, apiClient.getCurrentUserId()).then(function(userViews) {
							elem.classList.remove("verticalSection"), elem.classList.remove("focusable"), elem.removeAttribute("data-focusabletype");
							for (var excludeViewTypes = ["playlists", "livetv", "boxsets", "channels"], i = 0, length = userViews.length; i < length; i++) {
								var frag, item = userViews[i]; - 1 === user.Configuration.LatestItemsExcludes.indexOf(item.Id) && -1 === excludeViewTypes.indexOf(item.CollectionType || []) && ((frag = document.createElement("div")).classList.add("hide"), frag.classList.add("verticalSection"), frag.classList.add("focusable"), frag.setAttribute("data-focusabletype", "nearest"), elem.appendChild(frag), function(elem, apiClient, parent) {
									var html = "";
									html += '<div class="sectionTitleContainer sectionTitleContainer-cards padded-left padded-right">', layoutManager.tv ? html += '<h2 class="sectionTitle sectionTitle-cards">' + globalize.translate("LatestFromLibrary", parent.Name) + "</h2>" : (html += '<a is="emby-sectiontitle" href="' + appRouter.getRouteUrl(parent, {
										section: "latest"
									}) + '" class="more button-link sectionTitleTextButton">', html += '<h2 class="sectionTitle sectionTitle-cards">', html += globalize.translate("LatestFromLibrary", parent.Name), html += "</h2>", html += "</a>");
									html += "</div>";
									var monitor = "music" === parent.CollectionType || "audiobooks" === parent.CollectionType ? "markplayed" : "videoplayback,markplayed";
									html += '<div data-parentid="' + parent.Id + '" is="emby-scroller" data-mousewheel="false" data-centerfocus="true" class="padded-top-focusscale padded-bottom-focusscale"><div is="emby-itemscontainer" class="itemsContainer scrollSlider focuscontainer-x padded-left padded-right" data-monitor="' + monitor + '">', html += "</div>", html += "</div>", elem.innerHTML = html;
									var itemsContainer = elem.querySelector(".itemsContainer");
									itemsContainer.fetchData = function(serverId, parentId, collectionType) {
										return function() {
											var apiClient = connectionManager.getApiClient(serverId),
												limit = 16;
											"music" === collectionType && (limit = 30);
											var options = {
												Limit: limit,
												Fields: "PrimaryImageAspectRatio,BasicSyncInfo,ProductionYear,Status,EndDate,CanDelete",
												ImageTypeLimit: 1,
												EnableImageTypes: "Primary,Backdrop,Thumb",
												ParentId: parentId
											};
											return apiClient.getLatestItems(options)
										}
									}(apiClient.serverId(), parent.Id, parent.CollectionType), itemsContainer.getItemsHtml = function(viewType) {
										return function(items) {
											return cardBuilder.getCardsHtml({
												items: items,
												shape: "autooverflow",
												preferThumb: "auto",
												showUnplayedIndicator: !1,
												showChildCountIndicator: !0,
												context: "home",
												overlayText: !1,
												centerText: !0,
												overlayPlayButton: "photos" !== viewType,
												showTitle: "photos" !== viewType,
												showYear: "movies" === viewType || "tvshows" === viewType || "musicvideos" === viewType || !viewType,
												showParentTitle: "music" === viewType || "tvshows" === viewType || "musicvideos" === viewType || !viewType,
												lines: "musicvideos" !== viewType && viewType ? 2 : 3
											})
										}
									}((parent.Type, parent.CollectionType)), itemsContainer.parentContainer = elem
								}(frag, apiClient, item))
							}
						})
					}(elem, apiClient, user);
					if ("librarytiles" === section || "smalllibrarytiles" === section || "smalllibrarytiles-automobile" === section || "librarytiles-automobile" === section) return loadLibraryTiles(elem, apiClient, 0, index);
					if ("librarybuttons" === section) return loadLibraryTiles(elem, apiClient, 0, index, !0);
					if ("resume" === section)!
					function(elem, apiClient) {
						var html = "";
						html += '<h2 class="sectionTitle sectionTitle-cards padded-left">' + globalize.translate("HeaderContinueWatching") + "</h2>", html += '<div is="emby-scroller" data-mousewheel="false" data-centerfocus="true" class="padded-top-focusscale padded-bottom-focusscale"><div is="emby-itemscontainer" data-commands="removefromresume" class="itemsContainer scrollSlider focuscontainer-x padded-left padded-right" data-monitor="videoplayback,markplayed" data-virtualscrolllayout="horizontal-grid">', html += "</div>", html += "</div>", elem.classList.add("hide"), elem.innerHTML = html;
						var itemsContainer = elem.querySelector(".itemsContainer");
						itemsContainer.fetchData = function(serverId) {
							return function(query) {
								var apiClient = connectionManager.getApiClient(serverId),
									options = Object.assign({
										Recursive: !0,
										Fields: "PrimaryImageAspectRatio,BasicSyncInfo,ProductionYear,CanDelete",
										ImageTypeLimit: 1,
										EnableImageTypes: "Primary,Backdrop,Thumb",
										MediaTypes: "Video"
									}, query || {});
								return apiClient.getResumableItems(apiClient.getCurrentUserId(), options)
							}
						}(apiClient.serverId()), itemsContainer.getListOptions = getContinueWatchingListOptions, itemsContainer.parentContainer = elem
					}(elem, apiClient);
					else if ("resumeaudio" === section)!
					function(elem, apiClient) {
						var html = "";
						html += '<h2 class="sectionTitle sectionTitle-cards padded-left">' + globalize.translate("HeaderContinueListening") + "</h2>", html += '<div is="emby-scroller" data-mousewheel="false" data-centerfocus="true" class="padded-top-focusscale padded-bottom-focusscale"><div is="emby-itemscontainer" class="itemsContainer scrollSlider focuscontainer-x padded-left padded-right" data-monitor="audioplayback,markplayed" data-virtualscrolllayout="horizontal-grid">', html += "</div>", html += "</div>", elem.classList.add("hide"), elem.innerHTML = html;
						var itemsContainer = elem.querySelector(".itemsContainer");
						itemsContainer.fetchData = function(serverId) {
							return function(query) {
								var apiClient = connectionManager.getApiClient(serverId),
									options = Object.assign({
										Recursive: !0,
										Fields: "PrimaryImageAspectRatio,BasicSyncInfo,ProductionYear,CanDelete",
										ImageTypeLimit: 1,
										EnableImageTypes: "Primary,Backdrop,Thumb",
										MediaTypes: "Audio"
									}, query || {});
								return apiClient.getResumableItems(apiClient.getCurrentUserId(), options)
							}
						}(apiClient.serverId()), itemsContainer.getListOptions = getContinueListeningListOptions, itemsContainer.parentContainer = elem
					}(elem, apiClient);
					else if ("activerecordings" === section)!
					function(elem, activeRecordingsOnly, apiClient) {
						var title = activeRecordingsOnly ? globalize.translate("HeaderActiveRecordings") : globalize.translate("HeaderLatestRecordings"),
							html = "";
						html += '<div class="sectionTitleContainer sectionTitleContainer-cards">', html += '<h2 class="sectionTitle sectionTitle-cards padded-left">' + title + "</h2>", layoutManager.tv;
						html += "</div>", html += '<div is="emby-scroller" data-mousewheel="false" data-centerfocus="true" class="padded-top-focusscale padded-bottom-focusscale"><div is="emby-itemscontainer" class="itemsContainer scrollSlider focuscontainer-x padded-left padded-right">', html += "</div>", html += "</div>", elem.classList.add("hide"), elem.innerHTML = html;
						var itemsContainer = elem.querySelector(".itemsContainer");
						itemsContainer.fetchData = function(serverId, activeRecordingsOnly) {
							return function() {
								var apiClient = connectionManager.getApiClient(serverId);
								return apiClient.getLiveTvRecordings({
									userId: apiClient.getCurrentUserId(),
									Limit: 12,
									Fields: "PrimaryImageAspectRatio,BasicSyncInfo,ProductionYear,CanDelete",
									EnableTotalRecordCount: !1,
									IsLibraryItem: !! activeRecordingsOnly && null,
									IsInProgress: !! activeRecordingsOnly || null
								})
							}
						}(apiClient.serverId(), activeRecordingsOnly), itemsContainer.getItemsHtml = function(activeRecordingsOnly) {
							return function(items) {
								return cardBuilder.getCardsHtml({
									items: items,
									shape: "autooverflow",
									showTitle: !0,
									showParentTitle: !0,
									lazy: !0,
									showDetailsMenu: !0,
									centerText: !0,
									overlayText: !1,
									showYear: !0,
									lines: 2,
									overlayPlayButton: !activeRecordingsOnly,
									preferThumb: !0,
									cardLayout: !1,
									overlayMoreButton: activeRecordingsOnly,
									action: activeRecordingsOnly ? "none" : null,
									centerPlayButton: activeRecordingsOnly
								})
							}
						}(activeRecordingsOnly), itemsContainer.parentContainer = elem
					}(elem, !0, apiClient);
					else {
						if ("nextup" !== section) return "onnow" === section || "livetv" === section ?
						function(elem, apiClient, user) {
							if (!user.Policy.EnableLiveTvAccess) return Promise.resolve();
							var promises = [];
							promises.push(registrationServices.validateFeature("livetv", {
								viewOnly: !0,
								showDialog: !1
							}).then(function() {
								return Promise.resolve(!0)
							}, function() {
								return Promise.resolve(!1)
							}));
							apiClient.getCurrentUserId();
							return promises.push(apiClient.getLiveTvChannels({
								userId: apiClient.getCurrentUserId(),
								limit: 1,
								ImageTypeLimit: 1,
								EnableTotalRecordCount: !1,
								EnableImages: !1,
								EnableUserData: !1
							})), Promise.all(promises).then(function(responses) {
								var itemsContainer, serverId, registered = responses[0],
									result = responses[1],
									html = "";
								result.Items.length && registered ? (elem.classList.remove("padded-left"), elem.classList.remove("padded-right"), elem.classList.remove("padded-bottom"), elem.classList.remove("verticalSection"), elem.classList.remove("focusable"), elem.removeAttribute("data-focusabletype"), html += '<div class="verticalSection focusable" data-focusabletype="nearest">', html += '<div class="sectionTitleContainer sectionTitleContainer-cards padded-left">', html += '<h2 class="sectionTitle sectionTitle-cards">' + globalize.translate("LiveTV") + "</h2>", html += "</div>", html += '<div is="emby-scroller" class="padded-top-focusscale padded-bottom-focusscale" data-mousewheel="false" data-centerfocus="true" data-scrollbuttons="false">', html += '<div class="scrollSlider padded-left padded-right padded-top padded-bottom focuscontainer-x flex align-items-center">', html += '<a style="margin:.5em 0 .5em .4em;" is="emby-linkbutton" href="' + appRouter.getRouteUrl("livetv", {
									serverId: apiClient.serverId(),
									section: "programs"
								}) + '" class="raised justify-content-center"><i class="md-icon button-icon button-icon-left-centered">live_tv</i><span>' + globalize.translate("Programs") + "</span></a>", html += '<a style="margin:.5em 0 .5em .8em;" is="emby-linkbutton" href="' + appRouter.getRouteUrl("livetv", {
									serverId: apiClient.serverId(),
									section: "guide"
								}) + '" class="raised justify-content-center"><i class="md-icon button-icon button-icon-left-centered">dvr</i><span>' + globalize.translate("Guide") + "</span></a>", html += '<a style="margin:.5em 0 .5em .8em;" is="emby-linkbutton" href="' + appRouter.getRouteUrl("recordedtv", {
									serverId: apiClient.serverId()
								}) + '" class="raised justify-content-center"><i class="md-icon button-icon button-icon-left-centered">folder</i><span>' + globalize.translate("Recordings") + "</span></a>", html += '<a style="margin:.5em 0 .5em .8em;" is="emby-linkbutton" href="' + appRouter.getRouteUrl("livetv", {
									serverId: apiClient.serverId(),
									section: "dvrschedule"
								}) + '" class="raised justify-content-center"><i class="md-icon button-icon button-icon-left-centered">date_range</i><span>' + globalize.translate("Schedule") + "</span></a>", html += "</div>", html += "</div>", html += "</div>", html += "</div>", html += '<div class="verticalSection focusable" data-focusabletype="nearest">', html += '<div class="sectionTitleContainer sectionTitleContainer-cards padded-left padded-right">', layoutManager.tv ? html += '<h2 class="sectionTitle sectionTitle-cards">' + globalize.translate("HeaderOnNow") + "</h2>" : (html += '<a is="emby-sectiontitle" href="' + appRouter.getRouteUrl("livetv", {
									serverId: apiClient.serverId(),
									section: "onnow"
								}) + '" class="more button-link sectionTitleTextButton">', html += '<h2 class="sectionTitle sectionTitle-cards">', html += globalize.translate("HeaderOnNow"), html += "</h2>", html += "</a>"), html += "</div>", html += '<div is="emby-scroller" data-mousewheel="false" data-centerfocus="true" class="padded-top-focusscale padded-bottom-focusscale"><div is="emby-itemscontainer" class="itemsContainer scrollSlider focuscontainer-x padded-left padded-right" data-refreshinterval="300000" data-multiselect="false">', html += "</div>", html += "</div>", html += "</div>", elem.innerHTML = html, (itemsContainer = elem.querySelector(".itemsContainer")).parentContainer = elem, itemsContainer.fetchData = (serverId = apiClient.serverId(), function() {
									var apiClient = connectionManager.getApiClient(serverId);
									return apiClient.getLiveTvChannels({
										userId: apiClient.getCurrentUserId(),
										IsAiring: !0,
										limit: 24,
										ImageTypeLimit: 1,
										EnableImageTypes: "Primary,Thumb,Backdrop",
										EnableTotalRecordCount: !1,
										Fields: "ProgramPrimaryImageAspectRatio",
										EnableUserData: !1,
										SortBy: apiClient.isMinServerVersion("4.4.0.20") ? "ChannelNumber,SortName" : null
									})
								}), itemsContainer.getItemsHtml = getOnNowItemsHtml) : result.Items.length && !registered ? (elem.classList.add("padded-left"), elem.classList.add("padded-right"), elem.classList.add("padded-bottom"), html += '<h2 class="sectionTitle">' + globalize.translate("LiveTvRequiresUnlock") + "</h2>", html += '<button is="emby-button" type="button" class="raised button-submit block btnUnlock">', html += "<span>" + globalize.translate("HeaderBecomeProjectSupporter") + "</span>", html += "</button>", elem.innerHTML = html) : (elem.classList.remove("focusable"), elem.classList.add("hide")), function(elem) {
									var btnUnlock = elem.querySelector(".btnUnlock");
									btnUnlock && btnUnlock.addEventListener("click", function(e) {
										registrationServices.validateFeature("livetv", {
											viewOnly: !0
										}).then(function() {
											dom.parentWithClass(elem, "homeSectionsContainer").dispatchEvent(new CustomEvent("settingschange", {
												cancelable: !1
											}))
										})
									})
								}(elem)
							})
						}(elem, apiClient, user) : (elem.innerHTML = "", Promise.resolve());
						!
						function(elem, apiClient) {
							var html = "";
							html += '<div class="sectionTitleContainer sectionTitleContainer-cards padded-left padded-right">', layoutManager.tv ? html += '<h2 class="sectionTitle sectionTitle-cards">' + globalize.translate("HeaderNextUp") + "</h2>" : (html += '<a is="emby-sectiontitle" href="' + appRouter.getRouteUrl("nextup", {
								serverId: apiClient.serverId()
							}) + '" class="button-link sectionTitleTextButton">', html += '<h2 class="sectionTitle sectionTitle-cards">', html += globalize.translate("HeaderNextUp"), html += "</h2>", html += "</a>");
							html += "</div>", html += '<div is="emby-scroller" data-mousewheel="false" data-centerfocus="true" class="padded-top-focusscale padded-bottom-focusscale"><div is="emby-itemscontainer" data-commands="removefromnextup" class="itemsContainer scrollSlider focuscontainer-x padded-left padded-right" data-monitor="videoplayback,markplayed">', html += "</div>", html += "</div>", elem.classList.add("hide"), elem.innerHTML = html;
							var itemsContainer = elem.querySelector(".itemsContainer");
							itemsContainer.fetchData = function(serverId) {
								return function() {
									var apiClient = connectionManager.getApiClient(serverId);
									return apiClient.getNextUpEpisodes({
										Limit: 24,
										Fields: "PrimaryImageAspectRatio,SeriesInfo,DateCreated,BasicSyncInfo,CanDelete",
										UserId: apiClient.getCurrentUserId(),
										ImageTypeLimit: 1,
										EnableImageTypes: "Primary,Backdrop,Banner,Thumb",
										EnableTotalRecordCount: !1
									})
								}
							}(apiClient.serverId()), itemsContainer.getItemsHtml = getNextUpItemsHtml, itemsContainer.parentContainer = elem
						}(elem, apiClient)
					}
				}
				return Promise.resolve()
			}(elem, apiClient, user, userSettings, sections, i)), 0 === i && (promises.push(function(elem, apiClient, user) {
				if (elem.classList.add("hide"), !appHost.supports("sync") || !user.Policy.EnableContentDownloading) return Promise.resolve();
				var html = "";
				html += '<div class="sectionTitleContainer sectionTitleContainer-cards padded-left padded-right">', html += '<a is="emby-sectiontitle" href="' + appRouter.getRouteUrl("downloads") + '" class="more button-link sectionTitleTextButton">', html += '<h2 class="sectionTitle sectionTitle-cards">', html += globalize.translate("Downloads"), html += "</h2>", html += "</a>", layoutManager.tv || (html += '<a is="emby-linkbutton" href="' + appRouter.getRouteUrl("managedownloads") + '" class="sectionTitleIconButton"><i class="md-icon">&#xE8B8;</i></a>');
				html += "</div>", html += '<div is="emby-scroller" class="padded-top-focusscale padded-bottom-focusscale" data-mousewheel="false" data-centerfocus="true"><div is="emby-itemscontainer" class="itemsContainer scrollSlider focuscontainer-x padded-left padded-right">', html += "</div>", html += "</div>", elem.innerHTML = html;
				var itemsContainer = elem.querySelector(".itemsContainer");
				itemsContainer.fetchData = function(serverId) {
					return function() {
						var apiClient = connectionManager.getApiClient(serverId);
						return apiClient.getLatestOfflineItems ? apiClient.getLatestOfflineItems({
							Limit: 20,
							Filters: "IsNotFolder"
						}) : Promise.resolve([])
					}
				}(apiClient.serverId()), itemsContainer.getItemsHtml = getDownloadItemsHtml, itemsContainer.parentContainer = elem
			}(elem.querySelector(".section-downloads"), apiClient, user)), promises.push(function(elem) {
				return elem.classList.add("hide"), function() {
					var cacheKey = "lastappinfopresent5",
						lastDatePresented = parseInt(appSettings.get(cacheKey) || "0");
					if (!lastDatePresented) return appSettings.set(cacheKey, Date.now()), Promise.resolve("");
					if (Date.now() - lastDatePresented < 1728e5) return Promise.resolve("");
					return registrationServices.validateFeature("dvr", {
						showDialog: !1,
						viewOnly: !0
					}).then(function() {
						return appSettings.set(Date.now()), ""
					}, function() {
						return appSettings.set(Date.now()), html = "", html += '<div class="sectionTitleContainer sectionTitleContainer-cards"></div>'
						var html
					})
				}().then(function(html) {
					elem.innerHTML = html, function(elem) {
						var itemsContainer = elem.querySelector(".itemsContainer");
						if (!itemsContainer) return;
						itemsContainer.addEventListener("click", function(e) {
							dom.parentWithClass(e.target, "card") && registrationServices.showPremiereInfo()
						})
					}(elem), html ? elem.classList.remove("hide") : elem.classList.add("hide")
				}), Promise.resolve()
			}(elem.querySelector(".section-appinfo"))));
			return Promise.all(promises).then(function() {
				return resume(elem, {
					refresh: !0,
					returnPromise: !1
				})
			})
		},
		destroySections: function(elem) {
			for (var elems = elem.querySelectorAll(".itemsContainer"), i = 0, length = elems.length; i < length; i++) elems[i].fetchData = null, elems[i].parentContainer = null, elems[i].getItemsHtml = null;
			elem.innerHTML = ""
		},
		pause: function(elem) {
			for (var elems = elem.querySelectorAll(".itemsContainer"), i = 0, length = elems.length; i < length; i++) elems[i].pause()
		},
		resume: resume
	}
});
