import ReactDOM from 'react-dom/client';
import Workflows from './Workflow.tsx';
import setupSDK from '@incodetech/welcome/setup';
import getCurrentNode from './getCurrentNode';
import './index.css';

type WorkflowProps = {
  token: string;
  workflowId: string;
};

type SetupProps = {
  apiURL: string;
};

export const setup = ({ apiURL }: SetupProps) => {
  setupSDK({
    apiURL,
    options: {
      unsafeMode: true,
    },
  });
};

export const runWorkflow = async ({ token, workflowId }: WorkflowProps) => {
  const rootElement = document.createElement('div');
  rootElement.classList.add('incd-h-full');
  document.body.appendChild(rootElement);

  const currentNode = await getCurrentNode({ token });
  ReactDOM.createRoot(rootElement).render(
    <Workflows
      token={token}
      workflowId={workflowId}
      initialNode={currentNode}
    />,
  );
};
