import { initializeWysiwyg } from '@iteria-app/wysiwyg/dist/index.esm.js'

import { addFrontendListeners } from './frontendListeners'
import { WindowMessaging } from './messaging'
import { DevWorkbench } from './workbench/Workbench'
import { FrontendActions } from './messaging/actions'
import { addElementHighlight } from './react-instrumentation/elementHighlight'
import { BundleWorker } from './bundler'
import { ReactInstrumentation } from './react-instrumentation'

const messagingService = new WindowMessaging(window)
const bundler = new BundleWorker(messagingService)
export const workbench = new DevWorkbench(messagingService)
export const reactInstrumentation = new ReactInstrumentation(messagingService)
export const frontendActions = new FrontendActions(messagingService)

interface IteriaProps {
  fsPort?: number
  cwd?: string
  graphQLEndpoint?: string
  deploymentUrl?: string
  entryPoint?: string
  isDev?: boolean
}

export const iteriaApp = async (props?: IteriaProps) => {
  if (props.fsPort) workbench.setPort(props.fsPort)
  if (props.graphQLEndpoint) workbench.setGraphQLEndpoint(props.graphQLEndpoint)
  if (props.cwd) workbench.setCwd(props.cwd)
  if (props.isDev) {
    workbench.setIsDevServer(props.isDev)
    addFrontendListeners(messagingService, props.isDev)
    // await reactInstrumentation.setResourceHtmlElements(
    //   'https://example-material-ui.vercel.app'
    // )

    frontendActions.getProjectFromArchive(
      props?.deploymentUrl ?? window.location.origin
    )
    initializeWysiwyg()
  } else {
    initializeWysiwyg()
    addFrontendListeners(messagingService, props.isDev)
    addElementHighlight(window)
  }
}
