/*************************************************************************
* 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{sendAnalytics,sendAnalyticsOncePerMonth,sendErrorLog,getClosestElementBySelectors,getElementFromParent,getFirstElementBasedOnSelectors}from"../utils/util.js";import{createFteTooltip,updateFteToolTipCoolDown,initFteStateAndConfig,shouldShowFteTooltip}from"../utils/fte-utils.js";import state from"./state.js";const GMAIL_COMPRESS_PDF_TOUCH_POINT_DRIVE_CLASS="gmail-compress-pdf-drive-touch-point",GMAIL_COMPRESS_PDF_FTE_TOOLTIP_CONTAINER_CLASS="acrobat-fte-tooltip-container",GMAIL_COMPRESS_PDF_FTE_TOOLTIP_STORAGE_KEY="acrobat-gmail-compress-pdf-fte-config",GMAIL_COMPRESS_PDF_FTE_TOOLTIP_BUTTON_CLASS="acrobat-fte-tooltip-button",GMAIL_COMPRESS_PDF_FTE_TOOLTIP_VIEWPORT_BUTTON_GAP=69,GMAIL_COMPRESS_PDF_FTE_TOOLTIP_HEIGHT=150,FTE_TOOLTIP_TYPE_COMPRESS_PDF_FILE="compressPdfFile",FTE_TOOLTIP_TYPE_COMPRESS_PDF_DRIVE="compressPdfDrive",GMAIL_COMPRESS_PDF_FILE_FTE_TOOLTIP_CONTAINER_CLASS="acrobat-fte-tooltip-container-compressPdfFile",GMAIL_COMPRESS_PDF_DRIVE_FTE_TOOLTIP_CONTAINER_CLASS="acrobat-fte-tooltip-container-compressPdfDrive",GMAIL_COMPRESS_TOUCH_POINT_DRIVE_TITLE_CLASS="gmail-compress-pdf-drive-title-text",GMAIL_ATTACHMENT_CONTAINER_WIDTH_GAP=110,GMAIL_COMPRESS_PDF_TOUCH_POINT_FTE_TOOLTIP_GAP=12,fteDismissEventType=["click","mousedown","contextmenu","keydown"],fteDismissEventKey=["Enter","ArrowLeft","ArrowRight","Escape"];let fteEventListenersAdded=!1;const alignFTETooltipRelativeToTouchPoint=(e,t,o="above")=>{const s=e.getBoundingClientRect(),i=t.getBoundingClientRect();let n,r;"above"===o?(n=s.top-i.height-12,r=s.right-i.width):(n=s.bottom+12,r=s.left),t.style.top=`${n}px`,t.style.left=`${r}px`},alignFTETooltip=(e,t)=>{const o=getElementFromParent([e],document);if(!o)return;const s=state?.compressPdfFteState.compressPDFTouchPointWithFTE;if(!s)return void o?.parentNode?.removeChild(o);const i=t?t(s):"above";alignFTETooltipRelativeToTouchPoint(s,o,i)},alignFTETooltipToButton=()=>{const e=state?.compressPdfFteState.compressPDFTouchPointWithFTE;e.classList.contains("gmail-compress-pdf-drive-touch-point")?alignFTETooltip("acrobat-fte-tooltip-container-compressPdfDrive",e=>getClosestElementBySelectors(e,state?.gmailCompressPDFConfig?.selectors?.replyAndForwardComposeBox)?"above":"below"):alignFTETooltip("acrobat-fte-tooltip-container-compressPdfFile",e=>"above")},updateAttachmentContainerTitle=(e,t,o=0)=>{const s=state?.gmailCompressPDFConfig?.selectors?.[t]?.pdfAttachmentTitle;let i=null;if(i="drive"===t?getFirstElementBasedOnSelectors(s,e):getElementFromParent(s,e),!i)return;if("drive"===t){i.classList.add("gmail-compress-pdf-drive-title-text");const t=e.querySelector("a");t&&(t.style.width="auto")}const n=e.getBoundingClientRect().width-o-110;i.style.setProperty("max-width",`${n}px`,"important")},updateAttachmentContainer=e=>{if(e.classList.contains("gmail-compress-pdf-drive-touch-point")){const t=getClosestElementBySelectors(e,state?.gmailCompressPDFConfig?.selectors?.drive?.attachmentContainer);updateAttachmentContainerTitle(t,"drive")}else{const t=getClosestElementBySelectors(e,state?.gmailCompressPDFConfig?.selectors?.file?.attachmentContainer);updateAttachmentContainerTitle(t,"file")}},removeFteTooltip=()=>{const e=getElementFromParent(["acrobat-fte-tooltip-container"],document);e&&e.parentNode&&e.parentNode.removeChild(e);const t=state?.compressPdfFteState?.compressPDFTouchPointWithFTE;t&&updateAttachmentContainer(t),t?.remove(),state&&state.compressPdfFteState&&(state.compressPdfFteState.compressPDFTouchPointWithFTE=null),state?.abortCompressPdfFteController(),fteEventListenersAdded=!1},handleFteTooltipDismissal=(e,t)=>{const o=document.getElementsByClassName("acrobat-fte-tooltip-container")[0];if(!o)return;("scroll"===e.type||"resize"===e.type||(fteDismissEventType.includes(e.type)||fteDismissEventKey.includes(e.key))&&!o.contains(e.target))&&(removeFteTooltip(),sendAnalyticsOncePerMonth("DCBrowserExt:DirectVerb:Fte:Dismissed",{source:"gmail_chrome",workflow:t}))},handleFteButtonClick=(e,t)=>{e.preventDefault(),removeFteTooltip(),sendAnalyticsOncePerMonth("DCBrowserExt:DirectVerb:Fte:Closed",{source:"gmail_chrome",workflow:t})},addEventListenersToDismissFTE=e=>{if(fteEventListenersAdded)return;const t=state?.initCompressPdfFteAbortController();if(!t)return;const o=getElementFromParent(["acrobat-fte-tooltip-button"],document);o&&o.addEventListener("click",t=>handleFteButtonClick(t,e),{signal:t});[{target:document,type:"scroll",capture:!0},{target:document,type:"keydown",capture:!0},{target:document,type:"click",capture:!0},{target:document,type:"mousedown",capture:!0},{target:document,type:"contextmenu",capture:!0},{target:window,type:"resize",capture:!1}].forEach(({target:o,type:s,capture:i})=>{o.addEventListener(s,t=>handleFteTooltipDismissal(t,e),{signal:t,capture:i})}),fteEventListenersAdded=!0},checkFTEWillBeVisibleInViewPort=(e,t)=>new Promise(o=>{requestAnimationFrame(()=>{requestAnimationFrame(()=>{const s=e.getBoundingClientRect(),i=window.innerHeight;s.bottom+t>i?o(!1):o(!0)})})}),isEnoughSpaceToShowFTE=(e,t)=>{const o=getClosestElementBySelectors(t,state?.gmailCompressPDFConfig?.selectors?.replyAndForwardComposeBox);return checkFTEWillBeVisibleInViewPort(t,"drive"!==e||o?69:150)},shouldShowFTE=async()=>{const e=await initFteStateAndConfig("acrobat-gmail-compress-pdf-fte-config"),t=state?.gmailCompressPDFConfig?.gmailCompressPDFFteConfig,o=state?.gmailCompressPDFConfig?.enableGmailCompressPDFFteTooltip;return shouldShowFteTooltip(t,e,o)},addFte=async()=>{try{const e=state?.compressPdfFteState.compressPDFTouchPointWithFTE;if(!e)return;const t=e.classList.contains("gmail-compress-pdf-drive-touch-point");let o="compress_pdf_file",s="compressPdfFile";t&&(o="compress_pdf_drive",s="compressPdfDrive");getClosestElementBySelectors(e,state?.gmailCompressPDFConfig?.selectors?.replyAndForwardComposeBox)&&(s="compressPdfFile");const i=state?.gmailCompressPDFConfig?.fteToolTipStrings,n=state?.gmailCompressPDFConfig?.gmailCompressPDFFteConfig,r=createFteTooltip(i,s);if(!r)return;document.body.appendChild(r),addEventListenersToDismissFTE(o),sendAnalytics([["DCBrowserExt:DirectVerb:CTA:Shown",{source:"gmail_chrome",workflow:o}]]),sendAnalyticsOncePerMonth("DCBrowserExt:DirectVerb:Fte:Shown",{source:"gmail_chrome",workflow:o}),await updateFteToolTipCoolDown(n,"acrobat-gmail-compress-pdf-fte-config")}catch(e){sendErrorLog("GmailCompressPDFFte","Failure in adding FTE to compress PDF touch point")}};export{addFte,shouldShowFTE,isEnoughSpaceToShowFTE,alignFTETooltipToButton,updateAttachmentContainerTitle};