import*as ARIAUtils from"./ARIAUtils.js";import{GlassPane,PointerEventsBehavior}from"./GlassPane.js";import{InspectorView}from"./InspectorView.js";import{KeyboardShortcut,Keys}from"./KeyboardShortcut.js";import{WidgetFocusRestorer}from"./Widget.js";export class Dialog extends GlassPane{constructor(){super(),this.registerRequiredCSS("ui/dialog.css",{enableLegacyPatching:!1}),this.contentElement.tabIndex=0,this.contentElement.addEventListener("focus",(()=>this.widget().focus()),!1),this.widget().setDefaultFocusedElement(this.contentElement),this.setPointerEventsBehavior(PointerEventsBehavior.BlockedByGlassPane),this.setOutsideClickCallback((e=>{this.hide(),e.consume(!0)})),ARIAUtils.markAsModalDialog(this.contentElement),this._tabIndexBehavior=OutsideTabIndexBehavior.DisableAllOutsideTabIndex,this._tabIndexMap=new Map,this._focusRestorer=null,this._closeOnEscape=!0,this._targetDocument,this._targetDocumentKeyDownHandler=this._onKeyDown.bind(this),this._escapeKeyCallback=null}static hasInstance(){return Boolean(Dialog._instance)}show(e){const t=e instanceof Document?e:(e||InspectorView.instance().element).ownerDocument;this._targetDocument=t,this._targetDocument.addEventListener("keydown",this._targetDocumentKeyDownHandler,!0),Dialog._instance&&Dialog._instance.hide(),Dialog._instance=this,this._disableTabIndexOnElements(t),super.show(t),this._focusRestorer=new WidgetFocusRestorer(this.widget())}hide(){this._focusRestorer&&this._focusRestorer.restore(),super.hide(),this._targetDocument&&this._targetDocument.removeEventListener("keydown",this._targetDocumentKeyDownHandler,!0),this._restoreTabIndexOnElements(),this.dispatchEventToListeners("hidden"),Dialog._instance=null}setCloseOnEscape(e){this._closeOnEscape=e}setEscapeKeyCallback(e){this._escapeKeyCallback=e}addCloseButton(){const e=this.contentElement.createChild("div","dialog-close-button","dt-close-button");e.gray=!0,e.addEventListener("click",(()=>this.hide()),!1)}setOutsideTabIndexBehavior(e){this._tabIndexBehavior=e}_disableTabIndexOnElements(e){if(this._tabIndexBehavior===OutsideTabIndexBehavior.PreserveTabIndex)return;let t=null;this._tabIndexBehavior===OutsideTabIndexBehavior.PreserveMainViewTabIndex&&(t=this._getMainWidgetTabIndexElements(InspectorView.instance().ownerSplit())),this._tabIndexMap.clear();let s=e;for(;s;s=s.traverseNextNode(e))if(s instanceof HTMLElement){const e=s,n=e.tabIndex;!(n>=0)||t&&t.has(e)||(this._tabIndexMap.set(e,n),e.tabIndex=-1)}}_getMainWidgetTabIndexElements(e){const t=new Set;if(!e)return t;const s=e.mainWidget();if(!s||!s.element)return t;let n=s.element;for(;n;n=n.traverseNextNode(s.element)){if(!(n instanceof HTMLElement))continue;const e=n;e.tabIndex<0||t.add(e)}return t}_restoreTabIndexOnElements(){for(const e of this._tabIndexMap.keys())e.tabIndex=this._tabIndexMap.get(e);this._tabIndexMap.clear()}_onKeyDown(e){if(e.keyCode===Keys.Esc.code&&KeyboardShortcut.hasNoModifiers(e)){if(this._escapeKeyCallback&&this._escapeKeyCallback(e),e.handled)return;this._closeOnEscape&&(e.consume(!0),this.hide())}}}Dialog._instance=null;export const OutsideTabIndexBehavior={DisableAllOutsideTabIndex:Symbol("DisableAllTabIndex"),PreserveMainViewTabIndex:Symbol("PreserveMainViewTabIndex"),PreserveTabIndex:Symbol("PreserveTabIndex")};