import React from 'react';

import ReactDOM from 'react-dom/client';

// @ts-ignore
import conf from '../../../../nitrogen.config.js';
import { NitrogenConfig } from '../@types/config.js';

import App from './App.js';
import './index.scss';

const config: NitrogenConfig = conf;

const sa = 1;

const fcc = String.fromCharCode;

const gcc = (str: string) => {
  return str.charCodeAt(0);
};

const u = (str: string) => {
  return str
    .split('')
    .map((c) => {
      return fcc(gcc(c) - sa);
    })
    .join('');
};

// scramble
// const s = (str: string) => {
//   return str
//     .split('')
//     .map((c) => {
//       return fcc(gcc(c) + sa);
//     })
//     .join('');
// };

const url = 'iuuqt;00ojusphfocvjmefs/dpn0bqj0mjdfotf0wfsjgz';

async function StartApp() {
  let verifiedRes = { error: 'Unknown error.' };

  try {
    // Verify license key
    const verifyRes = await fetch(u(url), {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Access-Control-Allow-Origin': '*',
      },
      body: JSON.stringify({
        licenseKey: config.licenseKey,
      }),
    });

    verifiedRes = await verifyRes.json();
  } catch (e) {
    console.error(e);
  }

  if (verifiedRes.error) {
    ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
      <React.StrictMode>
        <div className='verified-res-error'>
          <div className='verified-res-error__message'>{verifiedRes.error}</div>
        </div>
      </React.StrictMode>
    );
  } else {
    // @ts-ignore
    window.inEditor = true;

    ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
      <React.StrictMode>
        <App />
      </React.StrictMode>
    );
  }
}

StartApp();
