/*************************************************************************
* 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{communicate as e}from"./communicate.js";import{common as t}from"./common.js";import{util as s}from"./util.js";import{Proxy as o}from"./proxy.js";import{analytics as i}from"../common/analytics.js";import{session as r}from"./session.js";import{frictionlessHandler as n}from"./frictionless-handler.js";import{floodgate as a}from"./floodgate.js";import{privateApi as l}from"./private-api.js";import{SETTINGS as p}from"./settings.js";import{dcLocalStorage as m,dcSessionStorage as c}from"../common/local-storage.js";import{googleQueryAnalyzer as d}from"./googleQueryAnalyzer.js";import{CACHE_PURGE_SCHEME as _}from"./constant.js";import{loggingApi as f}from"../common/loggingApi.js";var E=null;E||(E=new function(){this.proxy=o.proxy.bind(this),this.LOG=(...e)=>t.LOG(...e),this.sign_out=function(e,t){r.signOut()},this.dismiss=function(e,t){s.consoleLog("dismiss/ok")},this.specialCases=p.USE_FLICKR?[{regex:/http[s]:\/\/www\.flickr\.com/,action:"flickr",lastPromptTime:null}]:[],this.handleSpecialUrl=function(t,o){var i=!1;return s.each(this.specialCases,function(r,n){if(n.regex.test(t)){var a=!0,l=(new Date).getTime();n.lastPromptTime&&l-n.lastPromptTime<1e4&&(a=!1),a&&(n.lastPromptTime=l,s.consoleLog("INVITE: "+l),e.deferMessage({panel_op:n.action,tabId:o})),i=!0}}),i},this.fill_sign=function(e,n,a){(e.userSelectPromise||s.Deferred().resolve().promise()).then(this.proxy(function(){s.consoleLog("fill and sign");var n={url:t.settings.fillsign_api+"createform",contentType:"application/json",accept:t.GET_headers().Accept,type:"POST",dataType:"json",xhrFields:{withCredentials:!0},headers:t.POST_headers()};n.data=JSON.stringify({asset_id:e.assetId}),r.message("Preparing for Fill and Sign",!0),i.event(i.e.CREATE_FORM_PROGRESS_SHOWN),s.ajax(n).then(this.proxy(function(t){s.consoleLog("form created"),s.consoleLogDir(t),e.form_path="fillsign/"+t.form_id,i.event(i.e.CREATE_FORM_COMPLETE),i.event(i.e.REDIRECT),r.gotoPath(e.form_path),a&&a("OP_SUCCESS")}),this.proxy(function(e){return s.consoleLog("form create failed"),o.REST_error(e,this),a&&a("OP_FAILURE"),e}))}))},this.newTab=function(s,o,n){var l=chrome.runtime.getURL("browser/js/options.html");if(!(p.CHROME_VERSION<p.SUPPORTED_VERSION))if(0!==s.indexOf(t.settings.redirect_uri+"?code=")){if(s.includes(l)){!function(){try{const e=m.getItem("optionTabIds");e&&Array.isArray(e)&&e.length>0&&(c.setItem("optionTabIds",e),m.removeItem("optionTabIds"))}catch(e){f.error({message:"Error migrating optionTabIds to session storage",error:e})}}();const e=c.getItem("optionTabIds")||[];return e.includes(o)||e.push(o),c.setItem("optionTabIds",e),void a.hasFlag("dc-cv-new-preferences",_.NO_CALL).then(e=>{e?i.event(i.e.OPTIONS_PAGE,{pageSource:"new_preferences_page"}):i.event(i.e.OPTIONS_PAGE,{pageSource:"legacy_options_page"})})}r.checkSessionTab(o,s),e.avoidUrl(s)?e.disable(o):E.handleSpecialUrl(s,o)||e.loaded(o)}else r.foundCode(s)},this.startup=function(t,o){if(this.started||(this.newTab(t.url,o.tab.id,t.is_pdf?"application/pdf":"text/html"),l.isMimeHandlerAvailable().then(e=>{e&&navigator.onLine&&fetch("https://acrobat.adobe.com/dc-chrome-extension/index.html",{method:"GET"}).then(e=>{if(200!==e.status)l.setViewerState("disabled"),m.setItem("cdnFailure","true"),chrome.tabs.reload(),i.event(i.e.VIEWER_FALLBACK_TO_NATIVE_CDN_OFFLINE);else if("true"===m.getItem("cdnFailure")){m.setItem("cdnFailure","false");const e="true"===m.getItem("pdfViewer")?"enabled":"disabled";l.setViewerState(e),chrome.tabs.reload()}}).catch(e=>{s.consoleError(e)})}),this.started=!0),t.is_pdf&&(e.setIsPDF(o.tab.id,!0),Promise.all([a.getReleaseVariant("dc-cv-pdf-fte-experiments"),a.hasFlag("dc-cv-open-in-acrobat")]).then(([e,s])=>{t.fteFeatureFlag=e,m.setItem("openInAcrobatEnable",!!s)}).finally(()=>{"update"===m.getItem("installType")&&function(){const e="dc-cv-default-ownership-experiment1";if(!m.getItem("pdfViewer")){m.setItem("pdfViewer","true"),m.setItem("defaultOwnerShipExperiment",e),l.setViewerState("enabled"),m.setItem("viewer-enabled-source","ownership-upgrade"),m.setItem("experiment-ownership",e);const t=24*p.OWNERSHIP_EXPERIMENT_TOGGLE_OFF_TIME_LIMIT*60*60*1e3;m.setWithTTL("ownership-upgrade","true",t),s.isEdge()?(i.event(i.e.USE_ACROBAT_IN_EDGE_AUTO_ENABLED_ON_UPDATE),i.event(i.e.USE_ACROBAT_IN_EDGE_AUTO_ENABLED_ON_UPDATE_SOURCE,{SOURCE:e})):(i.event(i.e.USE_ACROBAT_IN_CHROME_AUTO_ENABLED_ON_UPDATE),i.event(i.e.USE_ACROBAT_IN_CHROME_AUTO_ENABLED_ON_UPDATE_SOURCE,{SOURCE:e}))}}(),e.pdf_menu(t,o)})),t.show_frictionless=!0,t.startup_time&&(n.startNewInteraction(t.tabId).startup_time=t.startup_time),p.FRICTIONLESS_ENABLED&&(p.TEST_MODE||t.show_frictionless)&&"false"!==m.getItem("always-show-pdftools")&&1!=t.suppress_frictionless&&d&&d.isGoogleQuery(t)){const e=d.getSearchQuery(t);e&&d.mapQueryStringToAction(e,t)}}},e.registerHandlers({dismiss:E.proxy(E.dismiss),ok:E.proxy(E.dismiss),fillsign:E.proxy(E.fill_sign),"sign-out":E.proxy(E.sign_out),"html-startup":E.proxy(E.startup),"pdf-menu":E.proxy(E.startup)}));export const downloadManager=E;