/*************************************************************************
* 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 a}from"./content-util.js";import{privateApi as n}from"./content-privateApi.js";import{SETTINGS as s}from"../../sw_modules/settings.js";import{sendPingEventHandler as o}from"../../common/util.js";await t.init();var i,r,d=!1,c=!1;function l(e,t){e.authenticatedPDF&&1==e.authenticatedPDF&&delete e.authenticatedPDF,a.messageToMain(e),delete e.newUI,delete e.analytics,r&&(clearTimeout(r),r=null),t||"dismiss"===e.content_op||E()}function _(){a.consoleLog("Clear timer: "+r),r&&(clearTimeout(r),r=null),s.IS_ERP_READER?$(".acrobatMainDiv").stop().css("display","none"):($(".acrobatMainDiv").stop().css("opacity",1),$(".convert-status").stop().css("opacity",1))}function E(){i.content_op="dismiss",i.main_op="relay_to_content",i.newUI=!0,l(i)}function p(){$(".convert-status").addClass("hidden"),$(".progress-area").addClass("hidden")}function m(){r=null,$(".convert-status").animate({opacity:0},1500,"linear",p)}function u(){(s.TEST_MODE||s.DEBUG_MODE)&&(r=1),r||d||(r=setTimeout(function(){setTimeout(m)},i.is_pdf?3e3:1500))}async function R(){i.main_op="open_in_acrobat",i.newUI=!0,void 0!==i.paramName&&delete i.paramName,delete i.content_op,delete i.is_viewer,delete i.click_context,delete i.dataURL;l(await a.handlePDFURL(i,!1),!0)}async function f(e){i.main_op="open_in_acrobat",i.newUI=!0,i.paramName=e,delete i.content_op,delete i.is_viewer,delete i.dataURL,delete i.click_context;l(await a.handlePDFURL(i,!1),!0)}function D(n){c||(c=!0,$(".do_acrobat").click(function(e){var t=$(e.currentTarget);_(),s.FILL_N_SIGN_ENABLED&&t.hasClass("do_acrobat_FS")?(o(),f("FillnSign")):t.hasClass("do_acrobat")&&(o(),R())}),$(".close-dialog").click(function(){if(i&&i.version){var t=e.PERSIST_PDF_MENU_CLOSED;t=s.FILL_N_SIGN_ENABLED&&"stripTextOpenFillnSign"===i.stripId?i.version===s.READER_VER?e.PERSIST_PDF_MENU_CLOSED_READER_FS:e.PERSIST_PDF_MENU_CLOSED_ACROBAT_FS:i.version===s.READER_VER?e.PERSIST_PDF_MENU_CLOSED_READER:e.PERSIST_PDF_MENU_CLOSED,a.analytics(i,t)}i.fteClosed=!1,E()}),$(".always-show").prop("checked","false"!==t.getItem("always-show-pdf-menu")),$(".always-show").click(function(){var e=$(".always-show").prop("checked")?"true":"false";t.setItem("always-show-pdf-menu",e)}))}function v(o){var r,c,l,E=!0,p=!0,m=!1,v="web2pdfPDFOpenFailedv2",C=!(0==o.version||1==o.version||o.version===s.READER_VER||o.version===s.ERP_READER_VER);o.version===s.READER_VER||(o.version,s.ERP_READER_VER);if(o.panel_op){if(o.test_extension)return function(e){a.consoleLog("TESTING"),a.consoleLogDir(JSON.stringify(e)),"doAcrobat"===e.test_extension&&($("persistent.do_acrobat").not(":hidden").hasClass("do_acrobat_FS")?f("FillnSign"):R()),void 0!==e.test_extension&&delete e.test_extension}(o);if(!o.authenticatedPDF)switch(o.version===s.ERP_READER_VER&&(s.IS_ERP_READER=!0),D(),delete(i=o).analytics,d=!1,$(".acrobatMainDiv").stop().css("opacity",1),a.translateElements(".translate"),1===i.version&&$("#web2pdfOpenButtonText").val(a.getTranslation("web2pdfOpenButtonTextOlder")),i.version===s.READER_VER&&$("#web2pdfOpenButtonText").val(a.getTranslation("web2pdfOpenButtonText")),$(".ui-element").addClass("hidden"),$("#action_message").text(""),function(e,t){if(s.DEBUG_MODE){var n,o=[t];for(n in e)e.hasOwnProperty(n)&&o.push("  "+n+": "+e[n]);a.consoleLog(o.join("\n"))}}(i,"Receive frame message:"),c=i.panel_op,delete i.panel_op,delete i.dataURL,delete i.click_context,delete i.is_viewer,c){case"pdf_menu":const c=t.getItem("fteDenied");if(a.isEdge()&&s.VIEWER_ENABLED&&(!C||s.VIEWER_ENABLED_FOR_ACROBAT))try{t.getItem("pdfViewer")||(t.setItem("fte","false"),t.setItem("pdfViewer","true"),n.setViewerState("enabled"),a.messageToMain({main_op:"analytics",analytics:[[e.USE_ACROBAT_IN_EDGE_AUTO_ENABLED]]}),T(i.tabId))}catch(t){a.analytics(i,e.LOCAL_STORAGE_DISABLED)}!C||s.VIEWER_ENABLED&&s.VIEWER_ENABLED_FOR_ACROBAT?!s.VIEWER_ENABLED||t.getItem("pdfViewer")||c&&10===parseInt(c)||C&&!s.VIEWER_ENABLED_FOR_ACROBAT||i.incognito?o.version<=1||!s.VIEWER_SHOW_OPEN_IN_ACRO||10!==c&&"false"!==t.getItem("pdfViewer")||($(".acrobatMainDiv").removeClass("hidden"),$(".acro-option.pdf").removeClass("hidden")):(c&&parseInt(c)>=9&&($("#acc-cancel").addClass("hidden"),$("#acc-deny").removeClass("hidden")),function(e){switch(e.fteFeatureFlag){case"dc-cv-fte-pdf-redcard":$(".acrobat-only-side-card").removeClass("hidden");break;case"dc-cv-fte-pdf-dmb":$(".acrobat-only-top-bar").removeClass("hidden");break;default:$(".acrobat-only-card").removeClass("hidden")}}(i)):($(".acrobatMainDiv").removeClass("hidden"),$(".acro-option.pdf").removeClass("hidden"),$(".acrobat-only-card").addClass("hidden")),t.getItem("pdfViewer")&&$(".acrobat-only-card").addClass("hidden"),$(".acrobatMainDiv").off("hover");break;case"error":$(".error").removeClass("hidden").text("Unexpected Error:"+i.error.name+"\nReference: "+i.error.errnum+"\n"+i.error.details);break;case"flickr":$(".action-available").removeClass("hidden"),$("#action_message").text("Create slide shows and contact sheets."),$(".special_question").removeClass("hidden"),$("#special").removeClass("hidden");break;case"status":$(".progress-area").removeClass("hidden"),$(".convert").text(i.domtitle),$(".convert-status, .convert-title").addClass("hidden"),$(".convert").removeClass("convert-button hidden"),$(".acrobatMainDiv").off("hover"),"waiting"===i.current_status?(a.analytics(i,e.TREFOIL_HTML_CONVERT_WAITING),r=a.getTranslation("web2pdfStatusWaiting"),E=!1):"downloading"===i.current_status?(a.analytics(i,e.TREFOIL_HTML_CONVERT_DOWNLOADING),r=a.getTranslation("web2pdfStatusDownloading"),E=!1):"in_progress"===i.current_status?(a.analytics(i,e.TREFOIL_HTML_CONVERT_IN_PROGRESS),r=a.getTranslation("web2pdfStatusInProgress"),E=!1):"filelocked"===i.current_status?r=a.getTranslation("web2pdfFileLockedError"):"cancelled"===i.current_status?(a.analytics(i,e.TREFOIL_HTML_CONVERT_CANCELLED),r=a.getTranslation("web2pdfStatusCancelled"),m=!0):"complete"===i.current_status?(a.analytics(i,e.TREFOIL_HTML_CONVERT_COMPLETE),i.file_path?($(".convert").text(a.getTranslation("web2pdfOpenInDCButtonText")),$(".convert").addClass("convert-button")):($(".convert").empty(),$(".convert").addClass("hidden"))):"failure"===i.current_status?(a.analytics(i,e.TREFOIL_HTML_CONVERT_FAILED),i.message&&(r=i.message,r=$("<div/>").text(r).html()),p=!1):"noacrobat"===i.current_status?(a.analytics(i,e.TREFOIL_HTML_CONVERT_NO_ACROBAT),r=a.getTranslation("web2pdfUnsupportedAcrobatVersion"),p=!1):"unknown"===i.current_status?(r=a.getTranslation("web2pdfStatusUnknownFailure"),p=!1):"pdf_downloading"===i.current_status?(r=a.getTranslation("web2pdfStatusDownloadingPDF"),E=!1):"pdf_failure"===i.current_status?(i.version===s.READER_VER?a.analytics(i,e.PERSIST_PDF_DOWNLOAD_FAILED_READER):a.analytics(i,e.PERSIST_PDF_DOWNLOAD_FAILED),v="web2pdfStatusUnknownFailure",p=!1):"pdf_downloaded"===i.current_status?(r=a.getTranslation("web2pdfPDFOpening"),E=!1):"pdf_opened"===i.current_status?(p=!0,"web2pdfPDFOpened"):"pdf_open_failed"===i.current_status&&(p=!1,E=!0,v="web2pdfPDFOpenFailedv2"),r&&($(".in-process").removeClass("hidden"),$(".convert-title").removeClass("hidden"),$(".convert-title").html(r)),E?(delete i.panel_op,$(".acrobatMainDiv").hover(_,u),$(".actions").removeClass("hidden"),$(".in-process").addClass("hidden"),l=i.is_pdf?".acro-option.pdf":".acro-option.html",$(l).removeClass("hidden"),$(".convert").removeClass("convert-busy"),$(".convert-status").removeClass("hidden"),$(".convert-status").stop().css("opacity",1),p?($(".progress-area").addClass("hidden"),$(".convert").removeClass("convert-busy"),$(".convert-status").addClass("hidden")):($(".convert-status-icon").removeClass("icon-success"),$(".convert-status-icon").addClass("icon-error"),$(".convert-status-title").text(a.getTranslation(v)),$(".convert").addClass("hidden")),m&&($(".convert-status").addClass("hidden"),$(".convert").addClass("hidden")),u()):(d=!0,$(".actions").addClass("hidden"),$(".convert").addClass("convert-busy"))}}}function C(t){var n,o,i;if(void 0!==(n=t)&&(void 0!==n.stripId&&delete n.stripId,void 0!==n.paramName&&delete n.paramName),s.FILL_N_SIGN_ENABLED=t.isFillnSignEnabled,s.SHAREPOINT_ENABLED=t.isSharePointEnabled,!0===(o=t,i=!1,s.FILL_N_SIGN_ENABLED?void 0===o.stripId&&(void 0!==o.url&&!0===a.isPDFForm(o.url)?(o.stripId="stripTextOpenFillnSign",i=!0):o.stripId="stripTextOpenAcrobat"):o.stripId="stripTextOpenAcrobat",i)){$(".acrobatMainDiv").width(205),$(".acro-option.pdf").addClass("do_acrobat_FS");{let a={tab:t.tab,main_op:"analytics",analytics:[]};t.version===s.READER_VER?a.analytics.push(e.PDF_FORM_DETECTED_READER):a.analytics.push(e.PDF_FORM_DETECTED_ACROBAT),l(a,!0)}}$(".acro-option.pdf").attr("id",t.stripId),v(t)}function T(e){setTimeout(()=>{chrome.tabs.reload(e)},100)}a.isChrome()&&$(function(){D()}),a.addMainListener(v),$(function(){window.location.search&&(i=JSON.parse(decodeURIComponent(window.location.search.split("=")[1])),s.TEST_MODE&&window.addEventListener("message",function(e){C(e.data)},!1),C(i),"false"===t.getItem("ViewResultsPref")?!1:($(".do_set_open_pref").addClass("open-pdf-in-acrobat"),!0),$("#acc-cancel, #sideCardCloseIcon, #acc-top-bar-close").on("click",function(){let n,s=t.getItem("repromptCount");switch(this.id){case"acc-top-bar-close":n=e.PERSIST_PDF_MENU_DMB_CANCEL;break;case"sideCardCloseIcon":n=e.PERSIST_PDF_MENU_RED_CARD_CANCEL;break;default:n=e.PERSIST_PDF_MENU_FTE_CANCEL}s?a.messageToMain({main_op:"analytics",analytics:[[n,{REPROMPT:"Reprompt"}]]}):a.messageToMain({main_op:"analytics",analytics:[[n,{":REPROMPT":""}]]});let o=t.getItem("fteDenied");if(o=o&&parseInt(o)>=6?parseInt(o)+1:6,10===o){let e=Date.now();t.setItem("reprompt-user-timestamp",e)}t.setItem("persist-menu-closed",o-1),t.setItem("fteDenied",o),i.fteClosed=!0,E()}),$("#acc-deny").on("click",function(){let n=t.getItem("fteDenied");if(t.getItem("repromptCount")?a.analytics(i,e.PERSIST_PDF_MENU_FTE_REPROMPT_DENIED):a.analytics(i,e.PERSIST_PDF_MENU_FTE_DENIED),n=n&&parseInt(n)>=6?parseInt(n)+1:6,t.setItem("fteDenied",n),10===n){let e=Date.now();t.setItem("reprompt-user-timestamp",e)}i.fteClosed=!0,E()}),$("#acc-ok, #top-bar-acc-ok, #side-card-acc-ok").on("click",function(){let s=!1;try{let i;o();let r=t.getItem("repromptCount");switch(this.id){case"top-bar-acc-ok":i=e.PERSIST_PDF_MENU_DMB_CONFIRM;break;case"side-card-acc-ok":i=e.PERSIST_PDF_MENU_RED_CARD_CONFIRM;break;default:i=e.PERSIST_PDF_MENU_FTE_ACCEPTED}r?a.messageToMain({main_op:"analytics",analytics:[[i,{REPROMPT:"Reprompt"}]]}):a.messageToMain({main_op:"analytics",analytics:[[i,{":REPROMPT":""}]]}),t.getItem("pdfViewer")||t.setItem("viewer-enabled-source","ownership-consent"),t.setItem("pdfViewer","true"),n.setViewerState("enabled"),s=!0}catch(t){a.messageToMain({main_op:"analytics",analytics:[[e.LOCAL_STORAGE_DISABLED]]})}s&&T()}),$("#acc-top-bar-close").on("click",function(){$(".acrobat-only-top-bar").addClass("hidden")}),$("#fteSideCard").hover(function(){$(".fteSideCard-content-body").toggleClass("toggled")}))});