//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// TypeScript support for Blazor-WASM application | Author: Sergei Y.Kitaev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Breakdown functions for unhandled error handling (static class):
class Breakdown
{
   private static _UseDefaultErrorReporting_: boolean = false
   private static _IsInErrorHandling_: boolean = false
   private static _DamagedBehaviour_: boolean = false

   static SetUseDefaultErrorReporting() {
      Breakdown._UseDefaultErrorReporting_ = true
   }

   static IgnoreErrorReportingWithDialog(): boolean {
      return Breakdown._UseDefaultErrorReporting_ || Breakdown._IsInErrorHandling_
   }

   static HandlePageError( message: string, url: string, line: number, col: number,
                           errorObj ): boolean {
      if (Breakdown.IgnoreErrorReportingWithDialog())
         return false // do not filter default error output
      if (AFXUI.InvokeReturn('wasm_Breakdown_UseFatalBreakdownInstead'))
         return FatalBreakdown.HandlePageError(message, url, line, col, errorObj)
      Breakdown._IsInErrorHandling_ = 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
      Breakdown.ReportError(errorText)
      AFXUI.Invoke_1('wasm_Breakdown_NavigateToErrorPageFromScripError', errorText)
      return true // filter default error output (allready printed in "ReportError")
   }

   static ShowWasmError(message: string, reloadCurrentPage: boolean) {
      if (!Breakdown.IgnoreErrorReportingWithDialog())
         Breakdown.ReportError(message, false, reloadCurrentPage)
   }

   private static ReportError( message: string,
                               traceError: boolean=true,
                               reloadCurrentPage: boolean=false ) {
      Breakdown._IsInErrorHandling_ = true
      if (!message)
         message = 'Undefined error in SPA.'
      if (traceError)
         console.error(message)
      let tryReloadPage = reloadCurrentPage && !Breakdown._DamagedBehaviour_
      let errorText = message
      errorText += tryReloadPage ?
         '\n\nSPA-app.page will be reloaded.' :
         '\n\nYou will be directed to error description page.'
      if (reloadCurrentPage)
         errorText +=
            '\nIf app.page is found blocked, use "navigation-back" (or "navigation-forward") '+
            'browser-commands to switch to anothed page of this SPA.'
      errorText +=
         '\n(See error details in browser debug-console.)'
      alert(errorText)
      if (tryReloadPage)
      try {
         Magistral.InitiateReloadPage()
      } catch(e) {
         Breakdown._DamagedBehaviour_ =true
         try {
            FatalBreakdown.BlockSPA()
         } catch (x){
            console.error('Can not block WASM-functioning from JS:\n'+x.message)
         }
         errorText =
            'Can not initiate app.page-reloading:'+
            e.message
         console.error(errorText)
         let reloadPage = page_loaded && App.IsReleaseConfiguration()
         let leavePageForDebug = !reloadPage
         errorText += leavePageForDebug ?
            '\nSee error details in browser debug console.'+
            '\n(Further app.functioning is incorrect; reload the page maually.)'
            : '\n\nWeb-application will be reloaded (SPA).'
         alert(errorText)
         if (reloadPage)
            window.location.href = "" // reload from root
      }
      Breakdown._IsInErrorHandling_ = false
   }
} // Breakdown

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

function Breakdown_ShowWasmError(message: string, reloadCurrentPage: boolean=false) {
   Breakdown.ShowWasmError(message, reloadCurrentPage)
}

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