/*************************************************************************
* 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{floodgate as t}from"./floodgate.js";import{dcLocalStorage as e,dcSessionStorage as a}from"../common/local-storage.js";import{loggingApi as s}from"../common/loggingApi.js";import{safeJsonParse as o}from"../common/util.js";import{communicate as r}from"./communicate.js";import{Proxy as i}from"./proxy.js";const n=1024,g=1048576;let l=null;class y{constructor(){this.proxy=i.proxy.bind(this)}calculateSize(t){try{return null==t?0:"string"==typeof t?t.length:new Blob([JSON.stringify(t)]).size}catch(t){return 0}}calculateStorageStats(t){const e={totalSize:0,totalKeys:0,keys:[],timestamp:Date.now()};return t?(Object.keys(t).forEach(a=>{const s=t[a],o=this.calculateSize(s);e.keys.push({key:a,size:o}),e.totalSize+=o}),e.totalKeys=e.keys.length,e.keys.sort((t,e)=>e.size-t.size),e):e}formatUsageDetails(t){if(!t||"object"!=typeof t)return"";return Object.entries(t).filter(([,t])=>t>0).map(([t,e])=>`${t}: ${this.formatSize(e)}`).join(" | ")}async getStorageEstimation(){const t={quota:0,usage:0,usagePercent:0,capacityInfo:"",usageDetailsString:"",usageDetailsBreakdown:[]};try{if(!navigator?.storage?.estimate)return t;const e=await navigator.storage.estimate(),a=e.quota||0,s=e.usage||0,o=a>0?parseFloat((s/a*100).toFixed(2)):0,r=(a/(n*g)).toFixed(2),i=[];e.usageDetails&&"object"==typeof e.usageDetails&&Object.entries(e.usageDetails).forEach(([t,e])=>{e>0&&i.push({type:t,sizeBytes:e,sizeFormatted:this.formatSize(e)})}),t.quota=a,t.usage=s,t.usagePercent=o,t.capacityInfo=`Usage: ${this.formatSize(s)} / ${r} GB (${o}%)`,t.usageDetailsString=this.formatUsageDetails(e.usageDetails),t.usageDetailsBreakdown=JSON.stringify(i)}catch(t){}return t}getTopKeys(t,e=20){const a=t.slice(0,e);return{topKeysString:a.map((t,e)=>`${e+1}. ${t.key}: ${this.formatSize(t.size)}`).join(" | "),topKeys:a}}async analyzeStorage(t,e={}){const{topKeysLimit:a=20,includeAllKeys:s=!1}=e,o=this.calculateStorageStats(t),r=await this.getStorageEstimation(),i=this.getTopKeys(o.keys,a);return{totalKeysSizeBytes:o.totalSize,totalKeys:o.totalKeys,storageQuotaBytes:r.quota,storageUsageBytes:r.usage,storageUsagePercent:r.usagePercent,totalKeysSizeFormatted:this.formatSize(o.totalSize),storageCapacityInfo:r.capacityInfo,storageUsageDetails:r.usageDetailsString,topKeysString:i.topKeysString,usageDetailsBreakdown:r.usageDetailsBreakdown,allKeys:s?JSON.stringify(o.keys):""}}formatSize(t){return t<n?`${t} B`:t<g?`${(t/n).toFixed(2)} KB`:`${(t/g).toFixed(2)} MB`}async handleQuotaError(r){try{if(!await t.hasFlag("dc-cv-storage-analyzer"))return;const{storageType:i,errorMessage:n="Unknown error",key:g="unknown"}=r,l="local"===i?e:a;await l.init();const y=t.getFeatureMeta("dc-cv-storage-analyzer"),c=o(y,{topKeysLimit:20,includeAllKeys:!1}),u=await this.analyzeStorage(l.storage,c),m=chrome.runtime.getManifest().version,p=e.getItem("anonUserUUID");s.error({storageType:i,errorMessage:n,key:g,totalKeysSizeBytes:u.totalKeysSizeBytes,totalKeys:u.totalKeys,storageQuotaBytes:u.storageQuotaBytes,storageUsageBytes:u.storageUsageBytes,storageUsagePercent:u.storageUsagePercent,totalKeysSizeFormatted:u.totalKeysSizeFormatted,storageCapacityInfo:u.storageCapacityInfo,storageUsageDetailsString:u.storageUsageDetails,topKeysString:u.topKeysString,allKeys:u.allKeys,extensionVersion:m,userUUID:p})}catch(t){}}}l||(l=new y),r.registerHandlers({"log-quota-error":l.proxy(l.handleQuotaError)});export const storageAnalyzer=l;