/*************************************************************************
* 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,sendAnalyticsEvent,sendAnalyticsEventOnce,ACROBAT_NON_PDF_PROCESSED}from"./util.js";import{createAcrobatIconElement,getFileDetails,buildAcrobatPromotionSource}from"../utils/util.js";import state from"./state.js";import{getAnalyticsType,getFileDetailsElementInNativeViewer,getParentElementForNativeViewerPrompt,isOrphanContentScript}from"./native-viewer-touch-point-service.js";import{acrobatTouchPointClicked}from"../utils/fte-utils.js";const GMAIL_CONVERT_TO_PDF_NATIVE_VIEWER="acrobat-covert-to-pdf-native-viewer-touch-point",GMAIL_CONVERT_TO_PDF_VERB_FTE_TOOLTIP_STORAGE_KEY="acrobat-gmail-convert-to-pdf-verb-fte-config",SOURCE="Gmail",WORKFLOW="ConvertToPdf",createNativeViewTouchpointTextElement=()=>{const e=document.createElement("span");return e.classList.add("acrobat-covert-to-pdf-native-viewer-touch-point-text"),e.textContent=state?.gmailConvertToPdfConfig?.acrobatTouchPointText,e},createNativeViewerTouchpoint=(e,t,o,i)=>{const n=document.createElement("a"),r=createAcrobatIconElement("acrobat-icon",state?.iconURL||"browser/images/acrobat_dc_appicon_128.png"),a=createNativeViewTouchpointTextElement(),c=buildAcrobatPromotionSource("gmail_chrome","convert_to_pdf"),s=createURLForAttachment(e,c,t,"createpdf");return n.classList.add(GMAIL_CONVERT_TO_PDF_NATIVE_VIEWER),n.setAttribute("href",s),n.setAttribute("target","_blank"),n.appendChild(r),n.appendChild(a),n.addEventListener("click",()=>{const t=getAnalyticsType(e,o);sendAnalyticsEvent("DCBrowserExt:DirectVerb:CTA:Clicked",{source:`${SOURCE}_${t}`,workflow:WORKFLOW,eventContext:i}),acrobatTouchPointClicked("acrobat-gmail-convert-to-pdf-verb-fte-config"),state.fteToolTip.eligibleFte.type="",state.fteToolTip.touchPointClicked=!0}),n},addTouchPointInNativeViewer=(e,t,o,i)=>{try{const{convertToPdfVisible:n}=state?.convertToPdfState;if(n)return;const r=document.getElementsByClassName(GMAIL_CONVERT_TO_PDF_NATIVE_VIEWER);if(r?.length>0)return;const a=createNativeViewerTouchpoint(e,t,o,i),c=getParentElementForNativeViewerPrompt();if(c&&c?.childNodes?.length>0){c.insertBefore(a,c.childNodes[0]),state.convertToPdfState.convertToPdfVisible=!0,state.convertToPdfState.convertToPdfAttachmentURL=e,state.convertToPdfState.convertToPdfAttachmentName=t,state.convertToPdfState.previousFileDetailsElement=getFileDetailsElementInNativeViewer();const n=getAnalyticsType(e,o);sendAnalyticsEvent("DCBrowserExt:DirectVerb:CTA:Shown",{source:`${SOURCE}_${n}`,workflow:WORKFLOW,eventContext:i}),window.innerWidth>1200&&chrome.runtime.sendMessage({main_op:"reRenderShowOneChild"})}else removeNativeViewerTouchPoint()}catch(e){removeNativeViewerTouchPoint()}},processForNativeViewer=(e,t)=>{const o=getFileDetailsElementInNativeViewer();if(o&&!isOrphanContentScript()){const i=getFileDetails(o);if(state?.convertToPdfState?.convertToPdfVisible&&state?.convertToPdfState?.convertToPdfAttachmentURL===e?.url)return;if(e?.name&&i&&e?.name!==i.title)return;addTouchPointInNativeViewer(e?.url,i.title,t,i?.mimeType)}else removeNativeViewerTouchPoint()},handleCommonClickAction=(e,t,o)=>{("click"===e.type||"keydown"===e.type&&"Enter"===e.key)&&setTimeout(()=>processForNativeViewer(t,o),800)},addCommonClickListener=(e,t,o,i=null)=>{if(!e)return;const n=e=>handleCommonClickAction(e,t,o),r=i&&i instanceof AbortSignal?{signal:i}:{};e.addEventListener("click",n,r),e.addEventListener("keydown",n,r)},markElementAsProcessed=e=>{e&&e.setAttribute(ACROBAT_NON_PDF_PROCESSED,"Y")},removeNativeViewerTouchPoint=e=>{if(!state.convertToPdfState?.convertToPdfVisible&&!e)return;const t=document.getElementsByClassName(GMAIL_CONVERT_TO_PDF_NATIVE_VIEWER);t?.length>0&&t[0].parentElement.removeChild(t[0]),state.convertToPdfState.convertToPdfVisible=!1},processForAlreadyShownNativeViewerTouchpoint=()=>{const{convertToPdfVisible:e,previousFileDetailsElement:t,convertToPdfAttachmentURL:o,convertToPdfAttachmentName:i}=state?.convertToPdfState;if(e){const e=getFileDetailsElementInNativeViewer();e?e!==t&&removeNativeViewerTouchPoint():(state.convertToPdfState.convertToPdfAttachmentURL=null,state.convertToPdfState.convertToPdfAttachmentName=null,state.convertToPdfState.previousFileDetailsElement=null,removeNativeViewerTouchPoint())}else if(t&&!isOrphanContentScript()){const e=getFileDetailsElementInNativeViewer();if(e&&t===e){const t=getFileDetails(e);addTouchPointInNativeViewer(o,i,"nativeViewer",t?.mimeType)}}},processForNativeViewerControlExperiment=()=>{const e=getFileDetailsElementInNativeViewer();if(!e||isOrphanContentScript())return;const t=getFileDetails(e),{metadata:o}=state?.gmailConvertToPdfConfig||{};Object.values(o?.fileExtToMimeTypeMap||{}).includes(t?.mimeType)&&sendAnalyticsEventOnce("DCBrowserExt:DirectVerb:NonPdf:Opened",{source:SOURCE,workflow:WORKFLOW,eventContext:t?.mimeType})},removeAllAcrobatNonPdfTouchPoints=()=>{if(document.querySelector(`img[${ACROBAT_NON_PDF_PROCESSED}]`)){const e=document.querySelectorAll(`img[${ACROBAT_NON_PDF_PROCESSED}="Y"]`);for(let t=0;t<e.length;t++){const o=e[t];o?.setAttribute(ACROBAT_NON_PDF_PROCESSED,"N")}}removeNativeViewerTouchPoint(!0)};export{removeNativeViewerTouchPoint,processForAlreadyShownNativeViewerTouchpoint,markElementAsProcessed,addCommonClickListener,processForNativeViewerControlExperiment,removeAllAcrobatNonPdfTouchPoints};