/*************************************************************************
* 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"../../gmail/state.js";import{getArrayElementBasedOnSelector}from"../../gmail/util.js";import ExpressHoverCTA from"../express-hover-cta.js";import expressUtils from"../express-utils.js";const messageViewTouchpoint="gmailMessageView",EXPRESS_PROCESSED_ATTRIBUTE="express-processed",gmailExpressStateKey="gmailExpressState",MIN_IMAGE_WIDTH=300,MIN_IMAGE_HEIGHT=150,_getMessageView=()=>getArrayElementBasedOnSelector(document,"messageView","messageView"),_imageSizeValid=e=>e.width>=300&&e.height>=150,_getMessageViewImageElement=e=>{const t=e.previousElementSibling;return t&&"IMG"===t.tagName?t:null},_showExpressButton=e=>{state.expressState.hoverCTA.shouldShow&&(state.expressState.hoverCTA.minimized?e.minimizeButton():(clearTimeout(state.expressState.hoverCTA.minimizeTimeout),state.expressState.hoverCTA.minimizeTimeout=setTimeout(()=>{e.minimizeButton(),state.expressState.hoverCTA.minimized=!0},5e3)),expressUtils.sendAnalyticsEventOncePerDay([["DCBrowserExt:Express:GmailMessageView:EntryPoint:Shown"]]),e.showButton())},_hideExpressButton=e=>{clearTimeout(state.expressState.hoverCTA.minimizeTimeout),e.hideButton()},_addHoverEventListeners=(e,t,s)=>{t.addEventListener("mouseenter",()=>{_showExpressButton(e)}),s.addEventListener("mouseenter",()=>{_showExpressButton(e)}),t.addEventListener("mouseleave",t=>{t.relatedTarget&&s.contains(t.relatedTarget)||_hideExpressButton(e)}),s.addEventListener("mouseleave",s=>{s.relatedTarget&&t.contains(s.relatedTarget)||_hideExpressButton(e)})},_setTouchpointClickedForFteState=async()=>{let e=await chrome.storage.local.get("gmailExpressState");e=e?.gmailExpressState?e.gmailExpressState:{},e.hoverTouchpointUsed=!0,chrome.storage.local.set({[gmailExpressStateKey]:e})},_getClickCallback=e=>t=>{chrome?.runtime?.id?(chrome.runtime.sendMessage({main_op:"launch-express",imgUrl:e,intent:t,touchpoint:"gmailMessageView"}),_setTouchpointClickedForFteState()):removeAllExpressMessageViewTouchpoints()},removeAllExpressMessageViewTouchpoints=()=>{Array.from(document.getElementsByClassName("gmailMessageView")).forEach(e=>{e.parentNode.removeChild(e)})},addExpressMessageViewTouchpoint=async()=>{if(!state?.expressState?.hoverCTA?.shouldShow)return;const e=_getMessageView();e&&e.length>0&&e.forEach(async e=>{const t=expressUtils.getElementsFromClassNames(e,state?.expressConfig?.selectors?.imageActionTray??["a6S"]);if(t&&t.length>0)for(const e of t){const t=_getMessageViewImageElement(e);if(t&&_imageSizeValid(t)&&!e.hasAttribute("express-processed")){e.setAttribute("express-processed","Y");const s=new ExpressHoverCTA;await s.renderButton({surface:"gmailMessageView",onClickCallback:_getClickCallback(t.src),onHideClickCallback:()=>{state.expressState.hoverCTA.shouldShow=!1,removeAllExpressMessageViewTouchpoints()},interactionCallback:()=>{clearTimeout(state.expressState.hoverCTA.minimizeTimeout),state.expressState.hoverCTA.minimized=!0}});const a=s.getCTAElement();e.prepend(a),_addHoverEventListeners(s,t,e),s.attachTooltip("bottom")}else e?.setAttribute("express-processed","Y")}})};export{addExpressMessageViewTouchpoint};