import*as Common from"../common/common.js";import*as i18n from"../i18n/i18n.js";import*as SDK from"../sdk/sdk.js";import*as UI from"../ui/ui.js";import{ApplicationPanelTreeElement,ExpandableApplicationPanelTreeElement}from"./ApplicationPanelTreeElement.js";import{ServiceWorkerCacheView}from"./ServiceWorkerCacheViews.js";const UIStrings={cacheStorage:"Cache Storage",refreshCaches:"Refresh Caches",delete:"Delete"},str_=i18n.i18n.registerUIStrings("resources/ApplicationPanelCacheSection.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class ApplicationCacheManifestTreeElement extends ApplicationPanelTreeElement{constructor(e,t){super(e,new Common.ParsedURL.ParsedURL(t).displayName,!1),this.tooltip=t,this.manifestURL=t}get itemURL(){return"appcache://"+this.manifestURL}onselect(e){return super.onselect(e),this.resourcesPanel.showCategoryView(this.manifestURL,null),!1}}export class ServiceWorkerCacheTreeElement extends ExpandableApplicationPanelTreeElement{constructor(e){super(e,i18nString(UIStrings.cacheStorage),"CacheStorage");const t=UI.Icon.Icon.create("mediumicon-database","resource-tree-item");this.setLink("https://developers.google.com/web/tools/chrome-devtools/storage/cache?utm_source=devtools"),this.setLeadingIcons([t]),this.swCacheModel=null,this.swCacheTreeElements=new Set}initialize(e){if(this.swCacheTreeElements.clear(),this.swCacheModel=e,e)for(const t of e.caches())this.addCache(e,t);SDK.SDKModel.TargetManager.instance().addModelListener(SDK.ServiceWorkerCacheModel.ServiceWorkerCacheModel,SDK.ServiceWorkerCacheModel.Events.CacheAdded,this.cacheAdded,this),SDK.SDKModel.TargetManager.instance().addModelListener(SDK.ServiceWorkerCacheModel.ServiceWorkerCacheModel,SDK.ServiceWorkerCacheModel.Events.CacheRemoved,this.cacheRemoved,this)}onattach(){super.onattach(),this.listItemElement.addEventListener("contextmenu",this.handleContextMenuEvent.bind(this),!0)}handleContextMenuEvent(e){const t=new UI.ContextMenu.ContextMenu(e);t.defaultSection().appendItem(i18nString(UIStrings.refreshCaches),this.refreshCaches.bind(this)),t.show()}refreshCaches(){this.swCacheModel&&this.swCacheModel.refreshCacheNames()}cacheAdded(e){const t=e.data.cache,s=e.data.model;this.addCache(s,t)}addCache(e,t){const s=new SWCacheTreeElement(this.resourcesPanel,e,t);this.swCacheTreeElements.add(s),this.appendChild(s)}cacheRemoved(e){const t=e.data.cache,s=e.data.model,i=this.cacheTreeElement(s,t);i&&(this.removeChild(i),this.swCacheTreeElements.delete(i),this.setExpandable(this.childCount()>0))}cacheTreeElement(e,t){for(const s of this.swCacheTreeElements)if(s.hasModelAndCache(e,t))return s;return null}}export class SWCacheTreeElement extends ApplicationPanelTreeElement{constructor(e,t,s){super(e,s.cacheName+" - "+s.securityOrigin,!1),this.model=t,this.cache=s,this.view=null;const i=UI.Icon.Icon.create("mediumicon-table","resource-tree-item");this.setLeadingIcons([i])}get itemURL(){return"cache://"+this.cache.cacheId}onattach(){super.onattach(),this.listItemElement.addEventListener("contextmenu",this.handleContextMenuEvent.bind(this),!0)}handleContextMenuEvent(e){const t=new UI.ContextMenu.ContextMenu(e);t.defaultSection().appendItem(i18nString(UIStrings.delete),this.clearCache.bind(this)),t.show()}clearCache(){this.model.deleteCache(this.cache)}update(e){this.cache=e,this.view&&this.view.update(e)}onselect(e){return super.onselect(e),this.view||(this.view=new ServiceWorkerCacheView(this.model,this.cache)),this.showView(this.view),!1}hasModelAndCache(e,t){return this.cache.equals(t)&&this.model===e}}export class ApplicationCacheFrameTreeElement extends ApplicationPanelTreeElement{constructor(e,t,s){super(e._panel,"",!1),this.sidebar=e,this.frameId=t.id,this.manifestURL=s,this.refreshTitles(t);const i=UI.Icon.Icon.create("mediumicon-frame-top","navigator-folder-tree-item");this.setLeadingIcons([i])}get itemURL(){return"appcache://"+this.manifestURL+"/"+encodeURI(this.titleAsText())}refreshTitles(e){this.title=e.displayName()}frameNavigated(e){this.refreshTitles(e)}onselect(e){return super.onselect(e),this.sidebar._showApplicationCache(this.frameId),!1}}