//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// TypeScript support for Blazor-WASM application | Author: Sergei Y.Kitaev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Fatal breakdown functions for access from WASM (static class):
class FatalBreakdown
{
   private static _WasBreakdownInvoked_: boolean = false

   static ShowWasmErrorReloadSPA(message: string, traceError: boolean=true) {
      let errorText = HOps.NullSubst(message, 'Undefined error in SPA.')
      traceError = HOps.BoolDefaultTrue( traceError )
      if (traceError)
         console.error(errorText)
      let reloadPage = page_loaded && App.IsReleaseConfiguration()
      let leavePageForDebug = !reloadPage
      errorText += leavePageForDebug ?
         '\n\nFurther app.functioning is incorrect; reload the page maually.'+
         '\n(See error details in browser debug-console.)'
         : '\n\nWeb-application will be reloaded (SPA).'
      Breakdown.SetUseDefaultErrorReporting()
      alert(errorText)
      if (reloadPage)
         window.location.href = "" // reload from root
   }

   static BlockSPA() {
      AFXUI.Invoke('wasm_FatalBreakdown_BlockSPA')
   }

   static HandlePageError( message: string, url: string, line: number, col: number,
                           errorObj ): boolean {
      if (FatalBreakdown._WasBreakdownInvoked_)
         return false // do not filter default error output
      FatalBreakdown._WasBreakdownInvoked_ = true
      let errorText = message ?
         'Error on web-page.' :
         'Undefined error on web-page.'
      if (url)
         errorText += '\nURL: '+url
/*
      if (line)
         errorText += '\nLine: '+line
*/
      let errorObjectAdded = false
      if (errorObj)
      {
         try {
            errorText += '\n\n'+errorObj
            errorObjectAdded = true
         } catch {}
      }
      if (message && !errorObjectAdded)
         errorText += '\n\n'+message
      FatalBreakdown.BlockSPA()
      FatalBreakdown.ShowWasmErrorReloadSPA(errorText)
      return true // filter default error output (allready printed in "ShowWasmErrorReloadSPA")
   }
} // FatalBreakdown

//==================================================================================================
// FATAL BREAKDOWN — FUNCTIONS:
//--------------------------------------------------------------------------------------------------

function FatalBreakdown_ShowWasmErrorReloadSPA(message: string) {
   FatalBreakdown.ShowWasmErrorReloadSPA(message, false)
}

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