/*************************************************************************
* ADOBE CONFIDENTIAL
* ___________________
*
*  Copyright 2015 Adobe Systems Incorporated
*  All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any.  The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and are protected by all applicable intellectual property laws,
* including trade secret and or copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
**************************************************************************/
class GmailExpressFte{id="GmailExpressFte";timeout=3e3;expressMaxFteCount=3;shortCoolDown=7;longCoolDown=60;ONE_DAY_IN_MILLISECONDS=864e5;gmailExpressStateKey="gmailExpressState";gmailExpressInitialState={previewTouchpointUsed:!1,hoverTouchpointUsed:!1,fte:{count:0,nextDate:null}};rendered=!1;constructor(){this.initPromise=this.init()}isEligibleWithAcrobat=t=>{const e=(new Date).getTime();return!(t.fte?.nextDate&&e<=t.fte.nextDate)};isEligibleWithExpress=t=>{if(t.fte?.shown>=this.expressMaxFteCount)return!1;const e=7*this.ONE_DAY_IN_MILLISECONDS;return!(t.fte?.lastShown&&t.fte?.lastShown+e>Date.now())};isEligible=async()=>{if(await this.initPromise,!this.config?.enableGmailExpressFte)return!1;let t=await chrome.storage.local.get(this.gmailExpressStateKey);if(t?.gmailExpressState?t=t.gmailExpressState:(t=this.gmailExpressInitialState,chrome.storage.local.set({[this.gmailExpressStateKey]:t})),t.previewTouchpointUsed||t.hoverTouchpointUsed)return!1;if(!(this.config?.enableGmailConvertToPdfInImages?this.isEligibleWithAcrobat(t):this.isEligibleWithExpress(t)))return!1;return!!await this.getCtaElement()};render=async()=>{if(this.rendered)return;const t=await chrome.storage.local.get(this.gmailExpressStateKey);if(t?.gmailExpressState?.previewTouchpointUsed)return;const e=t?.gmailExpressState?.fte?.lastShown;if(e&&e+this.ONE_DAY_IN_MILLISECONDS>Date.now())return;const s=await this.getCtaElement(),i=s?.getElementsByClassName("cc440d50ba-express-entrypoint-button")[0];if(i){this.rendered=!0;const t=t=>{"none"===getComputedStyle(i).display&&this.expressContextualFTE.remove();const e=t;e.style.position="absolute",e.style.left=(s.offsetWidth-224)/2+"px"};this.expressContextualFTE=new this.expressContextualFteClass({touchpoint:"gmailNativeViewer",ctaButtonElement:s,fteStrings:{fteTitle:this.config.fteStrings.title,fteDescription:this.config.fteStrings.description,fteCtaLabel:this.config.fteStrings.ctaLabel},updateFteStateCallback:this.updateGmailExpressState,positionFteCallback:t,renderCallback:t=>{s.appendChild(t)}}),await this.expressContextualFTE.render()}};getCtaElement=async()=>{for(let t=0;t<10;t+=1){const t=document.getElementById("express-gmail-native-viewer-entry-point");if(t)return t;await new Promise(t=>setTimeout(t,100))}return null};setAcrobatCooldown=t=>{const e=new Date,s=(t.fte.count||0)+1,i=s%3==0?this.longCoolDown:this.shortCoolDown;e.setDate(e.getDate()+i),t.fte.count=s,t.fte.nextDate=e.getTime()};setExpressCooldown=t=>{t.fte.shown=(t.fte.shown||0)+1,t.fte.lastShown=Date.now()};updateGmailExpressState=async()=>{let t=await chrome.storage.local.get(this.gmailExpressStateKey);t=t?.gmailExpressState?t.gmailExpressState:this.gmailExpressInitialState;const e={...this.gmailExpressInitialState,...t};this.config?.enableGmailConvertToPdfInImages?this.setAcrobatCooldown(e):this.setExpressCooldown(e),chrome.storage.local.set({[this.gmailExpressStateKey]:e})};loadContentScripts=async()=>{const t=chrome.runtime.getURL("content_scripts/express/fte/express-contextual-fte.js");return Promise.all([import(t)]).then(t=>{this.expressContextualFteClass=t[0].default})};init=async()=>{if("mail.google.com"!==window.location.hostname)return this.isEligible=async()=>!1,void(this.render=async()=>{});const t=await chrome.runtime.sendMessage({main_op:"gmail-express-init"});this.config=t,await this.loadContentScripts()}}