import 'dayjs/locale/ru'; // load on demand

import { useEffect } from 'react';

import dayjs from 'dayjs';
import { App } from 'konsta/react';
import { observer } from 'mobx-react';

import { rpcClient } from './api/request';
import AlertRoot, { alertRootRef } from './components/AlertRoot';
import FastAnswerPopupRoot, {
  fastAnswerPopupRootRef,
} from './components/FastAnswerPopupRoot';
import RecordingSheetRoot, {
  recordingSheetRef,
} from './components/RecordingSheetRoot';
import ToastRoot, {
  toast,
  toastRootRef,
} from './components/ToastRoot';
import { getEndpoint } from './config';
import {
  RouterRoot,
  routerRootRef,
} from './router/router';
import store from './stores/store';
import { isIOS } from './utils/platform';
import {
  ColorEnumBGOY,
  setColorTheme,
} from './utils/themeColor';

dayjs.locale("ru");


rpcClient.setEndpoints([getEndpoint()]);

rpcClient.setToast((message) => {
  toast(message);
});


const MainApp = () => {



  useEffect(() => {

    const searchParams = new URLSearchParams(window.location.search);


    const cap = searchParams.get("cap");
    if (cap) {
      store.setCap(true);
    } else {
      store.setCap(false);
    }



    const dark = searchParams.get("dark");
    if (dark) {
      document.body.classList.add("dark");
    } else {
      document.body.classList.remove("dark");
    }



    let theme = searchParams.get("theme");


    if (theme) {


      setColorTheme(theme as ColorEnumBGOY);

    }






  }, []);


  return <App

    theme={isIOS() ? "ios" : "material"} safeAreas>

    <RouterRoot ref={routerRootRef} />
    <FastAnswerPopupRoot ref={fastAnswerPopupRootRef} />
    <ToastRoot ref={toastRootRef} />
    <RecordingSheetRoot ref={recordingSheetRef} />
    <AlertRoot ref={alertRootRef} />
  </App>

};

export default observer(MainApp);
