/*************************************************************************
* 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 isGoogleSearchLink=checkGoogleSearchLink(),isDarkMode=window.matchMedia("(prefers-color-scheme: dark)").matches;let hoverTimeout,showAssistantMarker=!0,isCursorOnPopup=null,hoveredLink=null,currentPopupTimeouts=new Map,genaiMarkersConfig=null;async function getGenaiMarkersConfig(){if(null===genaiMarkersConfig){const e=await chrome.runtime.sendMessage({main_op:"getFloodgateMeta",flag:"dc-cv-genai-markers"});genaiMarkersConfig=JSON.parse(e||"{}")}return genaiMarkersConfig}async function showPopup(e,t,n=!1){if(isInvalidContentScript())return;const o=`__assistantPopup__${t}`;if(document.getElementById(o))n||(hoverTimeout=setTimeout(()=>showPopup(e,t,!0),300));else{let n=document.createElement("iframe");n.id=o,n.style.cssText=`\n            border: 0;\n            z-index: 127;\n            position: absolute;\n            margin: auto;\n            background: transparent;\n            color-scheme: auto;\n            left: ${e.right+(scrollX||0)}px;\n            top: ${e.top+(scrollY||0)-13}px;\n            width: 250px;\n            height: 50px;\n            min-height: unset;\n            overflow: hidden;\n            opacity: 0;\n        `;const i=await chrome.runtime.sendMessage({main_op:"getFloodgateFlag",flag:"dc-cv-genai-markers-updated-ui",cachePurge:"NO_CALL"});window.dcLocalStorage.setItem("AiMarkerVariant",i?"NEW":"LEGACY"),n.src=chrome.runtime.getURL("browser/js/"+(i?"newAssistantPopup":"assistantPopup")+".html?pdfMarkerLink="+encodeURIComponent(t)+"&theme="+(isDarkMode?"dark":"light")),document.body.appendChild(n),validateIframePosition(e,n),n.addEventListener("mouseenter",()=>{isCursorOnPopup=t}),n.addEventListener("mouseleave",()=>{isCursorOnPopup=null,setTimeout(()=>hidePopup(t),400)})}}async function validateIframePosition(e,t){const n=await getGenaiMarkersConfig(),{horizontalOffset:o=20,verticalOffset:i=20}=n,a=t.getBoundingClientRect();(a.left-e.right>o||e.top-a.top>i)&&(t?.remove(),chrome.runtime.sendMessage({main_op:"log-info",log:{message:"AI marker popup is too far from the text",link:hoveredLink}}))}function hidePopup(e,t=!1){if(!t&&(hoveredLink===e||isCursorOnPopup===e))return;const n=`__assistantPopup__${e}`,o=document.getElementById(n);if(o){currentPopupTimeouts.has(e)&&clearTimeout(currentPopupTimeouts.get(e)),o.contentWindow.postMessage({action:"exit"},`chrome-extension://${chrome.runtime.id}`);const t=setTimeout(()=>{o.remove(),currentPopupTimeouts.delete(e)},400);currentPopupTimeouts.set(e,t)}}function checkGoogleSearchLink(){const e=window.location.href;return/https:\/\/www\.google\.(com|[a-z]{2}|com\.[a-z]{2}|co\.[a-z]{2}|cat)\/search\?/.test(e)&&e.includes("q=")}function isPDFLink(e,t){return e&&e.match(/\.pdf(?=\?|#|$)/i)&&t.trim().length>0}let pdfLinksCache=null;function getPDFLinks(){if(null===pdfLinksCache){const e=Array.from(document.querySelectorAll("a[href]"));pdfLinksCache=e.filter(e=>isPDFLink(e.getAttribute("href"),e.textContent))}return pdfLinksCache}function addPdfIconToLinks(){getPDFLinks().forEach(e=>{const t=decodeURIComponent(e.getAttribute("href"));e.addEventListener("mouseenter",()=>{let n=e.getBoundingClientRect();if(isGoogleSearchLink){const t=e.getElementsByTagName("h3")[0]?.firstChild;if(t&&t.nodeType===Node.TEXT_NODE){const e=document.createRange();e.selectNodeContents(t),n=e.getBoundingClientRect()}}n&&showAssistantMarker&&(hoveredLink=t,hoverTimeout=setTimeout(()=>{showPopup(n,t),chrome.runtime.sendMessage({main_op:"getFloodgateFlag",flag:"dc-cv-genai-markers-impression-analytics",cachePurge:"NO_CALL"},e=>{e&&chrome.runtime.sendMessage({main_op:"analytics",analytics:[["DCBrowserExt:PdfLink:Hovered"]]})})},200))}),e.addEventListener("mouseleave",()=>{setTimeout(()=>hidePopup(t),400),hoveredLink=null,clearTimeout(hoverTimeout)})}),window.addEventListener("scroll",()=>{if(hoveredLink){const e=`__assistantPopup__${hoveredLink}`,t=document.getElementById(e);t&&t.remove()}},{passive:!0})}async function validateOverlappingElements(e,t){if(!e||t!==hoveredLink)return;const n=document.getElementById("__assistantPopup__"+t),o=n?.getBoundingClientRect();if(!n)return;const i={top:o.top+e.top,left:o.left+e.left,bottom:o.top+e.bottom,right:o.left+e.right,width:e.width,height:e.height,x:o.left+e.x,y:o.top+e.y},a=getOverlappingElement(n,i),s=await getGenaiMarkersConfig(),{validatorClass:r=".b8lM7"}=s,l=getClickableOverlappingElement(n,i)?.filter(e=>!e.closest(r));if(l.length>0)n?.remove(),chrome.runtime.sendMessage({main_op:"log-info",log:{message:"AI marker popup is overlapped a clickable element",link:t}});else if(a.length>0){function c(e){for(const t of e.children)if("IMG"===t.tagName)return!0}function m(e){for(const t of e.childNodes)if(t.nodeType===Node.TEXT_NODE&&t.textContent.trim().length>0)return!0}for(const p of a)if(c(p)||m(p)){n?.remove(),chrome.runtime.sendMessage({main_op:"log-info",log:{message:"AI marker popup is overlapped a non-clickable text or image element",link:t}});break}}n.style.opacity="1"}!async function(){const e=getPDFLinks().length>0;chrome.runtime.sendMessage({main_op:"resolve-has-pdf-promise",hasPDFLink:e});const t=await chrome.runtime.sendMessage({main_op:"getFloodgateFlag",flag:"dc-cv-genai-markers",cachePurge:"NO_CALL"});if(!e||!t)return;const{appLocale:n,pdfViewer:o,egaf:i,aiMarkers:a,genAIEligible:s,locale:r}=await chrome.storage.local.get(["appLocale","pdfViewer","egaf","aiMarkers","genAIEligible","locale"]);if("en"!==n?.split("-")[0]||"en"!==r?.split("-")[0]||"false"===o||"false"===i||"false"===a||"true"!==s)return;const l="true"===(await chrome.storage.managed.get("DisableGenAI"))?.DisableGenAI,c=await chrome.runtime.sendMessage({main_op:"getFloodgateMeta",flag:"dc-cv-genai-markers-allowlist"});let m;try{m=JSON.parse(c||"[]").some(e=>window.location.href.match(e))}catch(e){m=!1}!l&&m&&(chrome.runtime.sendMessage({main_op:"addExperimentCodeForAnalytics",experimentCode:"EGMS"}),addPdfIconToLinks())}(),chrome.runtime.onMessage.addListener((e,t,n)=>{switch(e.content_op){case"hideAIMarkerPopup":hidePopup(e.href,!0),showAssistantMarker=!1;break;case"updateIframeHeight":const t=`__assistantPopup__${e.href}`,n=document.getElementById(t);n?(n.style.height=(e.menuOpen?125:50)+"px",n.style.width=(e.menuOpen?400:250)+"px"):chrome.runtime.sendMessage({main_op:"log-info",log:{message:"Menu not shown because AI marker popup iframe is not found",link:e.href}});break;case"validateOverlappingElements":validateOverlappingElements(e.elementRect,e.link)}});