/*************************************************************************
* 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{events as e}from"../../common/analytics.js";import{dcLocalStorage as t}from"../../common/local-storage.js";import{util as n}from"./content-util.js";import{updateExtUserState as i}from"../../common/util.js";import{OptionsPageSource as o}from"../../common/constant.js";await t.init();const a=t.getItem("appLocale");var s,r,l=!1,c=!1;function d(e,t,i){delete e.click_context,e.authenticatedPDF&&1==e.authenticatedPDF&&delete e.authenticatedPDF,n.messageToMain(e,i),delete e.newUI,delete e.analytics,delete e.is_viewer,delete e.reload_in_native,r&&(clearTimeout(r),r=null),t||"dismiss"===e.content_op||"resize_window"===e.content_op||p()}function _(){r&&(clearTimeout(r),r=null),SETTINGS.IS_ERP_READER?$(".acrobatMainDiv").stop().css("display","none"):$(".acrobatMainDiv").stop().css("opacity",1)}function m(){delete s.current_status,delete s.file_path,delete s.domtitle,delete s.timing,delete s.panel_op,delete s.is_pdf,delete s.newUI}function f(e,t){const i={main_op:"analytics",analytics:[[e,t]]};n.messageToMain(i)}function p(){chrome.runtime?.id?(m(),s.forced_cancel=!0,s.content_op="dismiss",s.main_op="relay_to_content","search"===s.frictionless_workflow&&1==s.show_frictionless&&(s.suppress_frictionless=!0),delete s.newUI,d(s)):document.body.style.display="none"}function E(){r=null,$(".acrobatMainDiv").animate({opacity:0},1500,"swing",p)}function u(){r||l||(r=setTimeout(function(){setTimeout(E)},s.is_pdf?3e3:1500))}function I(e){try{let t=new URL(s.frictionless_uri);e.origin===t.origin&&(s.main_op="external_msg",s.data=e.data,s.timeStamp=Date.now(),n.messageToMain(s))}catch(e){}}function g(){f(e.TREFOIL_SETTINGS_ICON_CLICKED),t.setItem("optionsPageSource",o.FRICTIONLESS_WIDGET_SETTINGS).then(()=>{chrome.runtime.openOptionsPage(()=>{chrome.runtime.lastError&&f(e.TREFOIL_SETTINGS_FAILED_TO_OPEN)})})}function w(){c||(c=!0,window.addEventListener("message",I,!1),$(".close-dialog").click(function(){$(".acrobatMainDiv").length>0&&$(".acrobatMainDiv").hasClass("widget-screen-main-div")&&n.analytics(s,e.FRICTIONLESS_WIDGET_CROSS_CLICKED),p()}),$(".settings-dialog").click(g),$(".action-available-click").click(function(t){let i=$(t.currentTarget);_(),n.consoleLog(i),s.main_op="go_to_aonline",s.verb="acrobat_label",f(e.TREFOIL_ACROBAT_LABEL_CLICKED),d(s)}),$(".acrobatMainDiv").hover(_,u),$(".sign-out").click(function(){_(),n.analytics(s,e.SIGN_OUT_CLICKED),s.main_op="sign-out",d(s)}),$(".do-acro-prefs").click(function(){n.analytics(s,e.TREFOIL_HTML_PREFERENCES_CLICK),s.main_op="showConversionSettingsDialog",d(s)}))}function S(o){let r;if(!(s&&o&&s.tabId&&o.tabId&&o.tabId!=s.tabId)&&o.panel_op){switch(o.version===SETTINGS.ERP_READER_VER&&(SETTINGS.IS_ERP_READER=!0),w(),delete(s={...o,tabId:s.tabId}).analytics,_(),l=!1,n.translateElements(".translate"),$("#action_message").text(""),function(e,t){if(SETTINGS.DEBUG_MODE){let i,o=[t];for(i in e)e.hasOwnProperty(i)&&o.push("  "+i+": "+e[i]);n.consoleLog(o.join("\n"))}}(s,"Receive frame message:"),r=s.panel_op,delete s.panel_op,r){case"load-frictionless":if(l=!0,"resize_window"===s.content_op);else if(1==s.hide_spinner)delete s.hide_spinner,$(".frictionless-container").removeClass("hidden"),$(".frictionless-loader").addClass("hidden");else if(0==$(".frictionless-container").children().length){$(".acrobatMainDiv").removeClass("home-screen"),$(".acrobatMainDiv").addClass("widget-screen-main-div"),$(".actions").addClass("hidden"),"search"===s.frictionless_workflow&&i();let n=function(n,i){let o=document.createElement("iframe");o.setAttribute("referrerpolicy","no-referrer"),o.classList.add("frictionless-iframe");let r=new URL(n);if(i&&(a?i.locale=a:s.locale?i.locale=s.locale:i.locale=chrome.i18n.getMessage("@@ui_locale"),Object.keys(i).forEach(e=>{r.searchParams.append(e,i[e])})),"false"===t.getItem("logAnalytics")){let e=r.toString();e=e.concat("&app!analytics=disable");try{r=new URL(e)}catch(e){r=""}}if("test"===s.env||"stage"===s.env){let e=r.toString();e=e.concat("&app!versions=latest");try{r=new URL(e)}catch(e){r=""}}return o.onerror=function(){f(e.FRICTIONLESS_WIDGET_LOADING_FAILED)},o.onload=function(){d({iframe_onload_time:Date.now(),main_op:"timing_info",timing_op:"startup_to_iframe_load"},!0),"trefoil"===i.workflow&&o.contentWindow.postMessage({valid:!0},r.origin)},d({iframe_call_time:Date.now(),main_op:"timing_info"},!0),o.setAttribute("src",r),o}(s.frictionless_uri,{verb:s.pdf_action,workflow:s.frictionless_workflow,dropzone2:"true",useExtensionStorage:!0});$(".frictionless-container").append(n),$(".frictionless-host").removeClass("hidden"),T()}break;case"show-frictionless-error":if(s.error_title&&s.error_description){let e=s.error_title,t=s.error_description;delete s.error_title,delete s.error_description,function(e,t){const n=$(".frictionless-error"),i=$(".error-title");$(".error-details").text(t),i.text(e),n.removeClass("hidden"),T()}(e,t)}break;case"clear-frictionless-error":$(".frictionless-error").addClass("hidden"),T();break;case"send-external-msg":try{let e=new URL(s.frictionless_uri),t=$(".frictionless-iframe");if(1==t.length){t[0].contentWindow.postMessage(s.data,e.origin),delete s.data}}catch(e){}l=!0}u()}}function T(){m(),s.content_op="resize_window",s.panel_op="load-frictionless",s.main_op="relay_to_content",delete s.newUI,d(s),s.content_op=null,s.panel_op=null}n.addMainListener(S),$(function(){w(),window.location.search&&(s=JSON.parse(decodeURIComponent(window.location.search.split("=")[1])),SETTINGS.TEST_MODE&&window.addEventListener("message",function(e){S(e.data)},!1),$("body").addClass("desktop-app-reader"),"html_menu"==s.panel_op&&($(".acrobatMainDiv").addClass("home-screen"),f(e.FRICTIONLESS_HOME_SCREEN_SHOWN)),function(e){var t;void 0!==(t=e)&&void 0!==t.paramName&&delete t.paramName,SETTINGS.FILL_N_SIGN_ENABLED=e.isFillnSignEnabled,SETTINGS.SHAREPOINT_ENABLED=e.isSharePointEnabled,S(e)}(s))});