import*as i18n from"../i18n/i18n.js";import*as SDK from"../sdk/sdk.js";import*as UI from"../ui/ui.js";const UIStrings={breakpointHit:"breakpoint hit"},str_=i18n.i18n.registerUIStrings("browser_debugger/CategorizedBreakpointsSidebarPane.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class CategorizedBreakpointsSidebarPane extends UI.Widget.VBox{constructor(e,t,i,s){super(!0),this._categoriesTreeOutline=new UI.TreeOutline.TreeOutlineInShadow,this._categoriesTreeOutline.registerRequiredCSS("browser_debugger/categorizedBreakpointsSidebarPane.css",{enableLegacyPatching:!0}),this._categoriesTreeOutline.setShowSelectionOnKeyboardFocus(!0),this.contentElement.appendChild(this._categoriesTreeOutline.element),this._viewId=i,this._detailsPausedReason=s,this._categories=new Map;for(const n of e)this._categories.has(n)||this._createCategory(n);if(e.length>0){const t=this._categories.get(e[0]);t&&t.element.select()}this._breakpoints=new Map;for(const n of t)this._createBreakpoint(n);SDK.SDKModel.TargetManager.instance().addModelListener(SDK.DebuggerModel.DebuggerModel,SDK.DebuggerModel.Events.DebuggerPaused,this._update,this),SDK.SDKModel.TargetManager.instance().addModelListener(SDK.DebuggerModel.DebuggerModel,SDK.DebuggerModel.Events.DebuggerResumed,this._update,this),UI.Context.Context.instance().addFlavorChangeListener(SDK.SDKModel.Target,this._update,this)}focus(){this._categoriesTreeOutline.forceSelect()}_createCategory(e){const t=UI.UIUtils.CheckboxLabel.create(e);t.checkboxElement.addEventListener("click",this._categoryCheckboxClicked.bind(this,e),!0),t.checkboxElement.tabIndex=-1;const i=new UI.TreeOutline.TreeElement(t);i.listItemElement.addEventListener("keydown",(t=>{if(" "===t.key){const i=this._categories.get(e);i&&i.checkbox.click(),t.consume(!0)}})),t.checkboxElement.addEventListener("focus",(()=>i.listItemElement.focus())),UI.ARIAUtils.setChecked(i.listItemElement,!1),this._categoriesTreeOutline.appendChild(i),this._categories.set(e,{element:i,checkbox:t.checkboxElement})}_createBreakpoint(e){const t=UI.UIUtils.CheckboxLabel.create(e.title());t.classList.add("source-code"),t.checkboxElement.addEventListener("click",this._breakpointCheckboxClicked.bind(this,e),!0),t.checkboxElement.tabIndex=-1;const i=new UI.TreeOutline.TreeElement(t);i.listItemElement.addEventListener("keydown",(t=>{if(" "===t.key){const i=this._breakpoints.get(e);i&&i.checkbox.click(),t.consume(!0)}})),t.checkboxElement.addEventListener("focus",(()=>i.listItemElement.focus())),UI.ARIAUtils.setChecked(i.listItemElement,!1),i.listItemElement.createChild("div","breakpoint-hit-marker");const s=this._categories.get(e.category());s&&s.element.appendChild(i),this._breakpoints.set(e,{element:i,checkbox:t.checkboxElement})}_getBreakpointFromPausedDetails(e){return null}_update(){const e=UI.Context.Context.instance().flavor(SDK.SDKModel.Target),t=e?e.model(SDK.DebuggerModel.DebuggerModel):null,i=t?t.debuggerPausedDetails():null;if(!i||i.reason!==this._detailsPausedReason||!i.auxData)return void(this._highlightedElement&&(UI.ARIAUtils.setDescription(this._highlightedElement,""),this._highlightedElement.classList.remove("breakpoint-hit"),delete this._highlightedElement));const s=this._getBreakpointFromPausedDetails(i);if(!s)return;UI.ViewManager.ViewManager.instance().showView(this._viewId);const n=this._categories.get(s.category());n&&n.element.expand();const o=this._breakpoints.get(s);o&&(this._highlightedElement=o.element.listItemElement,UI.ARIAUtils.setDescription(this._highlightedElement,i18nString(UIStrings.breakpointHit)),this._highlightedElement.classList.add("breakpoint-hit"))}_categoryCheckboxClicked(e){const t=this._categories.get(e);if(!t)return;const i=t.checkbox.checked;UI.ARIAUtils.setChecked(t.element.listItemElement,i);for(const s of this._breakpoints.keys())if(s.category()===e){const e=this._breakpoints.get(s);e&&(e.checkbox.checked=i,this._toggleBreakpoint(s,i))}}_toggleBreakpoint(e,t){e.setEnabled(t)}_breakpointCheckboxClicked(e){const t=this._breakpoints.get(e);if(!t)return;this._toggleBreakpoint(e,t.checkbox.checked),UI.ARIAUtils.setChecked(t.element.listItemElement,t.checkbox.checked);let i=!1,s=!1;for(const o of this._breakpoints.keys())o.category()===e.category()&&(o.enabled()?i=!0:s=!0);const n=this._categories.get(e.category());n&&(n.checkbox.checked=i,n.checkbox.indeterminate=i&&s,n.checkbox.indeterminate?UI.ARIAUtils.setCheckboxAsIndeterminate(n.element.listItemElement):UI.ARIAUtils.setChecked(n.element.listItemElement,i))}}