/*************************************************************************
* 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.
**************************************************************************/
import{getElementBasedOnSelector,getFileDetailsElementInNativeViewer,createURLForAttachment,isOrphanContentScript,isDriveFileDirectDownloadLink,getParentElementForNativeViewerPrompt,normalizeFileName}from"./util.js";import{createAcrobatIconElement,buildAcrobatPromotionSource,sendAnalytics,sendAnalyticsOnce,sendAnalyticsOncePerMonth,getFileDetails,sendErrorLog}from"../utils/util.js";import{createFteTooltip,shouldShowFteTooltip,updateFteToolTipCoolDown,acrobatTouchPointClicked,getAcrobatTouchPointFteEligibility,removeFteTooltip,addFteCloseButtonListener}from"../utils/fte-utils.js";import state from"./state.js";const NATIVE_VIEWER_PROMPT_CLASS="acrobat-native-viewer-prompt",GMAIL_FTE_TOOLTIP_STORAGE_KEY="acrobat-gmail-fte-config",GMAIL_FTE_TOOLTIP_CONTAINER_CLASS="acrobat-fte-tooltip-container",ADD_TO_DRIVE_BUTTON_KEY="addToDriveButton",ORGANISE_IN_DRIVE_BUTTON_KEY="organiseInDriveButton",hideNativeViewerPrompt=e=>{if(!state.nativeViewerPromptState?.nativeViewerPromptVisible&&!e)return;const t=document.getElementsByClassName(NATIVE_VIEWER_PROMPT_CLASS);if(t?.length>0){const e=t[0].getElementsByClassName("acrobat-fte-tooltip-container");e?.length>0&&sendAnalytics(["DCBrowserExt:GmailFTE:NativeView:Dismissed"]),t[0].parentElement.removeChild(t[0])}const i="native-viewer-pdf-tools-touchpoint";document.getElementById(i)&&import(chrome.runtime.getURL("dist/PdfToolsDropdown.js")).then(e=>{const t=e.default;t&&t.isRendered()&&t.remove()}).catch(()=>{}).finally(()=>{const e=document.getElementById(i);e&&e.parentElement&&e.parentElement.removeChild(e)}),state.nativeViewerPromptState.nativeViewerPromptVisible=!1},createNativeViewPromptTextElement=()=>{const e=document.createElement("span");return e.setAttribute("class","acrobat-native-viewer-prompt-text"),e.textContent=state?.gmailConfig?.acrobatPromptText||"Open with Acrobat",e},getAnalyticsType=(e,t)=>{let i="LIST_VIEW"===t?"NativeViewerPromptLV":"NativeViewerPrompt";return isDriveFileDirectDownloadLink(e)&&(i+="_DriveLink"),i},createNativeViewerPrompt=(e,t,i)=>{const o=document.createElement("a"),n=createAcrobatIconElement("acrobat-icon",state?.iconURL||"browser/images/acrobat_dc_appicon_128.png"),r=createNativeViewPromptTextElement(),a=buildAcrobatPromotionSource("gmail_chrome","native_view"),l=createURLForAttachment(e,a,t);return o.setAttribute("class",NATIVE_VIEWER_PROMPT_CLASS),o.setAttribute("href",l),o.setAttribute("target","_blank"),o.appendChild(n),o.appendChild(r),o.addEventListener("click",()=>{const t=getAnalyticsType(e,i);sendAnalytics([[`DCBrowserExt:Gmail:${t}:Clicked`,{gsuiteFte:getAcrobatTouchPointFteEligibility()}]]),acrobatTouchPointClicked("acrobat-gmail-fte-config"),chrome.runtime.sendMessage({main_op:"akamai-ping"}),state.fteToolTip.eligibleFte.type="",state.fteToolTip.touchPointClicked=!0}),o},processForAlreadyShownNativeViewerPrompt=()=>{const{nativeViewerPromptVisible:e,previousFileDetailsElement:t,nativeViewerAttachmentURL:i}=state?.nativeViewerPromptState;if(e){const e=getFileDetailsElementInNativeViewer();e?e!==t&&hideNativeViewerPrompt():(state.nativeViewerPromptState.previousFileDetailsElement=null,state.nativeViewerPromptState.nativeViewerAttachmentURL=null,hideNativeViewerPrompt(),removeEventListenersForDriveButton("addToDriveButton"),removeEventListenersForDriveButton("organiseInDriveButton"))}else if(t&&!isOrphanContentScript()){const e=getFileDetailsElementInNativeViewer();e||(removeEventListenersForDriveButton("addToDriveButton"),removeEventListenersForDriveButton("organiseInDriveButton")),e&&t===e&&addAcrobatPromptInNativeViewer(i)}},removeFteTooltipFromAttachmentDiv=()=>{removeFteTooltip(),state.fteToolTip.eligibleFte.type=""},handleFteClickOutside=(e,t)=>{if(t.getRootNode()instanceof ShadowRoot){e.composedPath().includes(t)||(t.remove(),state.fteToolTip.eligibleFte.type="",sendAnalytics(["DCBrowserExt:GmailFTE:PdfTools:Dismissed"]),t.clickOutsideHandler&&document.removeEventListener("click",t.clickOutsideHandler))}else{const i=document.getElementsByClassName("acrobat-fte-tooltip-container");i?.length>0&&(t&&t.contains(e.target)||(sendAnalytics(["DCBrowserExt:GmailFTE:NativeView:Dismissed"]),removeFteTooltipFromAttachmentDiv(),document.removeEventListener("click",handleFteClickOutside)))}},addFteTooltipButtonEventListener=e=>{e.querySelector(".acrobat-fte-tooltip-button").addEventListener("click",()=>{removeFteTooltipFromAttachmentDiv(),sendAnalytics(["DCBrowserExt:GmailFTE:NativeView:Clicked"]),document.removeEventListener("click",handleFteClickOutside)})},addFteTooltipToAttachmentDiv=e=>{const t=createFteTooltip(state?.gmailConfig?.gmailFteToolTipStrings,"nativeView");addFteTooltipButtonEventListener(t),document.addEventListener("click",e=>handleFteClickOutside(e,t),{once:!0,signal:state?.eventControllerSignal}),state.fteToolTip.eligibleFte.type="nativeView",e.appendChild(t),sendAnalytics(["DCBrowserExt:GmailFTE:NativeView:Shown"]),updateFteToolTipCoolDown(state?.gmailConfig?.fteConfig?.tooltip,"acrobat-gmail-fte-config").then(e=>{state.fteToolTip={...state?.fteToolTip,...e}})},addFTETooltipIfEligible=e=>{const t=state?.gmailConfig?.fteConfig?.tooltip;shouldShowFteTooltip(t,state?.fteToolTip,!0).then(t=>{t?(gmailAcrobatFteCoachmark.setEligibility(!0,()=>{addFteTooltipToAttachmentDiv(e)}),chrome.runtime.sendMessage({main_op:"reRenderShowOneChild"})):gmailAcrobatFteCoachmark.setEligibility(!1)})},addPdfToolsFteTooltipToAttachmentDiv=e=>{const t=createFteTooltip({title:state?.gmailConfig?.pdfToolsFteToolTipStrings?.title,description:state?.gmailConfig?.pdfToolsFteToolTipStrings?.description,button:state?.gmailConfig?.pdfToolsFteToolTipStrings?.button},"pdfTools");function i(e){handleFteClickOutside(e,t)}t.clickOutsideHandler=i,addFteCloseButtonListener(t,{fteType:"pdfTools",onClose:()=>{state.fteToolTip.eligibleFte.type=""},sendAnalytics:sendAnalytics,sendErrorLog:sendErrorLog}),document.addEventListener("click",i,{once:!0,signal:state?.eventControllerSignal}),state.fteToolTip.eligibleFte.type="pdfTools";const o=e.shadowRoot;o?setTimeout(()=>{const e=o.querySelector("#pdf-tools-react-root"),i=o.querySelector(".cvPdfTools-pdf-entrypoint-button");if(!e||!i)return void sendErrorLog("PDFToolsFTE_ReactElementsNotFound","React root or CTA button not found in shadow DOM");e.appendChild(t);const n=()=>{t.remove(),state.fteToolTip.eligibleFte.type="",sendAnalytics(["DCBrowserExt:GmailFTE:PdfTools:Dismissed:Hover"]),t.clickOutsideHandler&&document.removeEventListener("click",t.clickOutsideHandler),i.removeEventListener("mouseenter",n)};i.addEventListener("mouseenter",n,{once:!0}),sendAnalytics(["DCBrowserExt:GmailFTE:PdfTools:Shown"])},50):sendErrorLog("Add_PDFToolsFTE_NoShadowRoot","Shadow root not found on pdfToolsDropdownElement")},addPdfToolsFTETooltipIfEligible=async e=>{const t="acrobat-gmail-pdf-tools-fte-shown";await initDcLocalStorage();window.dcLocalStorage.getItem(t)||(addPdfToolsFteTooltipToAttachmentDiv(e),window.dcLocalStorage.setItem(t,!0))},addAcrobatPromptInNativeViewer=async(e,t,i)=>{try{const{nativeViewerPromptVisible:o}=state?.nativeViewerPromptState;if(o)return;state.nativeViewerPromptState.nativeViewerPromptVisible=!0;const n=state?.gmailConfig?.enablePdfToolsTouchPoint||!1;await initDcLocalStorage();if(("en-US"===window.dcLocalStorage.getItem("locale")||"en-GB"===window.dcLocalStorage.getItem("locale"))&&n){const o=getParentElementForNativeViewerPrompt();o&&o?.childNodes?.length>0?import(chrome.runtime.getURL("dist/PdfToolsDropdown.js")).then(i=>i.default.render(i=>{const o=buildAcrobatPromotionSource("gmail_chrome",`pdf_tools_${i}`),n=createURLForAttachment(e,o,t,i);window.open(n,"_blank")},"gmailNativeViewer")).then(t=>{if(o.insertBefore(t,o.childNodes[0]),state.nativeViewerPromptState.nativeViewerAttachmentURL=e,state.nativeViewerPromptState.previousFileDetailsElement=getFileDetailsElementInNativeViewer(),window.innerWidth>1200){const o=getAnalyticsType(e,i);sendAnalyticsOncePerMonth(`DCBrowserExt:Gmail:${o}_PdfTools:Shown`),addPdfToolsFTETooltipIfEligible(t)}}).catch(e=>{sendErrorLog("PDFTools_ReactRenderError",e.message),state.nativeViewerPromptState.nativeViewerPromptVisible=!1,hideNativeViewerPrompt()}):(state.nativeViewerPromptState.nativeViewerPromptVisible=!1,hideNativeViewerPrompt())}else{const o=createNativeViewerPrompt(e,t,i),n=getParentElementForNativeViewerPrompt();if(n&&n?.childNodes?.length>0){if(n.insertBefore(o,n.childNodes[0]),state.nativeViewerPromptState.nativeViewerAttachmentURL=e,state.nativeViewerPromptState.previousFileDetailsElement=getFileDetailsElementInNativeViewer(),window.innerWidth>1200){addFTETooltipIfEligible(o);const t=getAnalyticsType(e,i);sendAnalyticsOncePerMonth(`DCBrowserExt:Gmail:${t}:Shown`)}}else state.nativeViewerPromptState.nativeViewerPromptVisible=!1,hideNativeViewerPrompt()}}catch(e){sendErrorLog("NativeViewerPrompt_Error",e.message),state.nativeViewerPromptState.nativeViewerPromptVisible=!1,hideNativeViewerPrompt()}},removeEventListenersForDriveButton=e=>{if(state?.nativeViewerPromptState?.driveButtonAddedListeners[e]){const t=getElementBasedOnSelector(document,e,"nativeViewer");t&&t.removeEventListener("click",state?.nativeViewerPromptState?.driveButtonAddedListeners[e]),delete state?.nativeViewerPromptState?.driveButtonAddedListeners[e]}},driveButtonClickListener=(e,t)=>()=>{sendAnalyticsOnce(e),"addToDriveButton"===t&&setTimeout(()=>addEventForDriveButtonClick("organiseInDriveButton"),1e3)},addEventForDriveButtonClick=e=>{if(!state?.nativeViewerPromptState?.driveButtonAddedListeners[e]){const t=getElementBasedOnSelector(document,e,"nativeViewer");if(t){const i=driveButtonClickListener(`DCBrowserExt:GmailNV:${e}:Clicked`,e);t.addEventListener("click",i,{signal:state?.eventControllerSignal,once:!0}),state.nativeViewerPromptState.driveButtonAddedListeners[e]=i}}},processForNativeViewer=(e,t)=>{const i=getFileDetailsElementInNativeViewer();if(i&&!isOrphanContentScript()){const o=getFileDetails(i);if(e?.name&&o){const t=/[&<>"'`!@#$%^*()\-+={}[\]|\\:;?/~]/.test(e?.name)?normalizeFileName(o?.title||""):o?.title;if(e?.name!==t)return}addAcrobatPromptInNativeViewer(e?.url,o.title,t),addEventForDriveButtonClick("addToDriveButton"),addEventForDriveButtonClick("organiseInDriveButton")}else hideNativeViewerPrompt()},sendAnalyticsIfNativeViewerLaunched=e=>{getFileDetailsElementInNativeViewer()&&sendAnalytics([[`DCBrowserExt:Gmail:NativeViewerLV:${e}`]])},removeAllTouchPoints=()=>{hideNativeViewerPrompt(!0)};export{processForAlreadyShownNativeViewerPrompt,sendAnalyticsIfNativeViewerLaunched,processForNativeViewer,removeAllTouchPoints,getFileDetails,getAnalyticsType,getFileDetailsElementInNativeViewer,getParentElementForNativeViewerPrompt,isOrphanContentScript};