/*************************************************************************
* 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{analyticsOverrideConfigManager as e}from"./analyticsOverrideConfigManager.js";import{compareCalendarMonth as t,compareCalendarWeek as s,compareCalendarDay as r}from"./calendarUtils.js";import{analyticsIndexedDB as o}from"./analyticsIndexedDB.js";import{dcSessionStorage as n,dcLocalStorage as a}from"./local-storage.js";import{util as i}from"../sw_modules/util.js";import{floodgate as l}from"../sw_modules/floodgate.js";export const addLocalStorageARFFlagValue=async()=>{await a.init();await l.hasFlag("dc-cv-analytics-restriction-framework")?a.setItem("arfEnabled","true"):a.removeItem("arfEnabled")};export const analyticsThrottlingService=new class{constructor(){this.ready=n.init().then(()=>{this.browserSessionId=n.getItem("analyticsThrottlingSessionId"),this.browserSessionId||(this.browserSessionId=i.generateUUID(),i.consoleLog("AnalyticsThrottlingService: Generated sessionId",this.browserSessionId),n.setItem("analyticsThrottlingSessionId",this.browserSessionId))})}shouldThrottleByFrequency(e,o,n){const a=e.lastSentTimeStamp;if(!a)return!1;const i=Date.now();switch(o){case"always":default:return!1;case"hourly":return i-a<36e5;case"daily":return r(a,i)<=0;case"weekly":return s(a,i)<=0;case"monthly":return t(a,i)<=0;case"session":return this.shouldThrottleBySession(e);case"tabSession":return this.shouldThrottleByTabSession(e,n)}}shouldThrottleBySession(e){return e.session?.id===this.browserSessionId}shouldThrottleByTabSession(e,t){return!(e.session?.id!==this.browserSessionId||!e.session?.tabs?.includes(t))}async eventRandomNumber(e,t){const{pageName:s,eVars:r,props:o}=e,n=t?.eVars??[],l=t?.props??[],d=s+"/"+n.sort().map(e=>r[e]??"").join("_")+"/"+l.sort().map(e=>o[e]??"").join("_");let c=a.getItem("analyticsRandomSeed");return c||(c=i.generateUUID(),a.setItem("analyticsRandomSeed",c)),this.randomNumberFromString(`${d}${c}`)}async randomNumberFromString(e){const t=(new TextEncoder).encode(e),s=await crypto.subtle.digest("SHA-256",t),r=new Uint8Array(s);return((r[0]<<24|r[1]<<16|r[2]<<8|r[3])>>>0)%100}async shouldThrottleBySamplingRate(e,t,s){return await this.eventRandomNumber(e,t)>=s}_generateObjectStoreKey(e,t){const{pageName:s,eVars:r,props:o}=e,n=t?.eVars??[],a=t?.props??[],l=s+"/"+n.sort().map(e=>r[e]??"").join("_")+"/"+a.sort().map(e=>o[e]??"").join("_");return i.stringToSHA256(l)}async shouldThrottle(t,s,r){await this.ready;const n=t.pageName;!r&&chrome.tabs?.getCurrent&&(r=await chrome.tabs.getCurrent().then(e=>e?.id));const a=await e.getConfig(n,s);if(a.is_blocked)return i.consoleLog("Event throttled by cdn blocklist",n),!0;if(a.end_date&&a.end_date<Date.now())return i.consoleLog("Event throttled by end date config",n,a.end_date),!0;if(void 0!==a.sampling_rate&&a.sampling_rate<100&&await this.shouldThrottleBySamplingRate(t,a.uniqueIdentifier,a.sampling_rate))return i.consoleLog("Event throttled by sampling rate",n,a.sampling_rate),!0;const l=e=>{if(e=e??{},this.shouldThrottleByFrequency(e,a.frequency,r))return i.consoleLog("Event throttled by frequency",n,a.frequency),{shouldThrottle:!0,updatedRecord:e};const t=e.session??{};t.id!==this.browserSessionId&&(t.id=this.browserSessionId,t.tabs=[]);const s=t.tabs??[];return r&&!s.includes(r)&&s.push(r),t.tabs=s,e.session=t,e.lastSentTimeStamp=Date.now(),{shouldThrottle:!1,updatedRecord:e}};try{const e=await this._generateObjectStoreKey(t,a.uniqueIdentifier);return(await o.createTransaction(e,l)).shouldThrottle}catch(e){return i.consoleError("AnalyticsThrottlingService: Error in indexeddb transaction",e),!1}}};