/*************************************************************************
* 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.
**************************************************************************/
const FTE_TOOLTIP_CONTAINER_CLASS="acrobat-fte-tooltip-container",FTE_TOOLTIP_ELIGIBLE_EVAR="FteEligible",EXTENSION_ENVIRONMENT_KEY="env",createFteToolTipDivObject=(t,e)=>{const o=document.createElement("div");return o.setAttribute("class",t),e&&e.length>0&&(o.textContent=e),o},createFteTooltipObject=(t,e,o)=>{const a=createFteToolTipDivObject(e,"");a.setAttribute("class",e),a.id=FTE_TOOLTIP_CONTAINER_CLASS;const n=createFteToolTipDivObject("acrobat-fte-tooltip",""),l=createFteToolTipDivObject(o,"");if(n.appendChild(l),t?.title?.length>0){const e=createFteToolTipDivObject("acrobat-fte-tooltip-title",t.title);n.appendChild(e)}if(t?.description?.length>0){const e=createFteToolTipDivObject("acrobat-fte-tooltip-description",t.description);n.appendChild(e)}if(t?.button?.length>0){const e=document.createElement("button");e.setAttribute("class","acrobat-fte-tooltip-button"),e.textContent=t.button,n.appendChild(e)}return a.appendChild(n),a},createFteTooltip=(t,e)=>{const o=createFteTooltipObject(t,`acrobat-fte-tooltip-container-${e} acrobat-fte-tooltip-container`,`acrobat-fte-tooltip-arrow-${e} acrobat-fte-tooltip-arrow`);return o.addEventListener("click",t=>{t.preventDefault(),t.stopPropagation()}),o},getFteCustomCoolDownTime=async()=>{const{env:t}=await getLocalStorageValue("env");if("prod"===t)return 0;const e=new URLSearchParams(window.location.search).get("acrobatTouchPointFteDay");return e?parseInt(e):0},shouldShowFteTooltip=async(t,e,o)=>{const a=(new Date).getTime();if(e?.touchPointClicked||!o)return!1;return!(document.getElementsByClassName(FTE_TOOLTIP_CONTAINER_CLASS).length>0)&&(a>e?.nextDate&&(e?.count<t?.maxFteCount||-1===t?.maxFteCount))},applyCoolDown=(t,e,o)=>{o?t.setSeconds(t.getSeconds()+e):t.setDate(t.getDate()+e)},getCoolDown=(t,e,o)=>{let a,n;return e>0?(a=t%3>0?o.shortCoolDown*e:o.longCoolDown*e,n=!0):(a=t%3>0?o.shortCoolDown:o.longCoolDown,n=!1),{coolDown:a,isSeconds:n}},updateFteToolTipCoolDown=async(t,e)=>{const o=await getLocalStorageValue(e),a=new Date,n=o?.[e]||{count:0,nextDate:a.toISOString()},l=n.count+1,i=await getFteCustomCoolDownTime(),{coolDown:c,isSeconds:r}=getCoolDown(l,i,t);return applyCoolDown(a,c,r),n.nextDate=a.getTime(),n.count=l,await setLocalStorageValue(e,n),n},updateOneTimeFteToolTipCoolDown=async(t,e={},o=null)=>{const a=new Date,n=await getLocalStorageValue(t),l=n?.[t]||{count:0,nextDate:a},i=(l.count||0)+1;if(l.count=i,o){const t=await getFteCustomCoolDownTime(),{coolDown:n,isSeconds:c}=getCoolDown(i,t,e);applyCoolDown(a,n,c),l.nextDate=a.getTime();const r=await getLocalStorageValue(o),s=r?.[o]||{count:0,nextDate:a};s.nextDate=a.getTime(),await setLocalStorageValue(o,s)}await setLocalStorageValue(t,l)},removeFteTooltip=(t=FTE_TOOLTIP_CONTAINER_CLASS)=>{const e=document.getElementsByClassName(t);e.length>0&&e[0].remove()},initFteStateAndConfig=async t=>{let e={count:0,nextDate:(new Date).getTime()};return e=(await chrome.storage.local.get(t))?.[t]||e,chrome.storage.local.set({[t]:e}),e},acrobatTouchPointClicked=async t=>{const e=await getLocalStorageValue(t),o=e?.[t]||{};o?.touchPointClicked||(removeFteTooltip(FTE_TOOLTIP_CONTAINER_CLASS),o.touchPointClicked=!0,setLocalStorageValue(t,o))},getAcrobatTouchPointFteEligibility=()=>"FteEligible",getLocalStorageValue=async t=>await chrome.storage.local.get(t),setLocalStorageValue=async(t,e)=>await chrome.storage.local.set({[t]:e},()=>{chrome.runtime?.sendMessage({main_op:t,fteState:e})}),addFteCloseButtonListener=(t,e={})=>{const{fteType:o="pdfTools",onClose:a,sendAnalytics:n,sendErrorLog:l}=e,i=t.querySelector(".acrobat-fte-tooltip-button");i?i.addEventListener("click",()=>{if(t.remove(),a&&a(),t.clickOutsideHandler&&document.removeEventListener("click",t.clickOutsideHandler),n){n([`DCBrowserExt:GmailFTE:${"pdfTools"===o?"PdfTools":"NativeView"}:Close:Clicked`])}}):l&&l(`FTE_CloseButtonNotFound_${o}`,"Close button not found in tooltip")};export{createFteTooltip,shouldShowFteTooltip,updateFteToolTipCoolDown,updateOneTimeFteToolTipCoolDown,acrobatTouchPointClicked,getAcrobatTouchPointFteEligibility,removeFteTooltip,addFteCloseButtonListener,initFteStateAndConfig};