/*************************************************************************
* 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{util as e}from"./util.js";import{Proxy as t}from"./proxy.js";import{SETTINGS as s}from"./settings.js";import{dcLocalStorage as i}from"../common/local-storage.js";import{getActiveExperimentAnalyticsArray as o}from"../common/experimentUtils.js";import{OFFSCREEN_DOCUMENT_PATH as r}from"../common/constant.js";let n=null,a={prod:{cloud_host:"https://cloud.acrobat.com/",redirect_uri:"https://createpdf.acrobat.com/static/js/aicuc/cpdf-template/sign_in_complete.html",cpdf_host:"https://createpdf.acrobat.com/",frictionlessUrl:"https://acrobat.adobe.com/dc-hosted-extension/iframe-index.html",env:"prod",viewer_ims_client_id:"dc-prod-chrome-viewer",acrobatViewerUrl:"https://acrobat.adobe.com/dc-chrome-extension/index.html",signInUrl:"https://acrobat.adobe.com/dc-chrome-extension/sign-in.html",imsURL:"https://ims-na1.adobelogin.com",floodgateUri:"https://p13n.adobe.io/fg/api",imsLibUrl:"https://auth.services.adobe.com/imslib/imslib.min.js",dcApiUri:"https://dc-api.adobe.io",viewer_ims_client_id_social:"dc-prod-chrome-viewer-social",uninstallUrl:"https://acrobat.adobe.com/dc-chrome-extension/index.html?la=true#/uninstall",whatsNewPageUrl:"https://acrobat.adobe.com/dc-chrome-extension/index.html#/whats-new",whatsNewJsonUrl:"https://acrobat.adobe.com/dc-chrome-extension/whats-new/wn.json",welcomePdfUrlHost:"https://acrobat.adobe.com",ims_context_id:"v:2,s,9122f250-90cf-11ed-9fe5-b3719c660a78",loggingUri:"https://dc-api.adobe.io",viewerUrl:{latest_uri:"https://acrobat.adobe.com/dc-chrome-extension/index.html",internal_uri:"https://acrobat.adobe.com/dc-chrome-extension/index.html",external_uri:"https://acrobat.adobe.com/dc-chrome-extension/1.0.4/index.html",version_template:"https://acrobat.adobe.com/dc-chrome-extension/{VERSION}/index.html"},version_config_uri:"https://acrobat.adobe.com/dc-chrome-extension/version-config.json",ote_config_uri:"https://acrobat.adobe.com/dc-hosted-extension/ote-config.json",moderation_list_uri:"https://acrobat.adobe.com/dc-hosted-extension/moderation.txt",kw_moderation_list_uri:"https://acrobat.adobe.com/dc-hosted-extension/kw-moderation-list.txt",express_moderation_list_uri:"https://acrobat.adobe.com/dc-hosted-extension/express-moderation-list.txt",floodgateUrl:"https://acrobat.adobe.com/dc-chrome-extension/floodgate.html",userStateUrl:"https://acrobat.adobe.com/dc-chrome-extension/user-state.html",popup_cdn_uri:"https://acrobat.adobe.com/dc-hosted-extension/react-index.html",sidepanelUrl:"https://acrobat.adobe.com/dc-hosted-extension/react-index.html?#/side-panel",analyticsOverrideConfigUri:"https://acrobat.adobe.com/dc-hosted-extension/build-analytics-config.json",addWebpageToProjectUrl:"https://acrobat.adobe.com/dc-hosted-extension/react-index.html?#/add-webpage-to-project",genAIPricingUri:" https://acrobat.adobe.com/proxy/pricing/us/en/acrobat-chrome-gen-ai.html",acrobat_ipm_uri:"https://acroipm2.adobe.com/acrobat-web",express:{pluginUrl:"https://cc-embed.adobe.com/plugin/v4/acrobat",webAppUrl:"https://new.express.adobe.com/new"},floodgate_sandbox_name:"xlg-prod"},stage:{cloud_host:"https://cloud.stage.acrobat.com/",redirect_uri:"https://createpdf.stage.acrobat.com/static/js/aicuc/cpdf-template/sign_in_complete.html",cpdf_host:"https://createpdf.stage.acrobat.com/",frictionlessUrl:"https://stage.acrobat.adobe.com/dc-hosted-extension/iframe-index.html",env:"stage",viewer_ims_client_id:"dc-stage-chrome-viewer",acrobatViewerUrl:"https://stage.acrobat.adobe.com/dc-chrome-extension/index.html",signInUrl:"https://stage.acrobat.adobe.com/dc-chrome-extension/sign-in.html",imsURL:"https://ims-na1-stg1.adobelogin.com",floodgateUri:"https://p13n-stage.adobe.io/fg/api",imsLibUrl:"https://auth-stg1.services.adobe.com/imslib/imslib.js",dcApiUri:"https://dc-api-stage.adobe.io",viewer_ims_client_id_social:"dc-stage-chrome-viewer-social",uninstallUrl:"https://stage.acrobat.adobe.com/dc-chrome-extension/index.html?la=true#/uninstall",whatsNewPageUrl:"https://stage.acrobat.adobe.com/dc-chrome-extension/index.html#/whats-new",whatsNewJsonUrl:"https://stage.acrobat.adobe.com/dc-chrome-extension/whats-new/wn.json",welcomePdfUrlHost:"https://stage.acrobat.adobe.com",ims_context_id:"v:2,s,03c19460-80fe-11ed-a731-c51304049b0d",loggingUri:"https://dc-api-stage.adobe.io",viewerUrl:{latest_uri:"https://stage.acrobat.adobe.com/dc-chrome-extension/index.html",internal_uri:"https://stage.acrobat.adobe.com/dc-chrome-extension/index.html",external_uri:"https://stage.acrobat.adobe.com/dc-chrome-extension/1.0.4/index.html",version_template:"https://stage.acrobat.adobe.com/dc-chrome-extension/{VERSION}/index.html"},version_config_uri:"https://stage.acrobat.adobe.com/dc-chrome-extension/version-config.json",ote_config_uri:"https://stage.acrobat.adobe.com/dc-hosted-extension/ote-config.json",moderation_list_uri:"https://stage.acrobat.adobe.com/dc-hosted-extension/moderation.txt",kw_moderation_list_uri:"https://stage.acrobat.adobe.com/dc-hosted-extension/kw-moderation-list.txt",express_moderation_list_uri:"https://stage.acrobat.adobe.com/dc-hosted-extension/express-moderation-list.txt",floodgateUrl:"https://stage.acrobat.adobe.com/dc-chrome-extension/floodgate.html",userStateUrl:"https://stage.acrobat.adobe.com/dc-chrome-extension/user-state.html",popup_cdn_uri:"https://stage.acrobat.adobe.com/dc-hosted-extension/react-index.html",sidepanelUrl:"https://stage.acrobat.adobe.com/dc-hosted-extension/react-index.html?#/side-panel",addWebpageToProjectUrl:"https://stage.acrobat.adobe.com/dc-hosted-extension/react-index.html?#/add-webpage-to-project",analyticsOverrideConfigUri:"https://stage.acrobat.adobe.com/dc-hosted-extension/build-analytics-config.json",genAIPricingUri:"https://stage.acrobat.adobe.com/proxy/pricing/acrobat-chrome-gen-ai.html",acrobat_ipm_uri:"https://acroipm2.stage.adobe.com/acrobat-web",express:{pluginUrl:"https://stage.cc-embed.adobe.com/plugin/v4/acrobat?env=stage",webAppUrl:"https://stage.projectx.corp.adobe.com/new"},floodgate_sandbox_name:"xlg-stage"}};export function deriveCDNURL(e="prod"){if(!i.getItem("enableCDNVersioning")){const t="acrobatViewerUrl";return a[e]?.[t]}const t="viewerUrl";let s;const o="prod"===e?"prod":"non_prod";let r=a[e]?.[t].external_uri;try{const n=i.getItem("adobeInternal");"true"===n&&(r=a[e]?.[t].internal_uri);const c=i.getItem("version-config");c&&(s="true"===n?c[`iv_${o}`]:c[`ev_${o}`],s&&""!==s&&null!==s&&(r="latest"===s?a[e]?.[t].latest_uri:a[e]?.[t].version_template.replace(new RegExp("{VERSION}","g"),s)))}catch{}return r}export function updateEnvironment(e){const t=e.env,s=a[t],o=deriveCDNURL(t),r=s.viewer_ims_client_id,n=s.viewer_ims_client_id_social,c=s.ims_context_id,d=s.imsURL,l=s.imsLibUrl,h=s.dcApiUri,m=s.genAIPricingUri,p=new URL(s.uninstallUrl);p.searchParams.append("callingApp",chrome.runtime.id),p.searchParams.append("theme",i.getItem("theme")||"auto"),common.settings={...common.settings,...s};try{i.setItem("cdnUrl",o),i.setItem("signInUrl",common.getSignInUrl()),i.setItem("viewerImsClientId",r),i.setItem("viewerImsClientIdSocial",n),i.setItem("imsContextId",c),i.setItem("imsURL",d),i.setItem("imsLibUrl",l),i.setItem("dcApiUri",h),i.setItem("genAIPricingUri",m),i.setItem("sidepanelUrl",common.getSidepanelUrl());try{const e={...i.getItem("whatsNewState")||{},whatsNewPageUrl:common.getWhatsNewPageUrl(),whatsNewJsonUrl:common.getWhatsNewJsonUrl()};i.setItem("whatsNewState",e)}catch(e){}chrome.runtime.setUninstallURL(p.href)}catch(e){}}export function onMessageListener(t,s,o){switch(t.type){case"getItem":o(i.getItem(t.key));break;case"setItem":i.setItem(t.key,t.value);break;case"removeItem":i.removeItem(t.key);break;case"saveGenAIPanelRender":(async()=>{const t=`${r}?env=${common.getEnv()}`;await e.setupOffscreenDocument(t),setTimeout(()=>{chrome.runtime.sendMessage({main_op:"saveGenAIPanelRender",target:"offscreen"})},50)})();break;case"update_env":updateEnvironment(t)}}class c{constructor(){this.$GET_headers={Accept:"application/vnd.adobe.dex+json;version=1",Authorization:null,"x-api-client-id":"api_browser_ext"},this.$POST_headers={Accept:"application/vnd.adobe.dex+json;version=1","Content-Type":"application/vnd.adobe.dex+json;version=1;charset=utf-8",Authorization:null,"x-api-client-id":"api_browser_ext"},this.reset()}proxy(...e){return t.proxy.bind(this)(...e)}uuid(){return"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".replace(/x/g,function(){return"0123456789abcdef"[Math.floor(16*Math.random())]})}updateEnvironment(e){return updateEnvironment(e)}deriveCDNURL(e="prod"){return deriveCDNURL(e)}reset(t="prod"){this.settings={cpdf_api:null,files_host:null,files_api:null,files_upload:null,files_root:null,fillsign_api:null,auth_token:null,ims_host:null},this.server=a[t]||a[Object.keys(a)[0]];const s=this.deriveCDNURL(t);this.settings=e.extend(this.settings,this.server);const i="acrobatViewerUrl";this.settings={...this.settings,[i]:s}}connected(){return!!this.settings.auth_token}setGlobals(e){this.globals=e}getFrictionlessUri(){const e="frictionlessUrl";return this.settings[e]||a.prod[e]}getPopupCDNUrl(){return this.settings.popup_cdn_uri||a.prod.popup_cdn_uri}getAddWebpageToProjectUrl(){return this.settings.addWebpageToProjectUrl||a.prod.addWebpageToProjectUrl}getUninstallUrl(){const e="uninstallUrl";return this.settings[e]||a.prod[e]}getWhatsNewPageUrl(){return a[this.getEnv()]?.whatsNewPageUrl}getWhatsNewJsonUrl(){return a[this.getEnv()]?.whatsNewJsonUrl}getVersionConfigUrl(){return this.settings.version_config_uri?this.settings.version_config_uri:a.prod.version_config_uri}getOteConfigUrl(){return this.settings.ote_config_uri?this.settings.ote_config_uri:a.prod.ote_config_uri}getModerationListUrl(){return this.settings.moderation_list_uri?this.settings.moderation_list_uri:a.prod.moderation_list_uri}getKWModerationListUrl(){return this.settings.kw_moderation_list_uri?this.settings.kw_moderation_list_uri:a.prod.kw_moderation_list_uri}getExpressBlockListUrl(){return a[this.getEnv()]?.express_moderation_list_uri}getEnv(){return this.settings.env||"prod"}getSignInUrl(){return a[this.getEnv()]?.signInUrl}getFloodgateUrl(){return a[this.getEnv()]?.floodgateUrl}getUserStateUrl(){return a[this.getEnv()]?.userStateUrl}getAnalyticsOverrideConfigUri(){return a[this.getEnv()]?.analyticsOverrideConfigUri}getAcroIpmURL(){return a[this.getEnv()]?.acrobat_ipm_uri}getSidepanelUrl(){return a[this.getEnv()]?.sidepanelUrl}getViewerIMSClientId(){return this.settings.viewer_ims_client_id?this.settings.viewer_ims_client_id:a.prod.viewer_ims_client_id}getViewerIMSClientIdSocial(){return this.settings.viewer_ims_client_id_social?this.settings.viewer_ims_client_id_social:a.prod.viewer_ims_client_id_social}getImsContextId(){return this.settings.ims_context_id?this.settings.ims_context_id:a.prod.ims_context_id}getIMSurl(){return this.settings.imsURL?this.settings.imsURL:a.prod.imsURL}getImsLibUrl(){return this.settings.imsLibUrl?this.settings.imsLibUrl:a.prod.imsLibUrl}getDcApiUri(){return this.settings.dcApiUri?this.settings.dcApiUri:a.prod.dcApiUri}getGenAIPricingUri(){return this.settings.genAIPricingUri?this.settings.genAIPricingUri:a.prod.genAIPricingUri}getExpressURLs(){return a[this.getEnv()]?.express||a.prod.express}getFloodgateuri(){return this.settings.floodgateUri?this.settings.floodgateUri:a.prod.floodgateUri}getWelcomePdfUrlHost(){return this.settings.welcomePdfUrlHost?this.settings.welcomePdfUrlHost:a.prod.welcomePdfUrlHost}getFloodgateSandboxName(){return this.settings.floodgate_sandbox_name?this.settings.floodgate_sandbox_name:a.prod.floodgate_sandbox_name}getLoggingUri(){return"development"===i.getItem("installSource")?"https://dc-api-stage.adobe.io":this.settings.loggingUri?this.settings.loggingUri:a.prod.loggingUri}isAdobeInternalUser(){return"true"===i.getItem("adobeInternal")}createAnonUserUUID(){const t=`${this.getEnv()}_${this.getViewerIMSClientId()}_${e.uuid()}`;try{i.setItem("anonUserUUID",t)}catch(e){}return t}GET_headers(){return this.$GET_headers["x-request-id"]=this.uuid(),this.$GET_headers.Authorization=this.settings.auth_token,this.$GET_headers}POST_headers(){return this.$POST_headers["x-request-id"]=this.uuid(),this.$POST_headers.Authorization=this.settings.auth_token,this.$POST_headers}noToken(e){e&&e.reject()}filesBaseUris(){var t=e.Deferred();return this.settings.files_api?t.resolve():e.ajax({url:this.settings.files_host+"api/base_uris",headers:{Accept:this.GET_headers().Accept,"x-api-client-id":this.GET_headers()["x-api-client-id"]}}).then(this.proxy(function(e){this.settings.files_api=e.api,this.settings.files_upload=e.upload,t.resolve()}),function(){t.reject()}),t.promise()}cloudBaseUris(){var t=e.Deferred();return this.settings.cloud_api?t.resolve():e.ajax({url:this.settings.cloud_host+"api/base_uris",headers:{Accept:this.GET_headers().Accept,"x-api-client-id":this.GET_headers()["x-api-client-id"]}}).then(this.proxy(function(e){this.settings.cloud_api=e.api,this.settings.files_host=e.files,this.settings.fillsign_api=e.fss,this.settings.ims_host=e.ims,this.settings.cpdf_api=e.cpdf,t.resolve()}),function(){t.reject()}),t.promise()}baseUris(){var t=e.Deferred();return this.cloudBaseUris().done(this.proxy(function(){this.filesBaseUris().done(this.proxy(function(){t.resolve()}))})),t.promise()}connect(){var t=e.Deferred();return this.settings.auth_code?(this.baseUris().then(this.proxy(function(){if(this.settings.auth_code){var s={grant_type:"authorization_code",code:this.settings.auth_code,client_id:this.settings.ims_client_id,client_secret:this.settings.ims_client_secret};e.ajax({url:this.settings.ims_host+"ims/token/v1",type:"POST",data:s,contentType:"application/x-www-form-urlencoded;charset=UTF-8"}).then(this.proxy(function(s){var i=(new Date).getTime(),o=/@adobe(test)?\.com$/i.test(s.email);this.settings.auth_code=null,o?(this.settings.auth_token="Bearer "+s.access_token,this.settings.refresh_token=s.refresh_token,this.settings.token_expiry=i+s.expires_in,this.settings.refresh_time=i+s.expires_in/2,this.settings.displayName=s.displayName,t.resolve(),e.consoleLog("got auth token")):(alert("PDF Helper Extension is available to Adobe Employees only"),import("../common/analytics.js").then(({analytics:e})=>{e.event(e.e.EXTENSION_FORCE_UNINSTALL),chrome.management.uninstallSelf()}))}),this.proxy(function(){this.noToken(t)}))}else this.noToken(t)}),this.proxy(function(){this.noToken(t)})),t.promise()):(t.reject(),t.promise())}refreshToken(t){var s,i=(new Date).getTime(),o=e.Deferred();return this.settings.refresh_token?!t&&i<this.settings.refresh_time?o.resolve():i>this.settings.token_expiry?o.reject():(s={grant_type:"refresh_token",refresh_token:this.settings.refresh_token,client_id:this.settings.ims_client_id,client_secret:this.settings.ims_client_secret},e.ajax({url:this.settings.ims_host+"ims/token/v1",type:"POST",data:s,contentType:"application/x-www-form-urlencoded; charset=UTF-8"}).then(this.proxy(function(t){var s=(new Date).getTime();this.settings.auth_token="Bearer "+t.access_token,this.settings.refresh_token=t.refresh_token,this.settings.token_expiry=s+t.expires_in,this.settings.refresh_time=s+t.expires_in/2,o.resolve(),e.consoleLog("refresh token result"),e.consoleLogDir(t)}),this.proxy(function(){this.noToken(o)}))):o.reject(),o.promise()}ajaxReady(t){let s=e.Deferred();return this.settings.auth_token?this.refreshToken(t).then(this.proxy(function(){s.resolve()}),this.proxy(function(){this.noToken(s)})):this.connect().then(this.proxy(function(){this.settings.files_root?s.resolve():e.ajax({url:this.settings.files_api+"root",type:"GET",headers:this.GET_headers()}).then(this.proxy(function(e){s.resolve(),this.settings.files_root=e.id}),this.proxy(function(){this.noToken(s)}))}),this.proxy(function(){this.noToken(s)})),s.promise()}sso_url(t){return e.ajax({url:this.settings.cloud_api+"session/sso_uri?path="+t,type:"GET",headers:this.GET_headers()})}authorize(e){return this.settings.auth_code=e,this.settings.auth_token=null,delete this.settings.refresh_token,delete this.settings.token_expiry,delete this.settings.refresh_time,this.ajaxReady()}clearAuth(){this.settings.auth_token&&(e.ajax({url:this.settings.cloud_api+"session",type:"DELETE",headers:this.$POST_headers}),delete this.settings.auth_token,delete this.settings.refresh_token,delete this.settings.token_expiry,delete this.settings.refresh_time,delete this.settings.displayName)}LOG(t,s){return e.Deferred().resolve()}registerUninstallUrl({locale:t,experimentCodes:s}={}){const r=i.getItem("appLocale"),n=i.getItem("installSource");if(["development","normal","sideload"].includes(n)){const n=new URL(this.getUninstallUrl());if(r)n.searchParams.append("locale",r);else if(t)n.searchParams.append("locale",t);else{let t=i.getItem("viewer-locale")||e.getFrictionlessLocale(chrome.i18n.getMessage("@@ui_locale"));n.searchParams.append("locale",t)}n.searchParams.append("theme",i.getItem("theme")||"auto"),n.searchParams.append("callingApp",chrome.runtime.id);let a=-1;if(i.getItem("extUserState"))a=0;else{const e=i.getItem("viewerStorage");if(e){a=+(0===(parseInt(e.usage)||0))}}n.searchParams.append("nu",a);let c=s;c||(c=o()),c.length>0&&n.searchParams.append("acEx",JSON.stringify(c.sort())),chrome.runtime.setUninstallURL(n.href)}}}n||(n=new c,e.ajaxError(function(e,t,s,i){401===t.status&&n.clearAuth()}));export const common=n;