import*as Common from"../common/common.js";import*as Host from"../host/host.js";import*as i18n from"../i18n/i18n.js";import{ToolbarButton}from"./Toolbar.js";const UIStrings={close:"Close",dockToRight:"Dock to right",dockToBottom:"Dock to bottom",dockToLeft:"Dock to left",undockIntoSeparateWindow:"Undock into separate window"},str_=i18n.i18n.registerUIStrings("ui/DockController.js",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);let dockControllerInstance;export class DockController extends Common.ObjectWrapper.ObjectWrapper{constructor(t){if(super(),this._canDock=t,this._closeButton=new ToolbarButton(i18nString(UIStrings.close),"largeicon-delete"),this._closeButton.addEventListener(ToolbarButton.Events.Click,Host.InspectorFrontendHost.InspectorFrontendHostInstance.closeWindow.bind(Host.InspectorFrontendHost.InspectorFrontendHostInstance)),this._currentDockStateSetting=Common.Settings.Settings.instance().moduleSetting("currentDockState"),this._lastDockStateSetting=Common.Settings.Settings.instance().createSetting("lastDockState","bottom"),this._dockSide,!t)return this._dockSide=State.Undocked,void this._closeButton.setVisible(!1);this._currentDockStateSetting.addChangeListener(this._dockSideChanged,this),-1===states.indexOf(this._currentDockStateSetting.get())&&this._currentDockStateSetting.set("right"),-1===states.indexOf(this._lastDockStateSetting.get())&&this._currentDockStateSetting.set("bottom")}static instance(t={forceNew:null,canDock:!1}){const{forceNew:e,canDock:o}=t;return dockControllerInstance&&!e||(dockControllerInstance=new DockController(o)),dockControllerInstance}initialize(){this._canDock&&(this._titles=[i18nString(UIStrings.dockToRight),i18nString(UIStrings.dockToBottom),i18nString(UIStrings.dockToLeft),i18nString(UIStrings.undockIntoSeparateWindow)],this._dockSideChanged())}_dockSideChanged(){this.setDockSide(this._currentDockStateSetting.get())}dockSide(){return this._dockSide}canDock(){return this._canDock}isVertical(){return this._dockSide===State.DockedToRight||this._dockSide===State.DockedToLeft}setDockSide(t){if(-1===states.indexOf(t)&&(t=states[0]),this._dockSide===t)return;this._dockSide&&this._lastDockStateSetting.set(this._dockSide),this._savedFocus=document.deepActiveElement();const e={from:this._dockSide,to:t};this.dispatchEventToListeners(Events.BeforeDockSideChanged,e),console.timeStamp("DockController.setIsDocked"),this._dockSide=t,this._currentDockStateSetting.set(t),Host.InspectorFrontendHost.InspectorFrontendHostInstance.setIsDocked(t!==State.Undocked,this._setIsDockedResponse.bind(this,e)),this._closeButton.setVisible(this._dockSide!==State.Undocked),this.dispatchEventToListeners(Events.DockSideChanged,e)}_setIsDockedResponse(t){this.dispatchEventToListeners(Events.AfterDockSideChanged,t),this._savedFocus&&(this._savedFocus.focus(),this._savedFocus=null)}_toggleDockSide(){if(this._lastDockStateSetting.get()===this._currentDockStateSetting.get()){const t=states.indexOf(this._currentDockStateSetting.get())||0;this._lastDockStateSetting.set(states[(t+1)%states.length])}this.setDockSide(this._lastDockStateSetting.get())}}export const State={DockedToBottom:"bottom",DockedToRight:"right",DockedToLeft:"left",Undocked:"undocked"};const states=[State.DockedToRight,State.DockedToBottom,State.DockedToLeft,State.Undocked];export const Events={BeforeDockSideChanged:Symbol("BeforeDockSideChanged"),DockSideChanged:Symbol("DockSideChanged"),AfterDockSideChanged:Symbol("AfterDockSideChanged")};let toggleDockActionDelegateInstance,closeButtonProviderInstance;export class ToggleDockActionDelegate{static instance(t={forceNew:null}){const{forceNew:e}=t;return toggleDockActionDelegateInstance&&!e||(toggleDockActionDelegateInstance=new ToggleDockActionDelegate),toggleDockActionDelegateInstance}handleAction(t,e){return DockController.instance()._toggleDockSide(),!0}}export class CloseButtonProvider{static instance(t={forceNew:null}){const{forceNew:e}=t;return closeButtonProviderInstance&&!e||(closeButtonProviderInstance=new CloseButtonProvider),closeButtonProviderInstance}item(){return DockController.instance()._closeButton}}