define(["appSettings", "loading", "apphost", "iapManager", "events", "shell", "globalize", "dialogHelper", "connectionManager", "layoutManager", "emby-button", "emby-linkbutton"], function(appSettings, loading, appHost, iapManager, events, shell, globalize, dialogHelper, connectionManager, layoutManager) {
"use strict";
function alertText(options) {
return new Promise(function(resolve, reject) {
require(["alert"], function(alert) {
alert(options).then(resolve, reject)
})
})
}
function showInAppPurchaseInfo(subscriptionOptions, unlockableProductInfo, dialogOptions) {
return new Promise(function(resolve, reject) {
require(["listViewStyle", "formDialogStyle"], function() {
showInAppPurchaseElement(subscriptionOptions, unlockableProductInfo, dialogOptions, resolve, reject), currentDisplayingResolve = resolve
})
})
}
function showPeriodicMessage(feature, settingsKey) {
return new Promise(function(resolve, reject) {
require(["listViewStyle", "emby-button", "formDialogStyle"], function() {
var dlg = dialogHelper.createDialog({
size: layoutManager.tv ? "fullscreen" : "fullscreen-border",
removeOnClose: !0,
scrollY: !1
});
dlg.classList.add("formDialog");
var html = "";
var seconds = 11;
html += '
", html += '", html += "", html += "", html += "", dlg.innerHTML = html;
var i, length, isRejected = !0,
timeTextInterval = setInterval(function() {
seconds -= 1, seconds <= 0 ? (dlg.querySelector(".continueTimeText").classList.add("hide"), dlg.querySelector(".btnContinue").classList.remove("hide")) : dlg.querySelector(".continueTimeText").innerHTML = globalize.translate("sharedcomponents#ContinueInSecondsValue", seconds)
}, 1e3),
btnPurchases = dlg.querySelectorAll(".buttonPremiereInfo");
for (i = 0, length = btnPurchases.length; i < length; i++) btnPurchases[i].addEventListener("click", showExternalPremiereInfo);
layoutManager.tv && centerFocus(dlg.querySelector(".formDialogContent"), !1, !0), dlg.addEventListener("close", function(e) {
clearInterval(timeTextInterval), layoutManager.tv && centerFocus(dlg.querySelector(".formDialogContent"), !1, !1), isRejected ? reject() : (appSettings.set(settingsKey, (new Date).getTime()), resolve())
}), dlg.querySelector(".btnContinue").addEventListener("click", function() {
isRejected = !1, dialogHelper.close(dlg)
}), dlg.querySelector(".btnGetPremiere").addEventListener("click", showPremiereInfo), dialogHelper.open(dlg);
var onCancelClick = function() {
dialogHelper.close(dlg)
},
elems = dlg.querySelectorAll(".btnCancelSupporterInfo");
for (i = 0, length = elems.length; i < length; i++) elems[i].addEventListener("click", onCancelClick)
})
})
}
function showPeriodicMessageIfNeeded(feature) {
if ("playback" !== feature) return Promise.resolve();
var intervalMs = iapManager.getPeriodicMessageIntervalMs(feature);
if (intervalMs <= 0) return Promise.resolve();
var settingsKey = "periodicmessage11-" + feature,
lastMessage = parseInt(appSettings.get(settingsKey) || "0");
if (!lastMessage) return appSettings.set(settingsKey, (new Date).getTime()), Promise.resolve();
if ((new Date).getTime() - lastMessage > intervalMs) {
var apiClient = connectionManager.currentApiClient();
if ("6da60dd6edfc4508bca2c434d4400816" === apiClient.serverId()) return Promise.resolve();
var registrationOptions = {
viewOnly: !0
};
return connectionManager.getRegistrationInfo(iapManager.getAdminFeatureName(feature), apiClient, registrationOptions).catch(function(errorResult) {
return "overlimit" === errorResult ? (appSettings.set(settingsKey, (new Date).getTime()), Promise.resolve()) : showPeriodicMessage(feature, settingsKey)
})
}
return Promise.resolve()
}
function validateFeature(feature, options) {
return options = options || {}, console.log("validateFeature: " + feature), iapManager.isUnlockedByDefault(feature, options).then(function() {
return showPeriodicMessageIfNeeded(feature)
}, function() {
var unlockableFeatureCacheKey = "featurepurchased-" + feature;
if ("1" === appSettings.get(unlockableFeatureCacheKey)) return showPeriodicMessageIfNeeded(feature);
var unlockableProduct = iapManager.getProductInfo(feature);
if (unlockableProduct) {
var unlockableCacheKey = "productpurchased-" + unlockableProduct.id;
if (unlockableProduct.owned) return appSettings.set(unlockableFeatureCacheKey, "1"), appSettings.set(unlockableCacheKey, "1"), showPeriodicMessageIfNeeded(feature);
if ("1" === appSettings.get(unlockableCacheKey)) return showPeriodicMessageIfNeeded(feature)
}
var unlockableProductInfo = unlockableProduct ? {
enableAppUnlock: !0,
id: unlockableProduct.id,
price: unlockableProduct.price,
feature: feature
} : null;
return iapManager.getSubscriptionOptions().then(function(subscriptionOptions) {
if (subscriptionOptions.filter(function(p) {
return p.owned
}).length > 0) return Promise.resolve();
var registrationOptions = {
viewOnly: options.viewOnly
};
return connectionManager.getRegistrationInfo(iapManager.getAdminFeatureName(feature), connectionManager.currentApiClient(), registrationOptions).catch(function(errorResult) {
if (!1 === options.showDialog) return Promise.reject();
var alertPromise;
return "overlimit" === errorResult && (alertPromise = showOverLimitAlert()), alertPromise || (alertPromise = Promise.resolve()), alertPromise.then(function() {
var dialogOptions = {
title: globalize.translate("sharedcomponents#HeaderUnlockFeature"),
feature: feature
};
return currentValidatingFeature = feature, showInAppPurchaseInfo(subscriptionOptions, unlockableProductInfo, dialogOptions)
})
})
})
})
}
function showOverLimitAlert() {
return alertText("Your Jellyfin Premiere device limit has been exceeded. Please check with the owner of your Jellyfin Server and have them contact Jellyfin support at apps@emby.media if necessary.").catch(function() {
return Promise.resolve()
})
}
function cancelInAppPurchase() {
var elem = document.querySelector(".inAppPurchaseOverlay");
elem && dialogHelper.close(elem)
}
function clearCurrentDisplayingInfo() {
currentDisplayingProductInfos = [], currentDisplayingResolve = null, currentValidatingFeature = null, isCurrentDialogRejected = null
}
function showExternalPremiereInfo() {
shell.openUrl(iapManager.getPremiumInfoUrl())
}
function centerFocus(elem, horiz, on) {
require(["scrollHelper"], function(scrollHelper) {
var fn = on ? "on" : "off";
scrollHelper.centerFocus[fn](elem, horiz)
})
}
function getPurchaseTermHtml(term) {
return "" + term + ""
}
function getTermsOfPurchaseHtml() {
var html = "",
termsOfPurchase = iapManager.getTermsOfPurchase ? iapManager.getTermsOfPurchase() : [];
return termsOfPurchase.length ? (html += "" + globalize.translate("sharedcomponents#HeaderTermsOfPurchase") + "
", termsOfPurchase.push('' + globalize.translate("sharedcomponents#PrivacyPolicy") + ""), termsOfPurchase.push('' + globalize.translate("sharedcomponents#TermsOfUse") + ""), html += "", html += termsOfPurchase.map(getPurchaseTermHtml).join(""), html += "
") : html
}
function showInAppPurchaseElement(subscriptionOptions, unlockableProductInfo, dialogOptions, resolve, reject) {
function onCloseButtonClick() {
dialogHelper.close(dlg)
}
cancelInAppPurchase(), currentDisplayingProductInfos = subscriptionOptions.slice(0), unlockableProductInfo && currentDisplayingProductInfos.push(unlockableProductInfo);
var dlg = dialogHelper.createDialog({
size: layoutManager.tv ? "fullscreen" : "fullscreen-border",
removeOnClose: !0,
scrollY: !1
});
dlg.classList.add("formDialog");
var html = "";
html += '", html += '", dlg.innerHTML = html, document.body.appendChild(dlg);
var btnPurchases = dlg.querySelectorAll(".btnPurchase");
for (i = 0, length = btnPurchases.length; i < length; i++) btnPurchases[i].addEventListener("click", onPurchaseButtonClick);
for (btnPurchases = dlg.querySelectorAll(".buttonPremiereInfo"), i = 0, length = btnPurchases.length; i < length; i++) btnPurchases[i].addEventListener("click", showExternalPremiereInfo);
isCurrentDialogRejected = !0;
var resolveWithTimeLimit = !1,
btnPlayMinute = dlg.querySelector(".btnPlayMinute");
btnPlayMinute && btnPlayMinute.addEventListener("click", function() {
resolveWithTimeLimit = !0, isCurrentDialogRejected = !1, dialogHelper.close(dlg)
}), dlg.querySelector(".btnRestorePurchase").addEventListener("click", function() {
restorePurchase(unlockableProductInfo)
}), loading.hide();
var btnCloseDialogs = dlg.querySelectorAll(".btnCloseDialog");
for (i = 0, length = btnCloseDialogs.length; i < length; i++) btnCloseDialogs[i].addEventListener("click", onCloseButtonClick);
dlg.classList.add("inAppPurchaseOverlay"), layoutManager.tv && centerFocus(dlg.querySelector(".formDialogContent"), !1, !0), dialogHelper.open(dlg).then(function() {
layoutManager.tv && centerFocus(dlg.querySelector(".formDialogContent"), !1, !1);
var rejected = isCurrentDialogRejected;
clearCurrentDisplayingInfo(), rejected ? reject() : resolveWithTimeLimit && resolve({
enableTimeLimit: !0
})
})
}
function getSubscriptionBenefits() {
var list = [];
return list.push({
name: globalize.translate("sharedcomponents#HeaderFreeApps"),
icon: "",
text: globalize.translate("sharedcomponents#FreeAppsFeatureDescription")
}), appHost.supports("sync") && list.push({
name: globalize.translate("sharedcomponents#HeaderOfflineDownloads"),
icon: "",
text: globalize.translate("sharedcomponents#HeaderOfflineDownloadsDescription")
}), list.push({
name: globalize.translate("sharedcomponents#LiveTV"),
icon: "",
text: globalize.translate("sharedcomponents#LiveTvFeatureDescription")
}), list.push({
name: "Jellyfin DVR",
icon: "",
text: globalize.translate("sharedcomponents#DvrFeatureDescription")
}), list.push({
name: globalize.translate("sharedcomponents#HeaderCinemaMode"),
icon: "",
text: globalize.translate("sharedcomponents#CinemaModeFeatureDescription")
}), list.push({
name: globalize.translate("sharedcomponents#HeaderCloudSync"),
icon: "",
text: globalize.translate("sharedcomponents#CloudSyncFeatureDescription")
}), list
}
function getSubscriptionBenefitHtml(item) {
var enableLink = appHost.supports("externalpremium"),
html = "",
cssClass = "listItem";
return layoutManager.tv && (cssClass += " listItem-focusscale"), enableLink ? (cssClass += " listItem-button", html += '