import { setup, runWorkflow } from './index';

const myWorkflowId = '6699466cd5a8f66e5aab0075';
const getSession = async () => {
  const response = await fetch(
    'https://user-service-k8s.stage.incodetest.com/omni/start',
    {
      headers: {
        accept: 'application/json',
        'content-type': 'application/json',
        'x-api-key': '13f87c72debb0228544d3322e35d1e3b9118afe3',
        'api-version': '1.0',
      },
      body: JSON.stringify({
        configurationId: myWorkflowId,
        language: 'en-US',
      }),
      method: 'POST',
    },
  );
  const data = await response.json();
  return data;
};

let isRunning = false;
const showApp = () => {
  const appSection = document.querySelector('section#app');
  console.log(appSection);
  appSection?.classList.remove('incd-hidden');
  appSection?.classList.add('incd-flex');
};

const app = async () => {
  if (isRunning) return;
  const session = await getSession();
  setup({ apiURL: 'https://user-service-k8s.stage.incodetest.com/0' });
  runWorkflow({
    token: session.token,
    workflowId: myWorkflowId,
  });
  isRunning = true;
};

showApp();
const button = document.querySelector('button#btn');
button?.addEventListener('click', app);
