/*************************************************************************
* 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.
**************************************************************************/
class ChatGPTExpressFte{id="ChatGPTExpressFte";timeout=3e3;rendered=!1;maxFteCount=3;fteGapBetweenIterations=6048e5;chatgptExpressStateKey="chatgptExpressState";chatgptExpressInitialState={previewTouchpointUsed:!1,chatViewTouchpointUsed:!1,webImagesPreviewTouchpointUsed:!1,fte:{shown:0,lastShown:null}};chatgptChatViewContainerClassname="flex h-full flex-col overflow-y-auto thread-xl:pt-(--header-height) [scrollbar-gutter:stable_both-edges]";constructor(){this.initPromise=this.init()}isChatGPTDomain=()=>window.location.hostname.includes("chatgpt.com");init=async()=>{const t=await chrome.runtime.sendMessage({main_op:"chatgpt-express-init"});if(!this.isChatGPTDomain())return this.isEligible=async()=>!1,void(this.render=async()=>{});this.config=t,this.chatgptChatViewContainerClassname=this.config.selectors?.chatgptChatViewContainerClassname||this.chatgptChatViewContainerClassname,await this.loadContentScripts()};loadContentScripts=async()=>{const t=chrome.runtime.getURL("content_scripts/express/fte/express-contextual-fte.js");return Promise.all([import(t)]).then(t=>{this.expressContextualFteClass=t[0].default})};updateChatGPTExpressState=async()=>{let t=await chrome.storage.local.get(this.chatgptExpressStateKey);t=t?.chatgptExpressState?t.chatgptExpressState:this.chatgptExpressInitialState,t.fte||(t.fte={shown:0,lastShown:null}),t.fte.shown=(t.fte.shown||0)+1,t.fte.lastShown=Date.now(),chrome.storage.local.set({[this.chatgptExpressStateKey]:t})};isEligible=async()=>{if(await this.initPromise,this.rendered)return!1;if(!this.config?.enableChatGPTExpressFte)return!1;let t=await chrome.storage.local.get(this.chatgptExpressStateKey);if(t?.chatgptExpressState?t=t.chatgptExpressState:(t=this.chatgptExpressInitialState,chrome.storage.local.set({[this.chatgptExpressStateKey]:t})),t.previewTouchpointUsed||t.chatViewTouchpointUsed||t.webImagesPreviewTouchpointUsed)return!1;if(t.fte?.shown>=this.maxFteCount)return!1;if(t.fte?.lastShown&&t.fte?.lastShown+this.fteGapBetweenIterations>Date.now())return!1;return!!await this.getCtaElement()};_renderFte=t=>{this.ctaButtonWrapper.classList.contains("chatgptChatView")?(document.body.appendChild(t),this._positionFte(this.expressContextualFTE.getFteElement(),this.ctaButtonWrapper)):this.ctaButtonWrapper?.appendChild(t)};_positionFte=(t,e)=>{const s=e.getBoundingClientRect(),i=t.getElementsByClassName("ae1eed7c-express-contextual-fte")?.[0];if(!i)return;let a=i.getBoundingClientRect();this.ctaButtonWrapper.classList.contains("chatgptWebImagesPreviewGreyBackground")?(i.style.top=`${e.offsetHeight+window.scrollY}px`,i.style.left=`${e.offsetWidth/2+window.scrollX}px`,i.style.transform="translate(-50%, 0%)"):this.ctaButtonWrapper.classList.contains("chatgptChatView")?(i.style.top=`${s.top-i.offsetHeight/2+window.scrollY}px`,i.style.left=`${s.right+window.scrollX+16}px`,a=i.getBoundingClientRect(),a&&0!==a.height&&0!==a.width&&(a.top<63||window.innerHeight-a.bottom<87)&&this.expressContextualFTE.remove()):(i.style.top=`${s.top+e.offsetHeight+window.scrollY}px`,i.style.left=`${s.left+e.offsetWidth/2+window.scrollX}px`,i.style.transform="translate(-50%, 0%)")};handleChatWindowScroll(){this._positionFte(this.expressContextualFTE.getFteElement(),this.ctaButtonWrapper)}addChatWindowScrollListener(){const t=document.getElementsByClassName(this.chatgptChatViewContainerClassname)[0];t&&t.addEventListener("scroll",this.handleChatWindowScroll)}_fteRemovedCallback=()=>{if("chatgptChatView"===this.touchpoint){const t=document.getElementsByClassName(this.chatgptChatViewContainerClassname)[0];t&&t.removeEventListener("scroll",this.handleChatWindowScroll)}};render=async()=>{if(this.rendered)return;if(this.ctaButtonWrapper=await this.getCtaElement(),!this.ctaButtonWrapper)return;const t=this.ctaButtonWrapper?.getElementsByClassName("cc440d50ba-express-entrypoint-button")[0];t&&(this.rendered=!0,this.expressContextualFTE=new this.expressContextualFteClass({touchpoint:this.touchpoint,ctaButtonElement:this.ctaButtonWrapper,fteStrings:{fteTitle:this.config.fteStrings?.title||"Edit images with Adobe Express",fteDescription:this.config.fteStrings?.description||"Click to enhance your images with professional editing tools",fteCtaLabel:this.config.fteStrings?.ctaLabel||"Got it"},updateFteStateCallback:this.updateChatGPTExpressState,positionFteCallback:this._positionFte,renderCallback:this._renderFte,fteRemovedCallback:this._fteRemovedCallback}),await this.expressContextualFTE.render(),"chatgptChatView"===this.touchpoint&&(this.handleChatWindowScroll=this.handleChatWindowScroll.bind(this),this.addChatWindowScrollListener()))};getCtaElement=async()=>{const t=["chatgpt-preview-adobe-express-entry-point","chatgpt-chatview-adobe-express-entry-point","chatgpt-webimagespreview-adobe-express-entry-point"];for(let e=0;e<10;e++){let e=null;const s=document.getElementsByClassName("cc440d50ba-express-entrypoint-wrapper");for(let i of s)if(i&&t.includes(i.id)){if(this.touchpoint=Array.from(i.classList).find(t=>t.includes("chatgpt")),"chatgptChatView"!==this.touchpoint){e=i;break}{const t=i.getBoundingClientRect();if(t.top<120||t.bottom<0||window.innerHeight-t.bottom<180)continue;e=i}}if(e)return e;await new Promise(t=>setTimeout(t,100))}return null};remove=()=>{this.expressContextualFTE&&(this.expressContextualFTE.remove(),this.rendered=!1)}}