﻿﻿import "@babel/polyfill";
import React, { useEffect } from 'react';
import ReactDOM from 'react-dom/client';
import { Provider } from 'react-redux';

import { AppConstants } from './constants/AppConstants.js';
import { init } from "./init.js";
import { store } from './reducers/store.js';
import MasterPageBase from './ui/MasterPageBase.jsx';


function MainReactApp() {
  useEffect(() => {
    console.log("\tAppConstants.DEBUG_VIEW: " + AppConstants.DEBUG_VIEW);
    console.log("\tprocess.env.NODE_ENV: " + process.env.NODE_ENV);
    console.log("\tReact.version: " + React.version);
  }, []);

  return (
    <ErrorBoundary>
      <div className={AppConstants.THEME}>
        <MasterPageBase />
      </div>
    </ErrorBoundary>
  );
}

class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }

  static getDerivedStateFromError(error) {
    // Update state so the next render will show the fallback UI.
    return { hasError: true };
  }

  componentDidCatch(error, errorInfo) {
    // You can also log the error to an error reporting service
    // logErrorToMyService(error, errorInfo);
  }

  render() {
    if (this.state.hasError) {
      // You can render any custom fallback UI
      return <h1>Something went wrong.</h1>;
    }

    return this.props.children;
  }
}

console.warn(`Start application: ${AppConstants.APP_NAME}`);
init();

const reactRoot = document.getElementById('main-react-app');
const root = ReactDOM.createRoot(reactRoot);
root.render(
  <Provider store={store} >
    <MainReactApp />
  </Provider>
);