/*
 * Runtime Web – Unified Serverless Framework for Full-Stack TypeScript Applications
 * Documentation: https://runtimeweb.com/docs/getting-started
 *
 * @template: basic
 */

import * as React from 'react';
import { ErrorBoundary, runtime, runtimeRouter } from '@worktif/runtime';

import runtimeConfig from 'runtime.config';

import App from './App';
import { routes } from './routes';

// Initialize Runtime Web application with serverless rendering support
runtime({
  app: ({ router }) => <App router={router}/>,
  router: runtimeRouter({
    router: routes,
    defaults: {
      HydrateFallback: () => <div>Loading...</div>,
      ErrorBoundary: ({ error }: any) =>
        <ErrorBoundary>{error?.message || 'An unexpected error occurred. Please try again later.'}</ErrorBoundary>,
    },
  }),
  config: {
    serviceName:
      typeof window === 'undefined'
        ? process.env.RUNTIME_ENV_STACK_NAME || runtimeConfig.projectName
        : 'runtime-app',
    stage:
      typeof window === 'undefined'
        ? (process.env.RUNTIME_ENV_STAGE as 'dev' | 'staging' | 'prod')
        : 'dev',
  },

  // Microservice registry
  register: {},
});
