/*************************************************************************
* 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{signInUtil as e}from"../../browser/js/viewer/signInUtils.js";import{dcLocalStorage as a}from"../../common/local-storage.js";import{upsellService as t}from"./upsellService.js";import{getContentLocale as s}from"./sidePanelUtil.js";import{util as n}from"../../browser/js/content-util.js";import{loggingApi as o}from"../../common/loggingApi.js";import{getGenAIServiceVariant as r,checkForImsSidCookie as i}from"../../common/util.js";import{OptionsPageSource as d}from"../../common/constant.js";import m from"../../libs/lottie-light-esm.js";import{largeBlobStorage as l}from"../../common/large-blob-storage.js";const c=Date.now();n.translateElementsByAppLocale(".translate");await a.init();const p=a.getItem("touchpoint");a.removeItem("touchpoint");const g=await(async()=>{if("FABPill:Summarize"===p)return!1;if(a.getItem("enableCSSSRForAnon")){return!await i()}return!1})(),h=await l.getItem("anonGenAISSRHtml");if(g&&h){const e=document.createElement("iframe");e.id="sidepanelPreRendered",e.title="Adobe Chatbot",e.srcdoc=h,document.body.appendChild(e)}else document.querySelector(".loader-container").classList.remove("hidden"),m.loadAnimation({container:document.getElementById("lottie-animation"),renderer:"svg",loop:!0,autoplay:!0,path:chrome.runtime.getURL("/resources/SidePanel/TrefoilLoader-NoPad.json")});const u=e=>{const a=new URLSearchParams(window.location.search),t=parseInt(a.get("tabId"));e&&chrome.runtime.sendMessage({main_op:"analytics",analytics:[e],tab:{id:t}})};u(`DCBrowserExt:SidePanel:Opened:${p||"Unspecified"}`);const I=e=>{a.setItem("touchpoint",e),window.location.reload()};window.onbeforeunload=()=>{chrome.tabs.sendMessage(b.tabId,{main_op:"removeHighlights"})},window.addEventListener("message",async s=>{if(s.origin===b.origin)switch(s.data.main_op){case"preferences":a.setItem("optionsPageSource",d.SIDE_PANEL_PREFERENCES).then(()=>{chrome.runtime.openOptionsPage()});break;case"highlightText":const n=await chrome.tabs.sendMessage(b.tabId,{...s.data});b.sendMessage({status:n,messageId:s.data.messageId});break;case"canHighlightText":const o=await chrome.tabs.sendMessage(b.tabId,{...s.data});b.sendMessage({status:o,messageId:s.data.messageId});break;case"sanitiseSources":const r=await chrome.tabs.sendMessage(b.tabId,{...s.data});b.sendMessage({status:r,messageId:s.data.messageId});break;case"removeHighlights":chrome.tabs.sendMessage(b.tabId,{...s.data});break;case"cdnReady":b.isCdnReadyResolver();break;case"removeLoader":const i=document.querySelector(".loader-container"),m=document.getElementById("sidepanelPreRendered"),l=document.getElementById("sidepanel");i?.classList.add("hidden"),m?.classList.add("hidden"),l?.classList.remove("hidden");break;case"reloadAll":chrome.runtime.sendMessage({main_op:"reload",touchpoint:s.data.touchpoint}),I(s.data.touchpoint);break;case"reload":I(s.data.touchpoint);break;case"analytics":u(s.data.event);break;case"signIn":e.sidePanelSignIn(b.tabId);break;case"subscribeNow":a.removeItem("upsellFromAnon"),t.clickSubscribeNow(),a.setItem("upsellFromAnon",s.data.isAnonUpsell);break;case"saveVisitorID":if(s.data.visitorID){const e=a.getItem("viewerVisitorID");a.setItem("viewerVisitorID",s.data.visitorID),e&&e!==s.data.visitorID&&u("DCBrowserExt:Analytics:viewerVisitorID:MCMID:Changed")}break;case"adminStatus":a.setItem("isAdminUser",s.data.isAdmin);break;case"saveNewHtmlContent":a.getItem("enableCSSSRForAnon")&&chrome.runtime.sendMessage({type:"saveGenAIPanelRender"})}}),chrome.runtime.onMessage.addListener((e,t,s)=>{if("reload"===e.main_op&&I(e.touchpoint),"reloadTab"===e.main_op&&e.tabId==b.tabId&&I(e.touchpoint),"post_upsell"===e.main_op&&e.tabId==b.tabId)return s({success:!0,message:"Upsell process completed successfully."}),b.sendMessage({type:"upsellComplete"}),a.removeItem("upsellFromAnon"),!0;"post_upsell_anon"===e.main_op&&e.tabId===b.tabId&&(a.removeItem("upsellFromAnon"),b.sendMessage({type:"upsellAnonTransition"})),"page_switched"===e.main_op&&e.tabId===b.tabId&&b.sendMessage({type:"sidepanelPageSwitch",disqualified:!e.qualified}),"chrome_viewer_opened"===e.main_op&&e.activeTabId==b.tabId&&window.close()});const b=new class{urlParams=new URLSearchParams(window.location.search);tabId=parseInt(this.urlParams.get("tabId"));iframeElement=document.getElementById("sidepanel");constructor(){const e=new URL(a.getItem("sidepanelUrl")),t="true"===a.getItem("adobeInternal"),s="false"!==a.getItem("logAnalytics"),i="false"!==a.getItem("ANALYTICS_OPT_IN_ADMIN"),d=a.getItem("appLocale")||chrome.i18n.getMessage("@@ui_locale"),m=a.getItem("fabVariant")||"",l=a.getItem("sidePanelUUID"),g=a.getItem("enableGenAIUnlimitedOffer");e.searchParams.append("la",s&&i),e.searchParams.append("ca",chrome.runtime.id),e.searchParams.append("cluster",r()),e.searchParams.append("locale",d),e.searchParams.append("uuid",l),e.searchParams.append("adi",t),e.searchParams.append("its",c),e.searchParams.append("ev",this.urlParams.get("version")),e.searchParams.append("ecid",a.getItem("ECID")),e.searchParams.append("fabVariant",m),e.searchParams.append("gaiuo",g),e.searchParams.append("utl",!0),e.searchParams.append("fgContextVars",JSON.stringify(a.getItem("fgContextVars")||{})),e.searchParams.append("hsnap",!!h);const I=n.getTranslation("tooltipTextEnabled");e.searchParams.append("slt",I),this.iframeElement.onload=()=>{o.info({message:"Sidepanel iframe loaded",url:this.iframeElement.src,touchpoint:p}),u(`DCBrowserExt:SidePanel:IframeLoaded:${p}`)},this.iframeElement.onerror=e=>{o.error({message:"Error in loading sidepanel iframe",error:e.toString(),url:this.iframeElement.src,touchpoint:p}),u(`DCBrowserExt:SidePanel:IframeLoadError:${p}`)},this.iframeElement.src=e.href,this.origin=e.origin,this.port=chrome.runtime.connect({name:`sidepanel_${this.tabId}_${c}`}),setInterval(()=>{this.port.postMessage({action:"keep_alive"})},2e4)}isCdnReady=new Promise((e,a)=>{const t=setTimeout(()=>{a(new Error("Hosted sidepanel ready event timeout"))},2e4);this.isCdnReadyResolver=()=>{clearTimeout(t),e()}});supportedOrigin=e=>{try{return!![/^https:\/\/([a-zA-Z\d-]+\.){0,}(adobe|acrobat)\.com(:[0-9]*)?$/].find(a=>a.test(e))}catch(e){return!1}};sendMessage=e=>{this.iframeElement&&this.supportedOrigin(this.origin)&&this.isCdnReady.then(()=>this.iframeElement.contentWindow.postMessage(e,this.origin)).catch(e=>{o.error({message:"Error in sending message to sidepanel",error:e.toString()})})}},f=await chrome.tabs.sendMessage(b.tabId,{main_op:"getHtmlContent"});n.consoleLog("HTML: Response from content script: ",f);let w=f?.htmlContent;const P=await s(f.textContent);b.sendMessage({type:"sidepanelHtmlContent",htmlContent:w,initialQuestion:f.initialQuestion,disqualified:f.disqualified,htmlContentForDocOverview:f.htmlContentForDocOverview,pageLanguage:P.language,url:f.url,touchpoint:p});