import events from"./../emby-apiclient/events.js";import connectionManager from"./../emby-apiclient/connectionmanager.js";import appSettings from"./../common/appsettings.js";import focusManager from"./../focusmanager.js";import dialogHelper from"./../dialoghelper/dialoghelper.js";import loading from"./../loading/loading.js";import{appHost,shell,iapManager}from"./../common/servicelocator.js";import layoutManager from"./../layoutmanager.js";import globalize from"./../common/globalize.js";import listView from"./../listview/listview.js";import"./../emby-elements/emby-button/emby-button.js";import"./../emby-elements/emby-dialogclosebutton/emby-dialogclosebutton.js";import"./../emby-elements/emby-itemscontainer/emby-itemscontainer.js";require(["formDialogStyle"]);let currentDisplayingProductInfos=[],currentDisplayingResolve=null,currentValidatingFeature=null,isCurrentDialogRejected=null;function showAlert(options){return Emby.importModule("./modules/common/dialogs/alert.js").then(function(alert){return alert(options)})}function showPrompt(options){return Emby.importModule("./modules/prompt/prompt.js").then(function(prompt){return prompt(options)})}function showInAppPurchaseInfo(subscriptionOptions,unlockableProductInfo,dialogOptions){return new Promise(function(resolve,reject){showInAppPurchaseElement(subscriptionOptions,unlockableProductInfo,dialogOptions,resolve,reject),currentDisplayingResolve=resolve})}function getBenefitsListOptions(items){return{renderer:listView,options:{defaultBackground:!1,moreButton:!1,action:"none",multiSelect:!1,roundImage:!0,fields:["Name","ShortOverview"],draggable:!1,draggableXActions:!1,contextMenu:!1}}}function initSubscriptionBenefitsItemsContainer(context){let itemsContainer=context.querySelector(".benefitsItemsContainer");if(itemsContainer)return itemsContainer.fetchData=getSubscriptionBenefits,itemsContainer.getListOptions=getBenefitsListOptions,itemsContainer.waitForCustomElementUpgrade().then(function(){return itemsContainer.resume({refresh:!0})})}function showPeriodicMessage(feature,settingsKey){return new Promise(function(resolve,reject){let dlg=dialogHelper.createDialog({size:layoutManager.tv?"fullscreen-border":"medium-tall",removeOnClose:!0,scrollY:!1});dlg.classList.add("formDialog");var html=(html=(html=(html=(html=(html=(html=(html=(html=(html=(html=(html=(html="")+'
'+'')+'

Emby Premiere'+"

")+"
"+'
')+'
'+'
')+('

'+globalize.translate("HeaderDiscoverEmbyPremiere")+"

"))+("

"+globalize.translate("MessageDidYouKnowCinemaMode")+"

"))+("

"+globalize.translate("CinemaModeFeatureDescription")+"

"))+("

"+globalize.translate("HeaderBenefitsEmbyPremiere")+"

"))+'
'+"
")+('

'+globalize.translate("AndMuchMoreExclamation")+"

"))+"
"+'
')+('");let seconds=11,isRejected=(html=(html=(html=(html+='
'+globalize.translate("ContinueInSecondsValue",seconds)+"
")+('"))+"
"+"
")+"
"+"
",dlg.innerHTML=html,!0),timeTextInterval=setInterval(function(){var btnContinue;--seconds<=0?(clearInterval(timeTextInterval),dlg.querySelector(".continueTimeText").classList.add("hide"),(btnContinue=dlg.querySelector(".btnContinue")).classList.remove("hide"),focusManager.focus(btnContinue)):dlg.querySelector(".continueTimeText").innerHTML=globalize.translate("ContinueInSecondsValue",seconds)},1e3),i,length;var btnPurchases=dlg.querySelectorAll(".buttonPremiereInfo");for(i=0,length=btnPurchases.length;iintervalMs)||"554ae9ea56b94c1c82cc628f6de52d85"===(lastMessage=connectionManager.currentApiClient()).serverId()?Promise.resolve():(intervalMs={viewOnly:!0},connectionManager.getRegistrationInfo(iapManager.getAdminFeatureName(feature),lastMessage,intervalMs).catch(function(errorResult){return"overlimit"===errorResult?(appSettings.set(settingsKey,Date.now()),Promise.resolve()):showPeriodicMessage(feature,settingsKey)})):(appSettings.set(settingsKey,Date.now()),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)}let unlockableProductInfo=unlockableProduct?{enableAppUnlock:!0,id:unlockableProduct.id,price:unlockableProduct.price,feature:feature}:null;return iapManager.getSubscriptionOptions().then(function(subscriptionOptions){var registrationOptions,apiClient;return 0"+term+""}function getTermsOfPurchaseHtml(){var html="",termsOfPurchase=iapManager.getTermsOfPurchase?iapManager.getTermsOfPurchase():[];return termsOfPurchase.length?(html+="

"+globalize.translate("HeaderTermsOfPurchase")+"

",termsOfPurchase.push(''+globalize.translate("PrivacyPolicy")+""),termsOfPurchase.push(''+globalize.translate("TermsOfUse")+""),(html+=""):""}function showInAppPurchaseElement(subscriptionOptions,unlockableProductInfo,dialogOptions,resolve,reject){cancelInAppPurchase(),currentDisplayingProductInfos=subscriptionOptions.slice(0),unlockableProductInfo&¤tDisplayingProductInfos.push(unlockableProductInfo);let dlg=dialogHelper.createDialog({size:layoutManager.tv?"fullscreen-border":"medium-tall",removeOnClose:!0,scrollY:!1}),html=(dlg.classList.add("formDialog"),"");html=(html=(html=(html=(html=(html=(html=(html=(html+='
')+'

')+(dialogOptions.title||""))+"

")+'
')+'

')+(unlockableProductInfo?globalize.translate("MessageUnlockAppWithPurchaseOrSupporter"):globalize.translate("MessageUnlockAppWithSupporter")))+'

')+globalize.translate("MessageToValidateSupporter")+"

";let i,length;for(i=0,length=subscriptionOptions.length;i")+'

";if(unlockableProductInfo){let unlockText=globalize.translate("ButtonUnlockWithPurchase");unlockableProductInfo.price&&(unlockText=globalize.translate("ButtonUnlockPrice",unlockableProductInfo.price)),html=(html+="

")+('")+"

"}html=(html+="

")+'

",subscriptionOptions.length&&(html=(html=(html+='

'+globalize.translate("HeaderBenefitsEmbyPremiere")+"

")+'
')+'

'+globalize.translate("AndMuchMoreExclamation")+"

"),html=(html=(html="playback"===dialogOptions.feature?(html+="

")+'

":html)+getTermsOfPurchaseHtml()+"")+"
",dlg.innerHTML=html,document.body.appendChild(dlg);let btnPurchases=dlg.querySelectorAll(".btnPurchase");for(i=0,length=btnPurchases.length;i')+'

')+iapManager.getRestoreButtonText())+"

")+'
')+'

')+globalize.translate("HowDidYouPay"))+"

")+'

",unlockableProductInfo&&(html=(html+="

")+'

"),html=(html+="
")+"
",dlg.innerHTML=html,document.body.appendChild(dlg),loading.hide(),dlg.querySelector(".btnRestoreSub").addEventListener("click",function(){dialogHelper.close(dlg),showAlert({text:globalize.translate("MessageToValidateSupporter"),title:"Emby Premiere"})});unlockableProductInfo=dlg.querySelector(".btnRestoreUnlock");unlockableProductInfo&&unlockableProductInfo.addEventListener("click",function(){dialogHelper.close(dlg),iapManager.restorePurchase()}),dialogHelper.open(dlg)}function getUserEmail(){if(connectionManager.isLoggedIntoConnect()){var connectUser=connectionManager.connectUser();if(connectUser&&connectUser.Email)return Promise.resolve(connectUser.Email)}return showPrompt({label:globalize.translate("LabelEmail")})}function onProductUpdated(e,product){let resolve=currentDisplayingResolve;var feature;product.owned&&resolve&¤tDisplayingProductInfos.filter(function(p){return product.id===p.id}).length?(isCurrentDialogRejected=!1,cancelInAppPurchase(),resolve()):(feature=currentValidatingFeature)&&iapManager.isUnlockedByDefault(feature).then(function(){isCurrentDialogRejected=!1,cancelInAppPurchase(),resolve&&resolve()})}function showPremiereInfo(){return appHost.supports("externalpremium")?(showExternalPremiereInfo(),Promise.resolve()):iapManager.getSubscriptionOptions().then(function(subscriptionOptions){return showInAppPurchaseInfo(subscriptionOptions,null,{title:"Emby Premiere",feature:"sync"})})}events.on(iapManager,"productupdated",onProductUpdated);export default{validateFeature:validateFeature,showPremiereInfo:showPremiereInfo};