/*************************************************************************
* 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 state from"../gmail/state.js";class ImageToolsDropdown{DROPDOWN_MENU_CLASS="cc440d50ba-express-dropdown-menu";DROPDOWN_MENU_SHOW_CLASS="cc440d50ba-express-dropdown-menu-show";ENTRY_POINT_BUTTON_CLASS="cc440d50ba-express-entrypoint-button";ENTRY_POINT_BUTTON_ICON_CLASS_IMG="cc440d50ba-express-entrypoint-button-icon-img";SECTION_HEADER_CLASS="cc440d50ba-express-dropdown-section-header";MENU_ITEM_ICON_IMG_CLASS="cc440d50ba-express-dropdown-menu-content-item-icon-img";VERBS={CONVERT_TO_PDF:{intent:"convertToPdf",divClass:"cc440d50ba-express-item-convert-to-pdf",iconPath:chrome.runtime.getURL("browser/images/convert_img_to_pdf.svg")},EDIT_IMAGE:{intent:"editImage",divClass:"cc440d50ba-express-item-edit-image",iconPath:chrome.runtime.getURL("browser/images/express_edit.svg")}};constructor(){this.ready=this.init()}async init(){const t=fetch(chrome.runtime.getURL("resources/express/imageToolsDropdown.html")).then(t=>t.text()),e=import(chrome.runtime.getURL("content_scripts/express/express-cta-tooltip.js")).then(t=>t.default),s=expressUtils.addFontToDocument();[this.htmlData,this.ExpressCTATooltip]=await Promise.all([t,e,s])}toggleDropdown(t,e,s){return o=>{o.stopPropagation(),chrome?.runtime?.id||e?.remove();const i=e.getElementsByClassName(this.DROPDOWN_MENU_CLASS)[0];i?.classList.toggle(this.DROPDOWN_MENU_SHOW_CLASS);const n=i?.classList.contains(this.DROPDOWN_MENU_SHOW_CLASS);if(s?.classList.toggle("is-open"),this.tooltip&&this.tooltip.hideTooltip(),n){const t=document.getElementById("express-contextual-fte");t&&t.remove()}const r=new URL(window.location.href).hostname;expressUtils.sendAnalyticsEvent([["DCBrowserExt:Image:EditDropdown:Shown",{domain:r,source:t,eventContext:state.expressState.mimeType},{frequency:"always"}]])}}async renderMenuButton(t,e){await this.ready;const s=document.createElement("div");s.innerHTML=this.htmlData,s.className=e,this.surface=e;const o=s.getElementsByClassName(this.ENTRY_POINT_BUTTON_CLASS)[0],i=new URL(window.location.href).hostname,n=state.expressState.mimeType,r={frequency:"always"};expressUtils.sendAnalyticsEvent([["DCBrowserExt:Image:EditOptions:CTA:Shown",{domain:i,source:e,eventContext:n},r]]),o.onclick=this.toggleDropdown(e,s,o).bind(this),this.button=o;return s.getElementsByClassName(this.ENTRY_POINT_BUTTON_ICON_CLASS_IMG)[0].src=chrome.runtime.getURL("browser/images/acrobat_prodc_appicon_24.svg"),Object.values(this.VERBS).forEach(i=>{const a=s.getElementsByClassName(i.divClass)[0];if(!a)return;a.getElementsByClassName(this.MENU_ITEM_ICON_IMG_CLASS)[0].src=i.iconPath,a.onclick=a=>{a.stopPropagation(),chrome?.runtime?.id||s.remove();const c=s.getElementsByClassName(this.DROPDOWN_MENU_CLASS)[0];c?.classList.remove(this.DROPDOWN_MENU_SHOW_CLASS),o?.classList.remove("is-open");const l=new URL(window.location.href).hostname;expressUtils.sendAnalyticsEvent([["DCBrowserExt:Image:EditDropdown:Verb:Clicked",{domain:l,workflow:i.intent,source:e,eventContext:n},r]]),t(i.intent)}}),util.translateElements(".translate",s),document.body.onclick=t=>{if(t.target.closest(`.${this.DROPDOWN_MENU_CLASS}`))return;const e=document.getElementsByClassName(this.DROPDOWN_MENU_SHOW_CLASS),s=document.getElementsByClassName("is-open");Array.from(e).forEach(t=>t.classList.remove(this.DROPDOWN_MENU_SHOW_CLASS)),Array.from(s).forEach(t=>t.classList.remove("is-open"))},s}attachTooltip(t,e=null){this.tooltip&&(this.tooltip.hideTooltip(),this.tooltip=null),this.tooltip=new this.ExpressCTATooltip({button:this.button,tooltipPosition:t,surface:this.surface,tooltipTextKey:"imageToolsContextMenuTooltip"});return this.tooltip.addTooltipToDOM(e,()=>this.button.classList.contains("is-open")),this.tooltip}}export default ImageToolsDropdown;