// @ts-ignore
import { router } from '@router';
import { ActionContext, ActionMethod, ActionTree, mapActions } from 'vuex';
import get from 'lodash/get';

/* TODO make as util function */
export const actions: ActionTree<any, any> = {
  dispatchUserTask(
    { commit, rootGetters }: ActionContext<any, any>,
    { data, rootTraceIds = [] }: any
  ) {
    return new Promise((resolve, reject) => {
      try {
        const currentProcessTraceId: string = get(
          {
            router,
          },
          'router.currentRoute.params.traceId'
        );
        if (
          (router &&
            router.currentRoute.name === 'stepper' &&
            currentProcessTraceId === data.traceId) ||
          (router.currentRoute.name !== 'stepper' &&
            rootTraceIds.includes(data.traceId))
        ) {
          router.push({
            name: 'stepper',
            params: {
              traceId: data.traceId,
            },
            query: {
              taskId: data.body.userTask.taskId,
            },
          });
        }
        resolve(data);
      } catch (e) {
        reject(e);
      }
    });
  },
};
