//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// TypeScript support for Blazor-WASM application | Author: Sergei Y.Kitaev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Service dialog frame support for message-box etc (instance class):
class ServiceDialogFrame {
   private static CR: ServiceDialogFrame|null

   private DialogDIV: HTMLDivElement
   private VeilDIV: HTMLDivElement
   private SpaceDIV: HTMLDivElement

   private LastFocusedElem: HTMLElement|null

   constructor() {
      this.DialogDIV = HById.ElementOf<HTMLDivElement>('service_dialog_DialogDIV','DIV')
      this.VeilDIV   = HById.ElementOf<HTMLDivElement>('service_dialog_VeilDIV'  ,'DIV')
      this.SpaceDIV  = HById.ElementOf<HTMLDivElement>('service_dialog_SpaceDIV' ,'DIV')
   }

   ContainsFocus(): boolean {
      return HElement.ContainsFocus( this.DialogDIV )
   }

   ShowDiv(divElement: HTMLDivElement) {
      let style = divElement.style
      style.left = style.top = '0'
      style.right = Magistral.GetPageContentWidthDiff() + 'px'
      style.bottom = ''
      style.height = '100%'
      style.overflowY = 'hidden'
      style.visibility = 'visible'
   }
   CollapseDiv(divElement: HTMLDivElement) {
      divElement.style.visibility = 'collapse';
   }

   Window_OnResize() {
      var fullSpaceRect = this.SpaceDIV.getBoundingClientRect()
      var style = this.DialogDIV.style
      style.maxWidth = fullSpaceRect.width + 'px'
      style.maxHeight = fullSpaceRect.height + 'px'
      if (MessageBox.IsActive())
         MessageBox.Handle_Window_OnResize()
   }
   HandleEarly_Window_OnFocus() {
   }
   HandleLate_Window_OnFocus() {
      if (MessageBox.IsActive()) {
         MessageBox.Handle_Window_OnFocus()
         return
      }
      let activeElement = HDoc.FocusedElement()
      if (activeElement && this.DialogDIV.contains(activeElement))
         this.LastFocusedElem = activeElement
      else
         if (this.LastFocusedElem)
            this.LastFocusedElem.focus()
   }

   RecoverFocus() {
      if (MessageBox.IsActive()) {
         MessageBox.RecoverFocus()
         return
      }
      if (this.LastFocusedElem)
         this.LastFocusedElem.focus()
   }

   Open() {
      this.ShowDiv(this.VeilDIV)
      this.ShowDiv(this.SpaceDIV)
      this.Window_OnResize()
   }
   Close() {
      this.CollapseDiv(this.SpaceDIV)
      this.CollapseDiv(this.VeilDIV)
      this.LastFocusedElem = null
   }

   static IsOpen(): boolean {
      return ServiceDialogFrame.CR != null
   }
   static Current(): ServiceDialogFrame {
      let curr = ServiceDialogFrame.CR
      if (!curr)
         throw new Error('Dialog is not open (\"ServiceDialogFrame\").')
      return curr
   }

   static DialogSpaceWidth(): number {
      return ServiceDialogFrame.Current().SpaceDIV.getBoundingClientRect().width
   }

   static Window_OnResize() {
      let curr = ServiceDialogFrame.CR
      if (curr!=null) curr.Window_OnResize()
   }
   static HandleEarly_Window_OnFocus() {
      let curr = ServiceDialogFrame.CR
      if (curr!=null) curr.HandleEarly_Window_OnFocus()
   }
   static HandleLate_Window_OnFocus() {
      let curr = ServiceDialogFrame.CR
      if (curr!=null) curr.HandleLate_Window_OnFocus()
   }

   static RecoverFocus() {
      let curr = ServiceDialogFrame.CR
      if (curr!=null) curr.RecoverFocus()
   }

   static ContainsFocus(): boolean {
      let curr = ServiceDialogFrame.CR
      return curr ? curr.ContainsFocus() : false
   }
   static LastFocused(): HTMLElement|null {
      let curr = ServiceDialogFrame.CR
      return curr ? curr.LastFocusedElem : null
   }

   static Open() {
      ServiceDialogFrame.Close()
      let inst = new ServiceDialogFrame()
      inst.Open()
      ServiceDialogFrame.CR = inst
   }
   static Close() {
      let curr = ServiceDialogFrame.CR
      if (curr) {
         curr.Close()
         ServiceDialogFrame.CR = null
      }
      MessageBox.Reset()
   }
} // ServiceDialogFrame

//==================================================================================================
// MODAL DIALOG FRAME SUPPORT — INITIALIZATION:
//--------------------------------------------------------------------------------------------------

window.addEventListener('resize', ServiceDialogFrame.Window_OnResize, true)
window.addEventListener('click', ServiceDialogFrame.RecoverFocus, true)
window.addEventListener('contextmenu', ServiceDialogFrame.RecoverFocus, true)
window.addEventListener('auxclick', ServiceDialogFrame.RecoverFocus, true)

//==================================================================================================
// MODAL DIALOG FRAME SUPPORT — FUNCTIONS:
//--------------------------------------------------------------------------------------------------

function ServiceDialogFrame_Open() {
   ServiceDialogFrame.Open()
}
function ServiceDialogFrame_Close() {
   ServiceDialogFrame.Close()
}

//==================================================================================================
