/*************************************************************************
* 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"../../gdrive/state.js";import SingleClickCTA from"../single-click-cta.js";import{getFileDetails}from"../../utils/util.js";import expressUtils from"../express-utils.js";class ExpressGdriveTouchpointService{constructor(){this.previewEntryPointId="express-gdrive-native-viewer-entry-point",this.touchpoint="gdriveNativeViewer",this.headerContainerSelectors=["a-b-K-sg-Ec","ndfHFb-c4YZDc-Wrql6b-LQLjdd"],this.activeViewElementId=["drive-active-item-info"],this.expressButtonSelector="cc440d50ba-express-entrypoint-button",this.expressButtonTextSelector="cc440d50ba-express-entrypoint-button-text",this.expressButtonIconSelector="cc440d50ba-express-entrypoint-button-icon",this.unsupportedMimeTypes=["image/svg+xml","image/tiff","image/tiff-fx"],this.gdriveExpressStateKey="express-gdrive-fte-state"}_getVisibleViewer=()=>{const e=state?.expressConfig?.selectors?.activeViewElementId??this.activeViewElementId;for(let t of e){const e=document.getElementById(t);if(e)return[e.parentElement,e]}return[null,null]};_getPreviewedImageURL=()=>{const[e,t]=this._getVisibleViewer();if(!e)return null;const s=getFileDetails(t);if(!s?.mimeType?.startsWith("image/")||this.unsupportedMimeTypes.includes(s?.mimeType))return null;const i=e.getElementsByTagName("IMG")[0];return i?i?.src?.startsWith("blob:")?null:i?.src:null};_setTouchpointClickedForFteState=async()=>{this._removeContextualFte();let e=await chrome.storage.local.get(this.gdriveExpressStateKey);e=e?.[this.gdriveExpressStateKey]?e[this.gdriveExpressStateKey]:{},e.previewTouchpointUsed=!0,chrome.storage.local.set({[this.gdriveExpressStateKey]:e})};_removeTooltipIfFteRendered=()=>{document.getElementById("express-contextual-fte")&&state.expressState.tooltip&&(state.expressState.tooltip.tooltip.remove(),state.expressState.tooltip=null)};_clickCallback=e=>{chrome.runtime.sendMessage({main_op:"launch-express",imgUrl:state.expressState?.imageURL,intent:e,touchpoint:this.touchpoint}),this._setTouchpointClickedForFteState()};toggleButtonSize=(e,t)=>{e.style.width=t?"32px":"";const s=e.getElementsByClassName(this.expressButtonTextSelector)[0],i=e.getElementsByClassName(this.expressButtonIconSelector)[0];s&&(s.style.display=t?"none":""),i&&(i.style.padding=t?"7px 9px 7px 5px":"",i.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)};_getElementFromSelector=e=>{for(let t of e){let e=document.getElementsByClassName(t)[0];if(e)return e}return null};_addExpressTouchpointToHeader=(e,t)=>{const s=state?.expressConfig?.selectors?.headerContainerSelectors||this.headerContainerSelectors,i=this._getElementFromSelector(s);return!!(i&&i?.childNodes?.length>0)&&(i.insertBefore(t,i.childNodes[0]),this._handleResponsiveButton(t),state.expressState.tooltip=e.attachTooltip("bottom"),chrome.runtime.sendMessage({main_op:"reRenderShowOneChild"}),expressUtils.sendAnalyticsEventOncePerDay([["DCBrowserExt:Express:Gdrive:NativeViewerEntryPoint:Shown"]]),!0)};_addExpressTouchpoint=async()=>{const e=new SingleClickCTA;state.expressState.nativeViewerTouchpointVisible=!0;const t=await e.renderMenuButton(this._clickCallback,this.touchpoint);t.id=this.previewEntryPointId,this._addExpressTouchpointToHeader(e,t)};_removeContextualFte=()=>{const e=document.getElementById("express-contextual-fte");e&&e.remove()};addExpressNativeViewerTouchpoint=()=>{this._removeTooltipIfFteRendered();const e=this._getPreviewedImageURL();if(!e)return void this.removeExpressTouchpoints();const{nativeViewerTouchpointVisible:t,imageURL:s}=state?.expressState;s!==e&&(state.expressState.imageURL=e),t||this._addExpressTouchpoint()};removeExpressTouchpoints=()=>{const e=document.getElementById(this.previewEntryPointId);e&&(state.expressState?.resizeHandler&&(window.removeEventListener("resize",state.expressState.resizeHandler),state.expressState.resizeHandler=null),state.expressState.imageURL=null,state.expressState.nativeViewerTouchpointVisible=!1,e.parentElement?.removeChild(e)),state.expressState.tooltip?.tooltip?.remove(),state.expressState.tooltip=null}}const expressGdriveTouchpointService=new ExpressGdriveTouchpointService;export{expressGdriveTouchpointService};