/*
 * Elastic License 2.0
 * Copyright (C) 2025–present Raman Marozau, Work Target Insight Function. All rights reserved.
 * Contact: raman@worktif.com
 *
 * This file is part of the Licensed Work: worktif.runtime.cdk <worktif_runtime_cdk>.
 * Use of this software is governed by the Elastic License 2.0; see the LICENSE file
 * or https://www.elastic.co/licensing/elastic-license for details.
 *
 * Re-licensing notice:
 *   This file was previously distributed under the Business Source License 1.1 (BUSL-1.1).
 *   As of 2025-09-22, it is re-licensed under Elastic License 2.0.
 *
 * SPDX-License-Identifier: Elastic-2.0
 */

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.PURE_ENV_STACK_NAME || runtimeConfig.projectName
        : 'runtime-app',
    stage:
      typeof window === 'undefined'
        ? (process.env.PURE_ENV_STAGE as 'dev' | 'staging' | 'prod')
        : 'dev',
  },
});
