define(function(require,exports,module){const KernalModeTrust=window.KernalModeTrust;if(!KernalModeTrust)throw new Error("Metrics should have access to KernalModeTrust. Cannot boot without trust ring");const MAX_AUDIT_ENTRIES=3e3,ONE_DAY=864e5;let initDone=!1,disabled=!1,loggedDataForAudit=new Map,isFirstUseDay,userID,isPowerUserFn,powerUserPrefix,cachedIsPowerUser=!1;function _setUserID(){const userIDKey="phoenixUserPseudoID";(userID=window.PhStore.getItem(userIDKey))||(userID=crypto.randomUUID(),window.PhStore.setItem(userIDKey,userID))}function _setFirstDayFlag(){const firstUseDayKey="healthData.firstUseDay";let firstBootTime=window.PhStore.getItem(firstUseDayKey);firstBootTime||(firstBootTime=Date.now(),window.PhStore.setItem(firstUseDayKey,firstBootTime));let firstUseDay=new Date(firstBootTime),dayAfterFirstUse=new Date(firstUseDay);dayAfterFirstUse.setUTCDate(firstUseDay.getUTCDate()+1);let today=new Date;(isFirstUseDay=today500&&window.dataLayer.splice(0,250)})}_createAnalyticsShims();const MINUTES_10=1e4;let tauriGaErrorCountSent=0,sendOnceMore=!1,noFurtherReporting=!1;function _sendTauriGAEvent(analyticsID,customUserID,events=[]){window.__TAURI__.event.emit("health",{analyticsID:analyticsID,customUserID:customUserID,events:events}).catch(err=>{window.debugMode&&console.error(err),noFurtherReporting||(tauriGaErrorCountSent++,sendOnceMore&&(noFurtherReporting=!0,logger.reportError(err,`${tauriGaErrorCountSent} _sendTauriGAEvent failures in ${MINUTES_10/1e3} minutes`)),1===tauriGaErrorCountSent&&(logger.reportError(err),setTimeout(()=>{sendOnceMore=!0},MINUTES_10)))})}function _sendNativeGAEvent(analyticsID,customUserID,events=[]){if(window.__TAURI__)return _sendTauriGAEvent(analyticsID,customUserID,events);window.__ELECTRON__?window.electronAPI.sendHealthMetric({analyticsID:analyticsID,customUserID:customUserID,events:events}):console.error("Metrics send event failed. Unknown native platform")}let tauriGAEvents=new Map;function _sendGaEvent(eventAct,category,label,count){if(Phoenix.isNativeApp){const key=`${eventAct}:${category}:${label}}`,existingEvent=tauriGAEvents.get(key);return existingEvent?void(existingEvent.count=(existingEvent.count||0)+count):void tauriGAEvents.set(key,{eventAct:eventAct,category:category,label:label,count:count})}gtag("event",eventAct,{event_category:category,event_label:label,value:count})}const TAURI_GA_EVENT_QUEUE_INTERVAL=3e3;function _sendQueuedTauriGAEvents(){_sendNativeGAEvent(brackets.config.googleAnalyticsIDDesktop,userID,Array.from(tauriGAEvents.values())),tauriGAEvents.clear()}function _getCampaignQuery(){const campaignParams=["utm_source","utm_medium","utm_campaign","utm_content","utm_term","utm_id"];try{const incoming=new URLSearchParams(window.location.search),allowed=new URLSearchParams;for(let i=0;i{cachedIsPowerUser=isPowerUserFn(),_setPowerUserPrefix()},ONE_DAY)),KernalModeTrust.EntitlementsManager&&KernalModeTrust.EntitlementsManager.on(KernalModeTrust.EntitlementsManager.EVENT_ENTITLEMENTS_CHANGED,_setPowerUserPrefix))}const ignoredGAEvents=["instantSearch"];function _sendToGoogleAnalytics(category,action,label,count){if(disabled||window.testEnvironment)return;action=action||"action",label||(label=action),count||(count=1);let eventAct=`${category=category||"category"}.${action}.${label}`;ignoredGAEvents.includes(action)||_sendGaEvent(eventAct,category,label,count)}function _sendToCoreAnalytics(category,action,label,count,value){disabled||window.testEnvironment||(category=category||"category",action=action||"action",label||(label=action),value?analytics.valueEvent(category,action,label,value,count||1):analytics.countEvent(category,action,label,count||1))}const AUDIT_TYPE_COUNT="count",AUDIT_TYPE_VALUE="val";function _logEventForAudit(eventType,eventCategory,eventSubCategory,val,type){let defaultVal={eventType:type,sum:0,count:0},key=`${eventType}.${eventCategory}.${eventSubCategory}`,newVal=loggedDataForAudit.get(key)||defaultVal;if(newVal.count=newVal.count+1,newVal.sum=newVal.sum+val,loggedDataForAudit.set(key,newVal),loggedDataForAudit.size>=MAX_AUDIT_ENTRIES){const NUM_ENTRIES_TO_DELETE=1e3;let keys;Array.from(loggedDataForAudit.keys()).slice(0,NUM_ENTRIES_TO_DELETE).forEach(k=>loggedDataForAudit.delete(k))}}function _countEvent(eventType,eventCategory,eventSubCategory,count=1){_logEventForAudit(eventType,eventCategory,eventSubCategory,count,AUDIT_TYPE_COUNT),_sendToGoogleAnalytics(eventType,eventCategory,eventSubCategory,count),_sendToCoreAnalytics(eventType,eventCategory,eventSubCategory,count)}function countEvent(eventType,eventCategory,eventSubCategory,count=1){powerUserPrefix&&_countEvent(`${powerUserPrefix}-${eventType}`,eventCategory,eventSubCategory,count),_countEvent(eventType,eventCategory,eventSubCategory,count)}function _valueEvent(eventType,eventCategory,eventSubCategory,value){_logEventForAudit(eventType,eventCategory,eventSubCategory,value,AUDIT_TYPE_VALUE),_sendToGoogleAnalytics(eventType,eventCategory,eventSubCategory,value),_sendToCoreAnalytics(eventType,eventCategory,eventSubCategory,1,value)}function valueEvent(eventType,eventCategory,eventSubCategory,value){powerUserPrefix&&_valueEvent(`${powerUserPrefix}-${eventType}`,eventCategory,eventSubCategory,value),_valueEvent(eventType,eventCategory,eventSubCategory,value)}const BATCH_FLUSH_MS=12e4,BATCH_KEY_SEPARATOR="|",_batchedCounts=new Map,_batchedValues=new Map;function countEventBatched(eventType,eventCategory,eventSubCategory){const key=eventType+BATCH_KEY_SEPARATOR+eventCategory+BATCH_KEY_SEPARATOR+eventSubCategory;_batchedCounts.set(key,(_batchedCounts.get(key)||0)+1)}function valueEventBatched(eventType,eventCategory,eventSubCategory,value){const key=eventType+BATCH_KEY_SEPARATOR+eventCategory+BATCH_KEY_SEPARATOR+eventSubCategory,entry=_batchedValues.get(key);entry?(entry.sum+=Number(value),entry.count++):_batchedValues.set(key,{sum:Number(value),count:1})}function _flushBatchedEvents(){for(const[key,count]of _batchedCounts){const parts=key.split(BATCH_KEY_SEPARATOR);countEvent(parts[0],parts[1],parts[2],count)}_batchedCounts.clear();for(const[key,entry]of _batchedValues){const parts=key.split(BATCH_KEY_SEPARATOR);valueEvent(parts[0],parts[1],parts[2],Math.round(entry.sum/entry.count))}_batchedValues.clear()}function setDisabled(shouldDisable){Phoenix._setHealthTrackingDisabled(shouldDisable),disabled=shouldDisable}function isDisabled(){return disabled}function getLoggedDataForAudit(){return loggedDataForAudit}function clearAuditData(){loggedDataForAudit.clear()}async function flushMetrics(){try{_flushBatchedEvents(),Phoenix.isNativeApp&&_sendQueuedTauriGAEvents()}catch(e){console.error("Error while flushMetrics: ",e)}}function logPerformanceTime(action,durationMs){valueEvent(EVENT_TYPE.PERFORMANCE,"ms",action,Number(durationMs))}function getRangeName(number){const ranges=[0,5,10,25,50,100,250,500,1e3,5e3,1e4];for(let i=0;i