/*************************************************************************
* 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.
**************************************************************************/
const sendAnalytics=(e,t)=>{try{t={...t,domain:domain},chrome.runtime.sendMessage({main_op:"analytics",analytics:[[e,t]]})}catch(e){}};let domain="";if(window.self!==window.top){const e={},t="acrobat-embedded-pdf-touch-point",n="acrobat-embedded-pdf-touch-point-context-menu",o="acrobat-embedded-pdf-touch-point-drag-handle",d="acrobat-embedded-pdf-touch-point-drag-handle-visible";let i;const c=20;let a=!1,s=!1,r=0,m=null,l=0,u=0;const p=async()=>{i=await import(chrome.runtime.getURL("content_scripts/utils/util.js")),fteUtils=await import(chrome.runtime.getURL("content_scripts/utils/fte-utils.js"))},h=(e,t={})=>{const n={...t,domain:domain};i?.sendAnalyticsOncePerMonth(e,n)},b=e=>{const t=document.createElement("div");return t.classList.add("acrobat-embedded-pdf-touch-point-tooltip"),t.innerText=e?.touchPointText||"Open in Acrobat",t},g=e=>{const t=document.createElement("div");return t.classList.add("acrobat-embedded-pdf-touch-point-tooltip"),t.innerText=e?.contextMenuTooltipText||"Preferences",t},f=()=>{const e=document.createElement("img");return e.setAttribute("src",chrome.runtime.getURL("browser/images/SDC_ShowMenu_18_N_24Canvas.svg")),e.classList.add("acrobat-embedded-pdf-touch-point-context-menu-icon"),e},E=()=>{const e=document.createElement("div");return e.classList.add("acrobat-embedded-pdf-drag-shield"),e},w=(e,t)=>{if("Enter"===e.key||" "===e.key||"click"===e.type){sendAnalytics("DCBrowserExt:EmbeddedPDF:TouchPoint:Clicked"),chrome.runtime.sendMessage({main_op:"akamai-ping"}),chrome.storage.local.set({embeddedPDFTouchPointFTEState:{touchPointClicked:!0}});const e=new URL(t),n=i?.buildAcrobatPromotionSource("embeddedpdfs_chrome","native_view");e.searchParams.set("acrobatPromotionSource",n);const o=chrome.runtime.getURL("viewer.html")+"?pdfurl="+encodeURIComponent(e.toString());window.open(o),fteUtils?.removeFteTooltip("acrobat-fte-tooltip-container")}},C=e=>{e.classList.remove("has-open-menu");const t=document.getElementsByClassName(n)?.length>0&&document.getElementsByClassName(n)[0];t&&(sendAnalytics("DCBrowserExt:EmbeddedPDF:Menu:Closed"),t.remove()),k()},v=(e,t)=>{const n=document.createElement("div");return n.textContent=e,n.classList.add("acrobat-embedded-pdf-touch-point-context-menu-item"),n.addEventListener("click",e=>{e.stopPropagation(),t(e)}),n},P=(e,n)=>v(e?.menuItemStrings?.hideIconForNow,()=>{document.getElementsByClassName(t)?.length>0&&document.getElementsByClassName(t)[0].remove(),sendAnalytics("DCBrowserExt:EmbeddedPDF:HideNow:Clicked"),C(n)}),y=()=>{const e=document.createElement("hr");return e.classList.add("acrobat-embedded-pdf-touch-point-context-menu-item-separator"),e},D=(e,t)=>v(e?.menuItemStrings?.openPDFInAcrobat,e=>{w(e,window.location.href),sendAnalytics("DCBrowserExt:EmbeddedPDF:OpenInAcrobat:Clicked"),C(t)}),L=(e,t)=>v(e?.menuItemStrings?.managePreferences,async()=>{sendAnalytics("DCBrowserExt:EmbeddedPDF:ManagePreferences:Clicked"),await chrome.storage.local.set({optionsPageSource:"EMBEDDED_PDF_TOUCH_POINT"}),chrome.runtime.sendMessage({type:"open_options_page",preferenceTabId:"connected-apps",controlId:"acrobatEmbeddedPDFTouchPointPreferenceTitle",accordionId:"acrobatIntegrationsTouchPointAdvancedSettings",highlightSection:["embedded-pdf-touchpoint-section"]}),C(t)}),T=(e,t)=>{const n=document.createElement("div");n.classList.add(o);const d=document.createElement("img");return d.setAttribute("src",chrome.runtime.getURL("browser/images/S_DragHandle_18_N.svg")),n.appendChild(d),n.addEventListener("mousedown",o=>{R(o,n,e,t)}),n},x=e=>e.getBoundingClientRect().top<188?"bottom":"top",F=e=>{const t=document.createElement("div");t.classList.add(n);return"bottom"===x(e)&&(t.style.bottom="auto",t.style.top="34px"),t},B=e=>{const t=document.createElement("div");t.classList.add("acrobat-embedded-pdf-menu-click-shield"),t.setAttribute("aria-hidden","true"),_(t,e);const n=e;n?.firstChild?n.insertBefore(t,n.firstChild):n.appendChild(t)},k=()=>{document.getElementsByClassName("acrobat-embedded-pdf-menu-click-shield")?.length>0&&document.getElementsByClassName("acrobat-embedded-pdf-menu-click-shield")[0]?.remove()},_=(e,t)=>{e.addEventListener("click",e=>{e.preventDefault(),C(t)})},A=(e,t,o,d)=>{if(e.getElementsByClassName(n)&&e.getElementsByClassName(n)[0])t.style.removeProperty("background"),C(d);else{sendAnalytics("DCBrowserExt:EmbeddedPDF:Menu:Clicked");const t=F(d),n=D(o,d),i=P(o,d),c=L(o,d),a=y();t.appendChild(n),t.appendChild(a),t.appendChild(i),t.appendChild(c),e.appendChild(t),d.classList.add("has-open-menu"),k(),B(d)}},N=e=>{const t=document.createElement("div");t.classList.add("acrobat-embedded-pdf-icon-container");const n=i?.createAcrobatIconElement("acrobat-embedded-pdf-icon","browser/images/acrobat_dc_trefoil_24_white.svg");t.appendChild(n);const o=b(e);return t.appendChild(o),t.addEventListener("click",e=>{if(a)return e.preventDefault(),void e.stopPropagation();w(e,window.location.href)}),t},S=(e,t)=>{const n=document.createElement("div");n.classList.add("acrobat-embedded-pdf-touch-point-context-menu-container");const o=f();n.appendChild(o);const d=g(e);return n.appendChild(d),o.addEventListener("click",()=>A(n,o,e,t)),n},M=e=>{const n=document.createElement("div");n.classList.add("acrobat-embedded-pdf-touch-point-container-wrapper");const o=document.createElement("button");o.classList.add(t);const d=N(e);o.appendChild(d);const i=S(e,o);o.appendChild(i);const c=T(n,o);return o.appendChild(c),n.addEventListener("mousedown",e=>R(e,c,n,o)),o.addEventListener("keydown",e=>w(e,window.location.href)),n.appendChild(o),n},I=(e,t,n,o)=>{a=!0,t.classList.add(d),C(o),h("DCBrowserExt:EmbeddedPDF:TouchPoint:Dragged");const i=n.getBoundingClientRect();r=e.clientY-i.top},U=e=>{if(!s)return;const t=document.elementFromPoint(e.clientX,e.clientY),n=new MouseEvent("click",{bubbles:!0,cancelable:!0,view:window,startX:l,startY:u});t?.dispatchEvent(n)},R=(e,t,n,o)=>{if(0!==e.button)return;if(e.target?.closest?.(".acrobat-embedded-pdf-touch-point-context-menu")||e.target?.closest?.(".acrobat-fte-tooltip-container")||e.target?.closest?.(".acrobat-embedded-pdf-touch-point-context-menu-container"))return;e.stopImmediatePropagation(),e.preventDefault(),s=!0,l=e.clientX,u=e.clientY,m=E(),document.documentElement.appendChild(m);const d=e=>H(e,t,m,d);m.addEventListener("mousemove",e=>j(e,t,n,o)),m.addEventListener("mouseup",d),document.documentElement.addEventListener("mouseleave",d)},H=(e,t,n,o)=>{document.documentElement.removeEventListener("mouseleave",o),t.classList.remove(d),n?.remove(),n=null,a?a=!1:U(e),s=!1},Y=(e,t)=>{const n=O(e.clientY,t);t.style.top=`${n}px`,t.style.bottom="auto"},j=(e,t,n,o)=>{if(!s)return;const d=Math.abs(e.clientX-l),i=Math.abs(e.clientY-u);!a&&(d>6||i>6)&&I(e,t,n,o),a&&Y(e,n)},O=(e,t)=>{const n=e-r,o=window.innerHeight-t.offsetHeight-c;return Math.max(c,Math.min(o,n))},X=async e=>{const t=M(e);document.documentElement.appendChild(t),sendAnalytics("DCBrowserExt:EmbeddedPDF:TouchPoint:Shown");const n=t?.getBoundingClientRect(),o=window?.frameElement?.getBoundingClientRect();chrome.runtime.sendMessage({main_op:"embedded-pdf-touch-point-added",frameId:e?.frameId,position:{top:n.top+o?.top,left:n.left+o?.left,right:n.right,bottom:n.bottom}})},W=async()=>{const e=await chrome.runtime.sendMessage({main_op:"embedded-pdf-touch-point-config"}),n=document.getElementsByClassName(t),o=document.getElementsByClassName("acrobat-fte-tooltip");if(n?.length>0&&0===o?.length){const t=fteUtils?.createFteTooltip(e?.fteToolTipStrings,"embeddedPDF");n[0].appendChild(t),sendAnalytics("DCBrowserExt:EmbeddedPDF:FTE:Shown");t.querySelector(".acrobat-fte-tooltip-button").addEventListener("click",()=>{t?.remove(),sendAnalytics("DCBrowserExt:EmbeddedPDF:FTE:Closed")}),fteUtils?.updateFteToolTipCoolDown(e?.touchPointConfig?.tooltip,"embeddedPDFTouchPointFTEState"),t.addEventListener("mouseenter",e=>{e.stopPropagation(),e.preventDefault()},{capture:!0})}},$=async()=>{const t=await chrome.runtime.sendMessage({main_op:"embedded-pdf-touch-point-config"});if(t?.enableEmbeddedPDFTouchPoint){if(t?.touchPointConfig?.blockedDomains?.includes(domain))return;if(h("DCBrowserExt:EmbeddedPDF:TouchPoint:Enabled"),window.innerHeight<280||window.innerWidth<320)return void h("DCBrowserExt:EmbeddedPDF:TouchPointSkipped:WidthHeight");i?.addFontToDocument(e),X(t)}};chrome.runtime.onMessage.addListener(e=>{"add-fte-for-embedded-pdf-touch-point"===e?.type&&W()});(()=>{if(document?.contentType?.includes("application/pdf")){const e=new URL(window.location.href),t=e?.hash?.includes("toolbar=0");domain=e.hostname;let n="Unknown";document.body?.childNodes?.length>0&&document.body.childNodes[0]?.getAttribute?.("src")?.startsWith("chrome-extension://dahenjhkoodjbpjheillcadbppiidmhp")?n="GoogleScholar":document.body?.childNodes?.length>0&&"about:blank"===document.body.childNodes[0]?.getAttribute?.("src")&&(n="NativeViewer"),p().then(()=>{h("DCBrowserExt:Viewer:Detected:EmbeddedPDF:IframeContentType",{eventContext:`${n}-${!t}`}),$()})}})()}