/*************************************************************************
* 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 state from"./state.js";import*as messageViewTouchPointService from"./message-view-touch-point-service.js";import*as listViewTouchPointService from"./list-view-touch-point-service.js";import*as nativeViewerTouchPointService from"./native-viewer-touch-point-service.js";import{sendAnalytics,extractFileIdFromDriveUrl}from"../utils/util.js";const ACROBAT_LISTENER_ADDED="acrobat-listener-added",ACROBAT_PROCESSED_ATTRIBUTE="acrobat-icon-added",ACROBAT_NON_PDF_PROCESSED="acrobat-non-pdf-processed",ACROBAT_CONTENT_SCRIPT_DISCONNECT_START="acrobatContentScriptDisconnectStart",LIST_VIEW="LIST_VIEW",DRIVE_USERCONTENT_URL="drive.usercontent.google.com",DRIVE_PDF_URL_PATTERN=`https://${DRIVE_USERCONTENT_URL}/download?id=_id&authuser=_authuser`,CONVERT_TO_PDF_PROMOTION_SOURCE="gmail_chrome-convert_to_pdf",DRIVE_DOWNLOAD_URL_PATTERN="https://drive.google.com/uc?id=_id&export=download&authuser=_authuser",getElementBasedOnSelector=(e,t,r)=>{if(e){const o=state?.gmailConfig?.selectors,n=o&&o[r]&&o[r][t];for(let t=0;t<n?.length;t++){let r=e.querySelector(n[t]);if(r)return r}}return null},getClosestElementBasedOnSelector=(e,t,r)=>{if(e){const o=state?.gmailConfig?.selectors,n=o&&o[r]&&o[r][t];for(let t=0;t<n?.length;t++){let r=e.closest(n[t]);if(r)return r}}return null},getArrayElementBasedOnSelector=(e,t,r)=>{if(e){const o=state?.gmailConfig?.selectors,n=o&&o[r]&&o[r][t];for(let t=0;t<n?.length;t++)if(e?.querySelector(n[t])){const r=e?.querySelectorAll(n[t]);if(r&&r.length>0)return r}}return null},getElementsByClassNameSelectors=(e,t,r)=>{if(e){const o=state?.gmailConfig?.selectors,n=o&&o[r]&&o[r][t];for(const t of n||[]){const r=e.getElementsByClassName(t);if(r?.length>0)return Array.from(r)}}return null},getFileDetailsElementInNativeViewer=()=>getElementBasedOnSelector(document,"lightBoxViewerFileDetails","nativeViewer"),getParentElementForNativeViewerPrompt=()=>getElementBasedOnSelector(document,"nativeViewerPromptParentElement","nativeViewer"),isOrphanContentScript=()=>!chrome?.runtime?.id,createURLForAttachment=(e,t,r,o="")=>{let n=e;return n="gmail_chrome-convert_to_pdf"===t?e?.replace("disp=inline","disp=safe"):e?.replace("disp=safe","disp=inline"),t&&(n=n+"&acrobatPromotionSource="+t),state?.viewerURLPrefix&&(n=`${state.viewerURLPrefix}?pdfurl=${encodeURIComponent(n)}`,r&&(n=n+"&pdffilename="+encodeURIComponent(r)),o&&(n=n+"&acrobatPromotionWorkflow="+encodeURIComponent(o))),n},getUserId=()=>{const e=window.location?.pathname?.split("/");return e?.length>3?e[3]:"0"},updateDrivePDFUrl=e=>{const t=extractFileIdFromDriveUrl(e);return t?formDrivePDFUrl(t):""},updateDriveNonPDFUrl=e=>{const t=extractFileIdFromDriveUrl(e);return t?DRIVE_DOWNLOAD_URL_PATTERN.replace("_id",t).replace("_authuser",getUserId()):""},formDrivePDFUrl=e=>DRIVE_PDF_URL_PATTERN.replace("_id",e).replace("_authuser",getUserId()),isDriveFileDirectDownloadLink=e=>e?.includes(DRIVE_USERCONTENT_URL),removeAllAcrobatTouchPoints=()=>{if(document.querySelector("img[acrobat-icon-added]")){const e=document.querySelectorAll('img[acrobat-icon-added="Y"]');for(let t=0;t<e.length;t++)e[t]?.setAttribute("acrobat-icon-added","N")}if(document.querySelector(`[${ACROBAT_LISTENER_ADDED}]`)){const e=document.querySelectorAll(`[${ACROBAT_LISTENER_ADDED}="Y"]`);for(let t=0;t<e.length;t++)e[t]?.setAttribute(ACROBAT_LISTENER_ADDED,"N")}messageViewTouchPointService?.removeAllTouchPoints(),listViewTouchPointService?.removeAllTouchPoints(),nativeViewerTouchPointService?.removeAllTouchPoints()},normalizeFileName=e=>{let t=e;const r=["&","'",'"',"<",">","/","=","`"," ","!","#","$","%","(",")","*","+","-",":",";","?","@","[","\\","]","^","{","|","}","~"].reduce((e,t)=>{const r=t.charCodeAt(0).toString(16);return e[`x${r}`]=t,e[`x${r.toUpperCase()}`]=t,e},{});for(const[e,o]of Object.entries(r))t.includes(e)&&(t=t.replaceAll(e,o));return t},getAttachmentByFileTypeSelectors=(e=[],t=[],r=ACROBAT_NON_PDF_PROCESSED)=>{const o=[];for(const n of e)n?.src&&t.some(e=>n?.src.endsWith(e))&&"Y"!==n.getAttribute(r)?o.push(n):n?.src.endsWith("undefined")||n.setAttribute(r,"Y");return o},sendAnalyticsEvent=(e,t={})=>{try{chrome.runtime.sendMessage({main_op:"analytics",analytics:[[e,t]]})}catch(e){}},eventString=new Set,sendAnalyticsEventOnce=(e,t={})=>{try{const r=`${e}:${JSON.stringify(t)}`;eventString.has(r)||(eventString.add(r),sendAnalyticsEvent(e,t))}catch(e){}},getMimeTypeFromAttachmentName=(e,t)=>{if(!e)return null;const r=e?.toLowerCase();for(const[e,o]of Object.entries(t))if(r.endsWith(e))return o;return null},shouldProcessForListView=(e,t)=>!!e?.enableGmailTouchPoint&&(t?.enableImplicitDefaultViewershipFeature&&t?.isAcrobatDefaultForSurface||e?.touchPointSettingEnabled||e?.isAcrobatDefaultForSurface),shouldProcessForConvertToPDF=(e,t)=>e?.enableConvertToPdfTouchpointInGmail&&t?.touchPointSettingEnabled;export{ACROBAT_CONTENT_SCRIPT_DISCONNECT_START,ACROBAT_LISTENER_ADDED,LIST_VIEW,getFileDetailsElementInNativeViewer,getArrayElementBasedOnSelector,getElementBasedOnSelector,getClosestElementBasedOnSelector,isOrphanContentScript,createURLForAttachment,getUserId,isDriveFileDirectDownloadLink,updateDrivePDFUrl,removeAllAcrobatTouchPoints,getParentElementForNativeViewerPrompt,getElementsByClassNameSelectors,getAttachmentByFileTypeSelectors,sendAnalyticsEvent,ACROBAT_NON_PDF_PROCESSED,getMimeTypeFromAttachmentName,normalizeFileName,updateDriveNonPDFUrl,sendAnalyticsEventOnce,shouldProcessForListView,shouldProcessForConvertToPDF};