/*************************************************************************
* 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{dcLocalStorage as e}from"../common/local-storage.js";import{analytics as t}from"../common/analytics.js";import{loggingApi as a}from"../common/loggingApi.js";import{communicate as r}from"./communicate.js";import{floodgate as n}from"./floodgate.js";import{CACHE_PURGE_SCHEME as s}from"./constant.js";import{safeJsonParse as i}from"../common/util.js";import{isAddWebpageToProjectEnabled as o}from"./add-webpage-to-project.js";const c="whatsNewVersionCheck",l=864e5;async function w(){try{return!!await n.hasFlag("dc-cv-whats-new",s.NO_CALL)&&function(){try{const{appLocale:t,viewerLocale:a,locale:r}=e.getItems(["appLocale","viewer-locale","locale"]),s=t||a||r,o=n.getFeatureMeta("dc-cv-whats-new");if(!o)return!0;const c=i(o,{}),l=c?.locales;return!l||!Array.isArray(l)||0===l.length||l.includes(s)}catch(e){return a.error({message:"Error checking What's New locale eligibility",error:e?.message}),!1}}()}catch(e){return a.error({message:"Error checking What's New eligibility",error:e?.message}),!1}}async function u(){try{if(!navigator.onLine)return!1;const t=e.getItem("whatsNewState")||{},a=t?.whatsNewJsonUrl;if(!a)return!1;const r=new URL(a),n=await fetch(r.toString(),{method:"GET"});if(!n.ok)throw new Error(`Failed to fetch wn.json: ${n.status}`);let s=await n.json();s=s.slice(0,13);const i=s[0];if(!i)return!1;const o=e.getItem("whatsNewState")?.lastSeenWn?.version,c=!o||o!==i.whatsNewVersion;if(c){const t=e.getItem("whatsNewState")||{};e.setItem("whatsNewState",{...t,currentAvailableWN:i.whatsNewVersion})}return c}catch(e){return a.error({message:"WhatsNew VersionCheck Failed",error:e?.message}),!1}}async function m(){try{const t=await w();if(e.setItem("whatsNew",!!t),!t)return void g();let r=10080;r=function(e,t=10080){if(!e)return t;try{const{alarmInterval:a}=JSON.parse(e)||{},r=parseInt(a,10);return Number.isFinite(r)&&r>0?r:t}catch{return t}}(n.getFeatureMeta("dc-cv-whats-new"));const s=await chrome.alarms.get(c);s&&s?.periodInMinutes===r||(await g(),chrome.alarms.create(c,{periodInMinutes:r,delayInMinutes:1}),a.info({message:"WhatsNew Alarm Created"}))}catch(e){}}async function g(){try{await chrome.alarms.get(c)&&chrome.alarms.clear(c)}catch(e){}}async function h(e){if(e&&e.name===c){if(!await w())return void await g();try{await u()&&a.info({message:"WhatsNew New Version Detected"})}catch(e){}}}async function f(t="browserStartup"){try{if(!navigator.onLine)return{eligible:!1,reason:"offline"};if(!await w())return{eligible:!1,reason:"disabled"};const{whatsNewState:a={},whatsNewAutoOpen:r}=e.getItems(["whatsNewState","whatsNewAutoOpen"]),{currentAvailableWN:n,lastSeenWn:s,disableAutoOpenByAdmin:i}=a||{};if(!n)return{eligible:!1,reason:"no_current_wn"};const o=Date.now();if("browserStartup"===t){if(!0===i)return{eligible:!1,reason:"admin_disabled"};if(s?.version&&n===s.version)return{eligible:!1,reason:"already_seen_version"};const t=e.getItem("installTimestamp");if(!s&&(t?(o-t)/l:0)<=15)return{eligible:!1,reason:"new_user_grace"};if("false"===r)return{eligible:!1,reason:"auto_open_toggle_off"};if(s?.timestamp){if((o-s.timestamp)/l<=25)return{eligible:!1,reason:"cooldown"}}}return{eligible:!0,currentAvailableWN:n}}catch{return{eligible:!1,reason:"error"}}}async function N(r){try{if(!navigator.onLine)return;const n=await f(r);if(!n?.eligible)return;const s=n.currentAvailableWN,i=Date.now(),o=function(){try{const t=e.getItem("whatsNewState")?.whatsNewPageUrl;return t||(a.error({message:"No What's New URL found in servers config"}),null)}catch(e){return null}}();if(o){const a=e.getItem("whatsNewState")||{};e.setItem("whatsNewState",{...a,currentWhatsNewOpenSource:r}),await chrome.tabs.create({url:o,active:!0});const n={version:s,timestamp:a.lastSeenWn?.timestamp};"browserStartup"===r&&(n.timestamp=i);const c=e.getItem("whatsNewState")||{};e.setItem("whatsNewState",{...c,lastSeenWn:n}),t.event("DCBrowserExt:WhatsNew:PageOpened",{version:s,trigger:r})}}catch(e){}}r.registerHandlers({getWhatsNewData:async function(t,a,r){try{let t=e.getItem("whatsNewState")||{};const a=t.currentWhatsNewOpenSource;if(a){const{currentWhatsNewOpenSource:a,...r}=t;e.setItem("whatsNewState",r),t=r}const{appLocale:n,viewerLocale:s,locale:i,isAllowedLocalFileAccess:c}=e.getItems(["appLocale","viewer-locale","locale","isAllowedLocalFileAccess"]),l=t.currentAvailableWN,w=t.disableAutoOpenByAdmin,u=n||s||i;let m=!1;try{m=await o()}catch(e){}r({success:!0,currentExtensionVersion:chrome.runtime.getManifest().version,whatsNewVersion:l,locale:u,source:a||"direct",isAllowedLocalFileAccess:c,disableAutoOpenByAdmin:w,isPDFSpacesEnabled:m})}catch(e){return{success:!1,error:e.message}}},openWhatsNew:({source:e})=>N(e),getWhatsNewAutoOpenEligibility:async function(e,t,r){try{if(await w()){await u()&&a.info({message:"WhatsNew New Version Detected"})}r({eligible:!!(await f()).eligible})}catch(e){r({eligible:!1})}},getWhatsNewFlag:async function(e,t,a){try{a(await w())}catch(e){a(!1)}}});export{g as clearWhatsNewVersionCheckAlarm,h as whatsNewAlarmHandler,N as checkAndOpenWhatsNewPage,m as checkAndInitialiseWhatsNewAlarm};