/* eslint-disable react/display-name */
import React from 'react';
import ReactDOM from 'react-dom/client';
  
import App from './app';
import { keycloak } from "./__data__/kc";
import { createStore } from "./__data__/store";

if(!module.hot) {
  import('./ym');
}

export default (props) => <App {...props} />;
  
let rootElement: ReactDOM.Root
  
export const mount = async (Сomponent, element = document.getElementById('app')) => {
  let user = null;
  try {
    await keycloak.init({ onLoad: "login-required" });
    user = { ...(await keycloak.loadUserInfo()), ...keycloak.tokenParsed };
  } catch (error) {
    console.error("Failed to initialize adapter:", error);
    keycloak.login();
  }
  const store = createStore({ user });

  rootElement = ReactDOM.createRoot(element);
  rootElement.render(<Сomponent store={store} />);

  if(module.hot) {
      module.hot.accept('./app', ()=> {
        rootElement.render(<Сomponent store={store} />);
      })
  }
};

export const unmount = () => {
  rootElement.unmount();
};
