/*************************************************************************
* 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{createURLForAttachment,getArrayElementBasedOnSelector,getClosestElementBasedOnSelector,getElementBasedOnSelector,updateDrivePDFUrl,isDriveFileDirectDownloadLink,sendAnalyticsEvent}from"./util.js";import{isDefaultViewer,openPdfInNewTab}from"./default-viewership-service.js";import{buildAcrobatPromotionSource,sendAnalytics,sendAnalyticsOncePerMonth,createAcrobatIconElement}from"../utils/util.js";import{acrobatTouchPointClicked,createFteTooltip,getAcrobatTouchPointFteEligibility,removeFteTooltip,shouldShowFteTooltip,updateFteToolTipCoolDown}from"../utils/fte-utils.js";import state from"./state.js";import{processForNativeViewer}from"./native-viewer-touch-point-service.js";const ATTACHMENT_CARD_HYPERLINK_CLASS="acrobat-attachmentcard-hyperlink",ACROBAT_PROCESSED_ATTRIBUTE="acrobat-icon-added",GMAIL_FTE_TOOLTIP_STORAGE_KEY="acrobat-gmail-fte-config",GMAIL_FTE_TOOLTIP_CONTAINER_CLASS="acrobat-fte-tooltip-container",getMessageView=()=>getArrayElementBasedOnSelector(document,"messageView","messageView"),getParentElementForAcrobatIcon=e=>getElementBasedOnSelector(e,"attachmentCardParentElementForAcrobatIcon","messageView"),getAttachmentCardElement=e=>getClosestElementBasedOnSelector(e,"attachmentCardElement","messageView"),createAcrobatTooltip=()=>{const e=document.createElement("div");return e.setAttribute("class","acrobat-attachmentcard-tooltip"),e.innerText=state?.gmailConfig?.acrobatPromptText,e},handleAcrobatHyperLinkClickForFteToolTip=e=>{const t=document.getElementsByClassName("acrobat-fte-tooltip-container");t?.length>0&&(e.style.removeProperty("z-index"),sendAnalytics(["DCBrowserExt:GmailFTE:MessageView:Dismissed"])),acrobatTouchPointClicked("acrobat-gmail-fte-config"),state.fteToolTip.eligibleFte.type="",state.fteToolTip.touchPointClicked=!0},createAcrobatHyperlinkForAttachmentCard=(e,t,o)=>{const a=buildAcrobatPromotionSource("gmail_chrome","card");e=createURLForAttachment(e,a,t);const n=document.createElement("a");n.setAttribute("class",ATTACHMENT_CARD_HYPERLINK_CLASS),n.setAttribute("href",e),n.setAttribute("target","_blank"),n.addEventListener("click",()=>{let t="DCBrowserExt:Gmail:AttachmentCardPrompt:Clicked";isDriveFileDirectDownloadLink(e)&&(t="DCBrowserExt:Gmail:AttachmentCardPrompt_DriveLink:Clicked"),sendAnalytics([[t,{gsuiteFte:getAcrobatTouchPointFteEligibility()}]]),handleAcrobatHyperLinkClickForFteToolTip(o),chrome.runtime.sendMessage({main_op:"akamai-ping"})},{signal:state?.eventControllerSignal});const i=createAcrobatIconElement("acrobat-icon",state?.iconURL||"browser/images/acrobat_dc_appicon_128.png"),r=createAcrobatTooltip();return n.appendChild(i),n.appendChild(r),n},getMessageViewPDFAttachmentsWithoutAcrobatIcon=()=>{const e=getMessageView();let t=[];if(e&&e.length>0)for(let o=0;o<e.length;o++){const a=e[o],n=getArrayElementBasedOnSelector(a,"pdfAttachmentWithoutAcrobatIcon","messageView");n&&n.length>0&&t.push(...n)}return t},addFteTooltipButtonEventListener=(e,t)=>{e.querySelector(".acrobat-fte-tooltip-button").addEventListener("click",()=>{removeFteTooltip(),state.fteToolTip.eligibleFte.type="",t?.style?.removeProperty("z-index"),sendAnalytics(["DCBrowserExt:GmailFTE:MessageView:Clicked"])})},addFteTooltipToAttachmentCard=e=>{const t=createFteTooltip(state?.gmailConfig?.gmailFteToolTipStrings,"messageView");addFteTooltipButtonEventListener(t),e.appendChild(t),sendAnalytics(["DCBrowserExt:GmailFTE:MessageView:Shown"]),updateFteToolTipCoolDown(state?.gmailConfig?.fteConfig?.tooltip,"acrobat-gmail-fte-config").then(e=>{state.fteToolTip={...state?.fteToolTip,...e}})},handleMouseEnterEventForCardElementWithFTETooltip=e=>{const t=e.getElementsByClassName("acrobat-fte-tooltip-container");t?.length>0&&(e.style.zIndex="2",t[0].style.removeProperty("display"))};function addFTETooltipToAttachmentCardIfEligible(e,t){const o=state?.gmailConfig?.fteConfig?.tooltip;shouldShowFteTooltip(o,state?.fteToolTip,!0).then(o=>{o?(gmailAcrobatFteCoachmark.setEligibility(!0,()=>{e.style.zIndex="2",addFteTooltipToAttachmentCard(t)}),chrome.runtime.sendMessage({main_op:"reRenderShowOneChild"})):gmailAcrobatFteCoachmark.setEligibility(!1)})}const attachmentCardMouseOverEventListener=(e,t)=>{e.addEventListener("mouseenter",()=>{handleMouseEnterEventForCardElementWithFTETooltip(e),addFTETooltipToAttachmentCardIfEligible(e,t)},{signal:state?.eventControllerSignal})},attachmentCardMouseOutEventListener=e=>{e.addEventListener("mouseleave",()=>{const t=e.getElementsByClassName("acrobat-fte-tooltip-container");t?.length>0&&(t[0].style.display="none",e.style.removeProperty("z-index"))},{signal:state?.eventControllerSignal})},handleFteTooltipForAttachmentCard=(e,t)=>{attachmentCardMouseOverEventListener(e,t),attachmentCardMouseOutEventListener(e)},handleClickAction=(e,t,o)=>{if("click"===e.type||"keydown"===e.type&&"Enter"===e.key){if(isDefaultViewer()){e.preventDefault(),e.stopPropagation();const t=buildAcrobatPromotionSource("gmail_chrome","card");openPdfInNewTab({url:o?.url,touchPoint:t,attachmentName:o?.name})}else{const e=document.getElementsByClassName("acrobat-fte-tooltip-container");e?.length>0&&(t.style.removeProperty("z-index"),removeFteTooltip(),state.fteToolTip.eligibleFte.type="",sendAnalytics(["DCBrowserExt:GmailFTE:MessageView:Dismissed"])),setTimeout(()=>processForNativeViewer({url:o?.url}),500),setTimeout(()=>processForNativeViewer({url:o?.url}),1e3)}sendAnalyticsOncePerMonth(`DCBrowserExt:Gmail:AttachmentCard${o?.isDriveAsset?"_DriveLink":""}:Clicked`)}},addAttachmentCardHyperlinkEventListener=(e,t,o)=>{e?.addEventListener("click",e=>handleClickAction(e,t,o),{signal:state?.eventControllerSignal,capture:!0}),e?.addEventListener("keydown",e=>handleClickAction(e,t,o),{signal:state?.eventControllerSignal,capture:!0})},addAcrobatIconToAttachmentCard=e=>{e?.forEach(e=>{const t=e?.closest("a");let o=t?.getAttribute("href"),a=!1;(o&&o.includes("drive.google.com")||o.includes("docs.google.com"))&&(a=!0,o=updateDrivePDFUrl(o));const n=getAttachmentCardElement(e),i=getParentElementForAcrobatIcon(n);if(o&&i){const r=getElementBasedOnSelector(t,"attachmentName","messageView")?.textContent;if(!isDefaultViewer()){const e=createAcrobatHyperlinkForAttachmentCard(o,r,n);handleFteTooltipForAttachmentCard(n,e),i.appendChild(e)}e.setAttribute("acrobat-icon-added","Y");addAttachmentCardHyperlinkEventListener(t,n,{url:o,name:r,isDriveAsset:a})}})},addAcrobatTouchPointInTheMessageView=()=>{try{const e=getMessageViewPDFAttachmentsWithoutAcrobatIcon();e&&e.length>0&&addAcrobatIconToAttachmentCard(e)}catch(e){sendAnalyticsEvent("DCBrowserExt:MessageView:ProcessingFailed",{source:"Gmail",workflow:"PreviewPDF"})}},removeAllTouchPoints=()=>{const e=document.querySelectorAll(`.${ATTACHMENT_CARD_HYPERLINK_CLASS}`);if(e?.length>0){const t=document.getElementsByClassName("acrobat-fte-tooltip-container");t?.length>0&&sendAnalytics(["DCBrowserExt:GmailFTE:MessageView:Dismissed"]);for(let t=0;t<e.length;t++)e[t]?.parentElement?.removeChild(e[t])}};export{addAcrobatTouchPointInTheMessageView,removeAllTouchPoints,getMessageView,getParentElementForAcrobatIcon,getAttachmentCardElement};