import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import {I18nextProvider} from "react-i18next";
import resourceEn from './locale/en/feedback.json'
import resourceCs from './locale/cs/feedback.json'
import {CookiesProvider} from "react-cookie"
import {Feedback} from "./types/feedback"
import {tusPath} from "./defaultConfig";

export const run = (config: Feedback.Config) => {
  if (i18next === undefined) {
    console.error('i18next script is not provided')
  }

  if (!('tus' in config)) {
    config.tus = tusPath
  }

  i18next.addResourceBundle('cs', 'feedbackSupport', resourceCs)
  i18next.addResourceBundle('en', 'feedbackSupport', resourceEn)

  const div = document.createElement('div')
  div.id = 'feedback-support-wrapper'
  div.style.display = 'none'

  document.body.append(div)

  const root = ReactDOM.createRoot(
    document.getElementById('feedback-support-wrapper') as HTMLElement
  );

  root.render(
    <>
      <I18nextProvider i18n={i18next}>
        <CookiesProvider>
          <App config={config}/>
        </CookiesProvider>
      </I18nextProvider>
    </>
  );
}

export const isOpened = () => {
  if (!('opened' in FeedbackSupport)) {
    return false
  } else {
    return FeedbackSupport.opened
  }
}

