import sleep from '@hatchet-dev/typescript-sdk/util/sleep';
import { Or } from '@hatchet-dev/typescript-sdk/v1/conditions';
import { hatchet } from '../hatchet-client';

type DagInput = {};

type DagOutput = {
  'first-task': {
    Completed: boolean;
  };
  'second-task': {
    Completed: boolean;
  };
};

export const dagWithConditions = hatchet.workflow<DagInput, DagOutput>({
  name: 'simple',
});

const firstTask = dagWithConditions.task({
  name: 'first-task',
  fn: async () => {
    await sleep(2000);
    return {
      Completed: true,
    };
  },
});

dagWithConditions.task({
  name: 'second-task',
  parents: [firstTask],
  waitFor: Or({ eventKey: 'user:event' }, { sleepFor: '10s' }),
  fn: async (_, ctx) => {
    ctx.logger.info('triggered by condition', { triggers: ctx.triggers() });

    return {
      Completed: true,
    };
  },
});
