import*as Common from"../common/common.js";import*as SDK from"../sdk/sdk.js";import*as SourceFrame from"../source_frame/source_frame.js";import*as UI from"../ui/ui.js";import{ApplicationPanelSidebar,StorageCategoryView}from"./ApplicationPanelSidebar.js";import{CookieItemsView}from"./CookieItemsView.js";import{DatabaseQueryView}from"./DatabaseQueryView.js";import{DatabaseTableView}from"./DatabaseTableView.js";import{DOMStorageItemsView}from"./DOMStorageItemsView.js";import{StorageItemsView}from"./StorageItemsView.js";let resourcesPanelInstance,resourceRevealerInstance,cookieReferenceRevealerInstance;export class ResourcesPanel extends UI.Panel.PanelWithSidebar{constructor(){super("resources"),this.registerRequiredCSS("resources/resourcesPanel.css",{enableLegacyPatching:!1}),this._resourcesLastSelectedItemSetting=Common.Settings.Settings.instance().createSetting("resourcesLastSelectedElementPath",[]),this.visibleView=null,this._pendingViewPromise=null,this._categoryView=null;const e=new UI.Widget.VBox;this.storageViews=e.element.createChild("div","vbox flex-auto"),this._storageViewToolbar=new UI.Toolbar.Toolbar("resources-toolbar",e.element),this.splitWidget().setMainWidget(e),this._domStorageView=null,this._cookieView=null,this._emptyWidget=null,this._sidebar=new ApplicationPanelSidebar(this),this._sidebar.show(this.panelSidebarElement())}static instance(e={forceNew:null}){const{forceNew:s}=e;return resourcesPanelInstance&&!s||(resourcesPanelInstance=new ResourcesPanel),resourcesPanelInstance}static _instance(){return ResourcesPanel.instance()}static _shouldCloseOnReset(e){return[SourceFrame.ResourceSourceFrame.ResourceSourceFrame,SourceFrame.ImageView.ImageView,SourceFrame.FontView.FontView,StorageItemsView,DatabaseQueryView,DatabaseTableView].some((s=>e instanceof s))}focus(){this._sidebar.focus()}lastSelectedItemPath(){return this._resourcesLastSelectedItemSetting.get()}setLastSelectedItemPath(e){this._resourcesLastSelectedItemSetting.set(e)}resetView(){this.visibleView&&ResourcesPanel._shouldCloseOnReset(this.visibleView)&&this.showView(null)}showView(e){this._pendingViewPromise=null,this.visibleView!==e&&(this.visibleView&&this.visibleView.detach(),e&&e.show(this.storageViews),this.visibleView=e,this._storageViewToolbar.removeToolbarItems(),this._storageViewToolbar.element.classList.toggle("hidden",!0),e instanceof UI.View.SimpleView&&e.toolbarItems().then((e=>{e.map((e=>this._storageViewToolbar.appendToolbarItem(e))),this._storageViewToolbar.element.classList.toggle("hidden",!e.length)})))}async scheduleShowView(e){this._pendingViewPromise=e;const s=await e;return this._pendingViewPromise!==e?null:(this.showView(s),s)}showCategoryView(e,s){this._categoryView||(this._categoryView=new StorageCategoryView),this._categoryView.setText(e),this._categoryView.setLink(s),this.showView(this._categoryView)}showDOMStorage(e){e&&(this._domStorageView?this._domStorageView.setStorage(e):this._domStorageView=new DOMStorageItemsView(e),this.showView(this._domStorageView))}showCookies(e,s){const t=e.model(SDK.CookieModel.CookieModel);t&&(this._cookieView?this._cookieView.setCookiesDomain(t,s):this._cookieView=new CookieItemsView(t,s),this.showView(this._cookieView))}clearCookies(e,s){const t=e.model(SDK.CookieModel.CookieModel);t&&t.clear(s).then((()=>{this._cookieView&&this._cookieView.refreshItems()}))}}export class ResourceRevealer{static instance(e={forceNew:null}){const{forceNew:s}=e;return resourceRevealerInstance&&!s||(resourceRevealerInstance=new ResourceRevealer),resourceRevealerInstance}async reveal(e){if(!(e instanceof SDK.Resource.Resource))return Promise.reject(new Error("Internal error: not a resource"));const s=ResourcesPanel._instance()._sidebar;await UI.ViewManager.ViewManager.instance().showView("resources"),await s.showResource(e)}}export class CookieReferenceRevealer{static instance(e={forceNew:null}){const{forceNew:s}=e;return cookieReferenceRevealerInstance&&!s||(cookieReferenceRevealerInstance=new CookieReferenceRevealer),cookieReferenceRevealerInstance}async reveal(e){if(!(e instanceof SDK.Cookie.CookieReference))throw new Error("Internal error: not a cookie reference");const s=ResourcesPanel._instance()._sidebar;await UI.ViewManager.ViewManager.instance().showView("resources"),await s.cookieListTreeElement.select();const t=e.contextUrl();t&&await this._revealByDomain(s,t)||this._revealByDomain(s,e.domain())}async _revealByDomain(e,s){const t=e.cookieListTreeElement.children().find((e=>e.cookieDomain().endsWith(s)));return!!t&&(await t.revealAndSelect(),!0)}}