/*************************************************************************
* 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 expressUtils from"./express-utils.js";import ExpressCTATooltip from"./express-cta-tooltip.js";import{OptionsPageSource}from"../../common/constant.js";const CLASS_PREFIX="cdf40d50ba";class ExpressHoverCTA{BUTTON_CTA_CLASS="cdf40d50ba-express-hover-cta";BUTTON_ICON_IMG_CLASS="cdf40d50ba-express-button-icon-img";FLOATING_MENU_CLASS="cdf40d50ba-express-floating-menu";EXPAND_MENU_ICON_CLASS="cdf40d50ba-express-expand-menu-icon";MENU_ITEM_EDIT_IMAGE_CLASS="cdf40d50ba-express-edit-cta-button";SUBMENU_MANAGE_SETTINGS_CLASS="cdf40d50ba-express-submenu-manage-settings";SUBMENU_HIDE_SESSION_CLASS="cdf40d50ba-express-submenu-hide-session";constructor(){this.ready=this.init()}async init(){await fetch(chrome.runtime.getURL("resources/express/expressHoverCTA.html")).then(e=>e.text()).then(e=>{this.htmlData=e}),await expressUtils.addFontToDocument()}_addCTAClickEventListener(e){e.onclick=e=>{e.stopPropagation();const t="editImage",i=new URL(window.location.href).hostname;expressUtils.sendAnalyticsEvent([["DCBrowserExt:Express:HoverCTA:Clicked",{domain:i,dvDisableSessionCount:t,expressTouchpoint:this.surface}]]),this.onClickCallback(t)}}_addHideSessionClickEventListener(e){e.onclick=e=>{e.stopPropagation();const t=new URL(window.location.href).hostname;expressUtils.sendAnalyticsEvent([["DCBrowserExt:Express:HoverCTA:HideForSession:Clicked",{domain:t,expressTouchpoint:this.surface}]]),this.onHideClickCallback()}}_addManageSettingsClickEventListener(e){e.onclick=e=>{e.stopPropagation();const t=new URL(window.location.href).hostname;expressUtils.sendAnalyticsEvent([["DCBrowserExt:Express:HoverCTA:ManageSettings:Clicked",{domain:t,expressTouchpoint:this.surface}]]),chrome.storage.local.set({optionsPageSource:`${OptionsPageSource.EXPRESS_CTA}_${this.surface}`}).then(()=>{chrome.runtime.sendMessage({type:"open_options_page",preferenceTabId:"connected-apps",controlId:"expressTouchpointPreferenceTitle",highlightSection:["express-touchpoints-section"]})})}}async renderButton(e){await this.ready;const{onClickCallback:t,onHideClickCallback:i,surface:s,interactionCallback:n}=e;this.onClickCallback=t,this.interactionCallback=n,this.onHideClickCallback=i,this.surface=s;const o=document.createElement("div");o.innerHTML=this.htmlData,o.className=s,o.style.display="none";const a=o.getElementsByClassName(this.BUTTON_CTA_CLASS)[0],r=a.getElementsByClassName(this.BUTTON_ICON_IMG_CLASS)[0],l=a.getElementsByClassName(this.FLOATING_MENU_CLASS)[0],c=a.getElementsByClassName(this.EXPAND_MENU_ICON_CLASS)[0],h=o.getElementsByClassName(this.MENU_ITEM_EDIT_IMAGE_CLASS)[0],d=l.getElementsByClassName(this.SUBMENU_MANAGE_SETTINGS_CLASS)[0],u=l.getElementsByClassName(this.SUBMENU_HIDE_SESSION_CLASS)[0];r.src=chrome.runtime.getURL("browser/images/acrobat_prodc_appicon_24.svg"),h&&this._addCTAClickEventListener(h),u&&this._addHideSessionClickEventListener(u),d&&this._addManageSettingsClickEventListener(d),util.translateElements(".translate",o),this.button=a,this.container=o,this.floatingMenu=l,this.expandMenu=c,this.addHoverEventListeners(),this.addHamburgerMenuClickListeners()}addHamburgerMenuClickListeners(){this.expandMenu.addEventListener("click",e=>{e.stopPropagation(),this.floatingMenuShown?this.hideFloatingMenu():this.showFloatingMenu()}),this.expandMenu.addEventListener("dblclick",e=>{e.stopPropagation()})}addHoverEventListeners(){this.container.addEventListener("mouseenter",()=>{clearTimeout(this.hideTimeout),this.maximizeButton(),this.interactionCallback?.()}),this.container.addEventListener("mouseleave",()=>{clearTimeout(this.hideTimeout),this.hideTimeout=setTimeout(()=>{this.minimizeButton()},100)})}closeFloatingMenuAndMinimizeButton(){this.hideFloatingMenu(),setTimeout(()=>{this.minimizeButton()},100)}showFloatingMenu(){this.hideTooltip(),this.button.classList.add("expand-menu-open"),this.floatingMenuShown=!0,document.addEventListener("click",()=>this.closeFloatingMenuAndMinimizeButton())}hideFloatingMenu(){this.button.classList.remove("expand-menu-open"),this.floatingMenuShown=!1,document.removeEventListener("click",()=>this.closeFloatingMenuAndMinimizeButton())}minimizeButton(){this.floatingMenuShown||this.container.classList.add("minify")}maximizeButton(){this.container.classList.remove("minify")}showButton(){this.container.style.display="flex"}hideButton(){this.floatingMenuShown&&this.hideFloatingMenu(),this.container.style.display="none"}remove(){this.container&&this.container.parentNode&&this.container.parentNode.removeChild(this.container),this.button=null,this.container=null,this.hideTimeout=null}getCTAElement(){return this.container}attachTooltip(e,t=null){this.tooltip=new ExpressCTATooltip({button:this.button,tooltipPosition:e,surface:this.surface}),this.tooltip.addTooltipToDOM(t,()=>this.floatingMenuShown)}hideTooltip(){this.tooltip&&this.tooltip.hideTooltip()}}export default ExpressHoverCTA;