/*************************************************************************
* 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"../../outlook/state.js";import SingleClickCTA from"../single-click-cta.js";import expressUtils from"../express-utils.js";class ExpressOutlookTouchpointService{previewEntryPointId="express-outlook-native-viewer-entry-point";attachmentHoverMenuEntryPointId="express-outlook-attachment-hover-menu-entry-point";touchpoint="outlookNativeViewer";attachmentHoverMenuTouchpoint="outlookAttachmentView";outlookExpressStateKey="outlookExpressState";nativeViewerSelectors=["m79Ne"];imgSelectors=["bk93t"];headerSelectors=["ms-CommandBar-primaryCommand"];attachmentChevronSelectors=["o4euS"];imageHoverElementSelectors=[".l6VuP.KV4XY"];attachmentHoverDropdownMenuSelectors=["ms-ContextualMenu-list is-open"];hoverMenuElementSelectors=[".ms-Layer.ms-Layer--fixed"];expressButtonSelector="cc440d50ba-express-entrypoint-button";expressButtonTextSelector="cc440d50ba-express-entrypoint-button-text";expressButtonIconSelector="cc440d50ba-express-entrypoint-button-icon";constructor(){this.nativeViewerSelectors=state?.expressConfig?.selectors?.nativeViewerSelectors||this.nativeViewerSelectors,this.imgSelectors=state?.expressConfig?.selectors?.imgSelectors||this.imgSelectors,this.headerSelectors=state?.expressConfig?.selectors?.headerSelectors||this.headerSelectors,this.attachmentChevronSelectors=state?.expressConfig?.selectors?.attachmentChevronSelectors||this.attachmentChevronSelectors,this.imageHoverElementSelectors=state?.expressConfig?.selectors?.imageHoverElementSelectors||this.imageHoverElementSelectors,this.attachmentHoverDropdownMenuSelectors=state?.expressConfig?.selectors?.attachmentHoverDropdownMenuSelectors||this.attachmentHoverDropdownMenuSelectors,this.hoverMenuElementSelectors=state?.expressConfig?.selectors?.hoverMenuElementSelectors||this.hoverMenuElementSelectors}_isImagePreviewed=()=>{const e=expressUtils.getElementsFromClassNames(document,this.nativeViewerSelectors)[0];if(!e)return null;const t=expressUtils.getElementsFromClassNames(e,this.imgSelectors)[0];if(t){const e=t.getElementsByTagName("IMG")[0];return e?.getAttribute("src")}};_isImageAttachmentChevronClicked=e=>{if(e.target){const t=expressUtils.getClosestElementBasedOnSelectors(e.target,this.imageHoverElementSelectors);if(!t)return null;const s=t.previousElementSibling;if(!s)return null;const o=s.getElementsByTagName("IMG")[0];if(!o)return null;const n=o.src;if(!n)return null;const r=new URL(n);return r.searchParams.set("thumbnailType",2),r.toString()}};addClickEventListenerToAttachmentChevron=()=>{const e=expressUtils.getElementsFromClassNames(document,this.attachmentChevronSelectors);if(e&&0!==e.length)for(const t of e)t.hasAttribute("express-attachment-chevron-click-listener-added")||(t.setAttribute("express-attachment-chevron-click-listener-added","true"),t.addEventListener("click",e=>{const t=this._isImageAttachmentChevronClicked(e);state.expressState.imageURLForAttachmentHoverMenu=t,t&&this._addAttachmentHoverMenuTouchpoint()}))};_addAttachmentHoverMenuTouchpoint=async()=>{const e=new SingleClickCTA,t=await e.renderMenuButton(this._clickCallbackForAttachmentHoverMenu,this.attachmentHoverMenuTouchpoint);t.id=this.attachmentHoverMenuEntryPointId;const s=expressUtils.getElementsFromClassNames(document,this.attachmentHoverDropdownMenuSelectors)[0];if(s){this.hoverMenuElement=expressUtils.getClosestElementBasedOnSelectors(s,[this.hoverMenuElementSelectors]);const o=document.createElement("hr");o.style.cssText="\n                margin: 0 4px;\n                border: 0;\n                border-top: 1px solid #d3d3d3;\n            ",o.className="cc440d50ba-express-separator",s.appendChild(o),s.appendChild(t),state.expressState.tooltipForAttachmentHoverMenu=e.attachTooltip("left"),chrome.runtime.sendMessage({main_op:"reRenderShowOneChild"}),expressUtils.sendAnalyticsEventOncePerDay([["DCBrowserExt:Express:Outlook:AttachmentHoverMenuEntryPoint:Shown"]])}};_removeContextualFteAndTooltipIfCtaButtonRemoved=()=>{const e=document.getElementById(this.attachmentHoverMenuEntryPointId),t=document.getElementById(this.previewEntryPointId);e||t||(this._removeContextualFte(),this._removeTooltip()),e||this._removeTooltipForAttachmentHoverMenu()};_removeContextualFte=()=>{const e=document.getElementById("express-contextual-fte");e&&e.remove()};_removeTooltip=()=>{const e=document.getElementsByClassName("cc440d50ba-tooltiptext");for(const t of e)t.remove()};_removeTooltipForAttachmentHoverMenu=()=>{const e=document.getElementsByClassName("cc440d50ba-tooltiptext outlookAttachmentView");for(const t of e)t.remove()};_removeTooltipIfFteRendered=()=>{document.getElementById("express-contextual-fte")&&this._removeTooltip()};_setTouchpointClickedForFteState=async e=>{this._removeContextualFte();let t=await chrome.storage.local.get(this.outlookExpressStateKey);switch(t=t?.outlookExpressState?t.outlookExpressState:{},e){case this.touchpoint:t.previewTouchpointUsed=!0;break;case this.attachmentHoverMenuTouchpoint:t.attachmentHoverMenuTouchpointUsed=!0}chrome.storage.local.set({[this.outlookExpressStateKey]:t})};_clickCallback=e=>{this._genericClickCallback(e,this.touchpoint,"imageURL")};_clickCallbackForAttachmentHoverMenu=e=>{this.hoverMenuElement&&(this.hoverMenuElement.remove(),this.hoverMenuElement=null,this._removeTooltip(),state.expressState.tooltipForAttachmentHoverMenu=null),this._genericClickCallback(e,this.attachmentHoverMenuTouchpoint,"imageURLForAttachmentHoverMenu")};_genericClickCallback=(e,t,s)=>{const o=new URL(state.expressState[s]);state.token&&o.searchParams.set("token",state.token);const n=o.toString();chrome.runtime.sendMessage({main_op:"launch-express",imgUrl:n,intent:e,touchpoint:t}),this._setTouchpointClickedForFteState(t)};_moveTouchpointAtEndIfNotAlreadyThere=e=>{e.nextElementSibling&&this.removeExpressTouchpoints()};detectDarkMode=()=>{if(document.documentElement.className.includes("dark")){const e=document.getElementById(this.previewEntryPointId);e&&e.classList.add("outlookDark");const t=document.getElementById(this.attachmentHoverMenuEntryPointId);t&&(t.classList.add("outlookDark"),state.expressState.tooltipForAttachmentHoverMenu?.tooltip?.classList.add("outlookDark"))}};toggleButtonSize=(e,t)=>{e.style.width=t?"32px":"";const s=e.getElementsByClassName(this.expressButtonTextSelector)[0],o=e.getElementsByClassName(this.expressButtonIconSelector)[0];s&&(s.style.display=t?"none":""),o&&(o.style.padding=t?"7px 9px 7px 5px":"",o.style.width=t?"32px":"")};updateButtonStyles=e=>{if(!e)return;e.style.visibility="hidden",this.toggleButtonSize(e,!1);const t=e.offsetWidth,s=window.innerWidth-t<1e3;this.toggleButtonSize(e,s),e.style.visibility="visible"};_handleResponsiveButton=e=>{const t=e.getElementsByClassName(this.expressButtonSelector)[0];this.updateButtonStyles(t),state.expressState.resizeHandler=()=>this.updateButtonStyles(t),window.addEventListener("resize",state.expressState.resizeHandler)};_addExpressTouchpoint=async()=>{const e=new SingleClickCTA,t=await e.renderMenuButton(this._clickCallback,this.touchpoint);t.id=this.previewEntryPointId;const s=expressUtils.getElementsFromClassNames(document,this.nativeViewerSelectors)[0];if(s){const o=expressUtils.getElementsFromClassNames(s,this.headerSelectors)[0];if(o&&o?.childNodes?.length>0){const s=document.createElement("div");s.style.cssText="\n                    width: 1px;\n                    height: 28px;\n                    background-color: #E5E5E5;\n                    margin: 0 8px;\n                    display: inline-block;\n                    vertical-align: middle;\n                ",s.className="cc440d50ba-express-separator",state.expressState.expressTouchpointAdded||(state.expressState.expressTouchpointAdded=!0,o.appendChild(s),o.appendChild(t),this._handleResponsiveButton(t),state.expressState.tooltip=e.attachTooltip("bottom"),chrome.runtime.sendMessage({main_op:"reRenderShowOneChild"}),expressUtils.sendAnalyticsEventOncePerDay([["DCBrowserExt:Express:Outlook:NativeViewerEntryPoint:Shown"]]))}}};addExpressOutlookTouchpoint=()=>{this._removeTooltipIfFteRendered();const e=this._isImagePreviewed();if(!e)return void this.removeExpressTouchpoints();state.expressState.imageURL!==e&&(state.expressState.imageURL=e);const t=document.getElementById(this.previewEntryPointId);t?this._moveTouchpointAtEndIfNotAlreadyThere(t):(state.expressState.expressTouchpointAdded=!1,this._addExpressTouchpoint())};removeExpressTouchpoints=()=>{const e=document.getElementById(this.previewEntryPointId);if(e){state.expressState?.resizeHandler&&(window.removeEventListener("resize",state.expressState.resizeHandler),state.expressState.resizeHandler=null),state.expressState.imageURL=null;const t=document.getElementsByClassName("cc440d50ba-express-separator")[0];t&&t.parentElement?.removeChild(t),e.parentElement?.removeChild(e)}state.expressState.tooltip?.tooltip?.remove(),state.expressState.tooltip=null};processForOutlook=()=>{state.expressConfig?.enableOutlookNVExpressMenu&&this.addExpressOutlookTouchpoint(),state.expressConfig?.enableOutlookAttachmentViewExpressMenu&&(this.addClickEventListenerToAttachmentChevron(),this._removeContextualFteAndTooltipIfCtaButtonRemoved()),(state.expressConfig?.enableOutlookNVExpressMenu||state.expressConfig?.enableOutlookAttachmentViewExpressMenu)&&this.detectDarkMode()}}const expressOutlookTouchpointService=new ExpressOutlookTouchpointService;export default expressOutlookTouchpointService;