/*************************************************************************
* 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{getParentElementForNativeViewerPrompt,updateDriveNonPDFUrl}from"../../gmail/util.js";import state from"../../gmail/state.js";import ImageToolsDropdown from"../image-tools-dropdown.js";import SingleClickCTA from"../single-click-cta.js";import expressUtils from"../express-utils.js";import{getFileDetails}from"../../utils/util.js";let previewEntryPointId="express-gmail-native-viewer-entry-point";const touchpoint="gmailNativeViewer",gmailExpressStateKey="gmailExpressState",gmailImageEditDropdownStateKey="gmailImageEditDropdownState",convertToPDFUnsupported=["image/webp","image/heif"],_getVisibleViewer=()=>{const e=state?.expressConfig?.selectors?.activeViewElementId??["drive-active-item-info"],t=e.length;for(let s=0;s<t;s+=1){const t=document.getElementById(e[s]);if(t){const e=getFileDetails(t);return state.expressState.mimeType=e?.mimeType,state.expressState.imageName=e?.title,t.parentElement}}return null},_sendErrorLog=(e,t)=>{chrome.runtime.sendMessage({main_op:"log-error",log:{message:e,error:t}})},_setDriveURL=()=>{try{const e=state?.expressConfig?.selectors?.imageMessageViewPreview,t=state.expressState.imageName;if(!e||!t)return;const s=expressUtils.getElementsFromClassNames(document,e);if(0===s.length)return;Array.from(s).some(e=>Array.from(e.getElementsByTagName("a")).some(e=>{const s=(e.textContent||"").trim();return!(!e.href||!e.href.includes("drive.google.com")&&!e.href.includes("docs.google.com")||s!==t)&&(state.expressState.imageDriveURL=updateDriveNonPDFUrl(e.href),!0)}))}catch(e){_sendErrorLog("Error setting drive URL","Error in _setDriveURL")}},_isImagePreviewed=()=>{const e=(state?.expressConfig?.selectors??{}).imgPrev??["aLF-aPX-J1-J3"],t=_getVisibleViewer();if(!t)return null;const s=expressUtils.getElementsFromClassNames(t,e);if(s&&s.length>0){const e=s[0].getAttribute("src");return e&&e.includes("googleusercontent.com")&&state?.expressConfig?.enableGmailConvertToPdfInImages&&_setDriveURL(),e}return null},_removeContextualFte=()=>{const e=document.getElementById("express-contextual-fte");e&&e.remove()},_removeTooltipIfFteRendered=()=>{document.getElementById("express-contextual-fte")&&state.expressState.tooltip&&(state.expressState.tooltip.tooltip.remove(),state.expressState.tooltip=null)},_setTouchpointClickedForFteState=async e=>{_removeContextualFte();const t={imageTools:{storageKey:"gmailImageEditDropdownState",field:"previewTouchpointUsed"},editImage:{storageKey:"gmailExpressState",field:"previewTouchpointUsed"}}[e];if(!t)return;const{storageKey:s,field:i}=t,r=(await chrome.storage.local.get(s))[s]||{};r[i]=!0,chrome.storage.local.set({[s]:r})},_getImageSrc=e=>{const t=state.expressState?.imageURL;if("convertToPdf"===e&&t){if(state.expressState?.imageDriveURL)return state.expressState.imageDriveURL;const e=new URL(t);return e.searchParams.set("view","att"),e.searchParams.delete("disp"),e.searchParams.delete("sz"),e.toString()}return t},_clickCallback=(e,t="editImage")=>{const s=_getImageSrc(e);"convertToPdf"===e?chrome.runtime.sendMessage({main_op:"execute-direct-verb",fileUrl:s,fileName:state.expressState.imageName,viewerURL:state?.viewerURLPrefix,promotionSource:"gmail_chrome-convert_to_pdf_from_images",verb:"createpdf"}):chrome.runtime.sendMessage({main_op:"launch-express",imgUrl:s,intent:e,touchpoint:touchpoint}),_setTouchpointClickedForFteState(t)},toggleButtonSize=(e,t,s="editImage")=>{const i=e.getElementsByClassName("cc440d50ba-express-entrypoint-button-text")[0],r=e.getElementsByClassName("cc440d50ba-express-entrypoint-button-icon")[0];if(i&&(i.style.display=t?"none":""),e.style.width=t?"32px":"","imageTools"===s){const s=r?.getElementsByTagName("img")[0];e.style.height=t?"32px":"",e.style.padding=t?"0":"",e.style.justifyContent=t?"center":"",s&&(s.style.marginRight=t?"0":"")}else r.style.padding=t?"7px 9px 7px 5px":"",r.style.width=t?"32px":""},updateButtonStyles=(e,t="editImage")=>{if(!e)return;e.style.visibility="hidden",toggleButtonSize(e,!1,t);const s=e.offsetWidth,i=window.innerWidth-s<1e3;toggleButtonSize(e,i,t),e.style.visibility="visible"},_handleResponsiveButton=(e,t="editImage")=>{const s=e.getElementsByClassName("cc440d50ba-express-entrypoint-button")[0];updateButtonStyles(s,t),state.expressState.resizeHandler=()=>updateButtonStyles(s,t),window.addEventListener("resize",state.expressState.resizeHandler)},_addTouchpoint=async()=>{const e=state?.expressConfig?.enableGmailConvertToPdfInImages,t=state.expressState.mimeType;let s=new SingleClickCTA,i="editImage";e&&(convertToPDFUnsupported.includes(t)?s=new SingleClickCTA:(s=new ImageToolsDropdown,previewEntryPointId="edit-tools-dropdown-gmail-native-viewer-entry-point",i="imageTools")),state.expressState.nativeViewerTouchpointVisible=!0;const r=await s.renderMenuButton(e=>_clickCallback(e,i),touchpoint,{mimeType:t});r.id=previewEntryPointId;const o=getParentElementForNativeViewerPrompt();if(o&&o?.childNodes?.length>0){o.insertBefore(r,o.childNodes[0]),_handleResponsiveButton(r,i),state.expressState.tooltip=s.attachTooltip("bottom"),chrome.runtime.sendMessage({main_op:"reRenderShowOneChild"});const t="DCBrowserExt:Express:Gmail:NativeViewerEntryPoint:Shown";e||state?.expressConfig?.enableGmailConvertToPdfInImagesControl?expressUtils.sendAnalyticsEvent([[t]]):expressUtils.sendAnalyticsEventOncePerDay([[t]])}},removeExpressTouchpoints=()=>{["express-gmail-native-viewer-entry-point","edit-tools-dropdown-gmail-native-viewer-entry-point"].forEach(e=>{const t=document.getElementById(e);t&&t.parentElement?.removeChild(t)}),state.expressState?.resizeHandler&&(window.removeEventListener("resize",state.expressState.resizeHandler),state.expressState.resizeHandler=null),state.expressState.imageURL=null,state.expressState.imageDriveURL=null,state.expressState.nativeViewerTouchpointVisible=!1,state.expressState.tooltip?.tooltip?.remove(),state.expressState.tooltip=null},addExpressNativeViewerTouchpoint=()=>{_removeTooltipIfFteRendered();const e=_isImagePreviewed();if(!e)return void removeExpressTouchpoints();const{nativeViewerTouchpointVisible:t,imageURL:s}=state?.expressState;s!==e&&t&&state?.expressConfig?.enableGmailConvertToPdfInImages&&removeExpressTouchpoints(),t||_addTouchpoint(),s!==e&&(state.expressState.imageURL=e)};export{addExpressNativeViewerTouchpoint,removeExpressTouchpoints};