//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// TypeScript support for Blazor-WASM application | Author: Sergei Y.Kitaev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Instance base class for SPA-page:
class PageBase {
   private static _CurrentPage_: PageBase|null
   private SavedFocus: HTMLElement|null
   private WithSelectInputFocusRestoring: boolean = false
   private SavedScrollLeft: number|null
   private SavedScrollTop: number|null

   constructor () {
      PageBase._CurrentPage_ = this
   }

   SaveFocus(withSelectRestoring: boolean=false): boolean {
      return this.SaveFocusExplicit( HDoc.FocusedElement() )
   }
   SaveFocusExplicit( element: HTMLElement|null,
                      withSelectRestoring: boolean=false ): boolean {
      this.SavedFocus = element
      this.WithSelectInputFocusRestoring = HOps.BoolDefaultFalse(withSelectRestoring)
      return !!this.SavedFocus
   }
   RestoreFocus(): boolean {
      let element = this.SavedFocus
      if (!element)
         return false
      if (this.WithSelectInputFocusRestoring)
         HElement.DeferredFocusSelect( element )
      else
         HElement.DeferredFocus( element )
      this.ResetSavedFocus()
      return true
   }
   ResetSavedFocus(): boolean {
      let rslt = this.SavedFocus != null
      this.SavedFocus = null
      this.WithSelectInputFocusRestoring = false
      return rslt
   }

   SaveScrolling(): boolean {
      this.SavedScrollLeft = Magistral.GetScrollLeft()
      this.SavedScrollTop = Magistral.GetScrollTop()
      return true
   }
   RestoreScrolling(): boolean {
      let scrollLeft = this.SavedScrollLeft
      let scrollTop = this.SavedScrollTop
      if (scrollLeft==null && scrollTop==null)
         return false
      Page.DeferredScrollTo(scrollLeft, scrollTop)
      this.ResetSavedScrolling()
      return true
   }
   ResetSavedScrolling(): boolean {
      this.SavedScrollLeft = null
      this.SavedScrollTop = null
      return true
   }

   static HasCurrentPage(): boolean {
      return !!PageBase.CurrentPage()
   }
   static CurrentPage(): PageBase|null {
      return PageBase._CurrentPage_
   }
   static ResetCurrentPage() {
      PageBase._CurrentPage_ = null
   }
} // PageBase

//==================================================================================================
// PageBase — INITIALIZATION:
//--------------------------------------------------------------------------------------------------

//==================================================================================================
// PageBase — FUNCTIONS:
//--------------------------------------------------------------------------------------------------

function PageBase_ResetCurrentPage() {
   PageBase.ResetCurrentPage()
}

function PageBase_SaveFocus(withSelectRestoring: boolean=false): boolean {
   let curr = PageBase.CurrentPage()
   return curr ? curr.SaveFocus(withSelectRestoring) : false
}
function PageBase_SaveFocusExplicit( element: HTMLElement|null,
                                     withSelectRestoring: boolean=false ): boolean {
   let curr = PageBase.CurrentPage()
   return curr ? curr.SaveFocusExplicit(element, withSelectRestoring) : false
}
function PageBase_RestoreFocus(): boolean {
   let curr = PageBase.CurrentPage()
   return curr ? curr.RestoreFocus() : false
}
function PageBase_ResetSavedFocus(): boolean {
   let curr = PageBase.CurrentPage()
   return curr ? curr.ResetSavedFocus() : false
}

function PageBase_SaveScrolling(): boolean {
   let curr = PageBase.CurrentPage()
   return curr ? curr.SaveScrolling() : false
}
function PageBase_RestoreScrolling(): boolean {
   let curr = PageBase.CurrentPage()
   return curr ? curr.RestoreScrolling() : false
}
function PageBase_ResetSavedScrolling(): boolean {
   let curr = PageBase.CurrentPage()
   return curr ? curr.ResetSavedScrolling() : false
}

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