import mzService from '@services/mz/index';
import { WorkflowServiceApi } from '@openapi/wf';
import { ActionTree, mapActions } from 'vuex';

export interface PastGatewayParams {
  id?: string;
  taskId: string;
  areaId: string;
  orderId: string;
  organizationId: string;
  comment: string;
  startDate?: string;
  endDate?: string;
  eventDate?: string;
}

export const state = {};

export const getters = {};

export const mutations = {};

export const actions: ActionTree<any, any> = {
  async eventServiceRequest({ commit, dispatch }, params: PastGatewayParams): Promise<unknown> {
    if (!params) {
      throw new Error();
    }
    const wfApi = new WorkflowServiceApi();
    const data: any = {
      className: 'eventServiceRequest@http://epgu.gosuslugi.ru/lk/order/event/3.1.1',
      to: 'test',
      object: {
        namespaceURI: 'http://epgu.gosuslugi.ru/lk/order/event/3.1.1',
        concentratorServiceType: 'eventServiceRequest',
        orderId: params.orderId,
        eventDate: params.eventDate,
        eventComment: params.comment,
        env: 'PROD',
        event: {
          equeueEvent: {
            equeueInvitation: {
              organizationData: [
                {
                  organizationId: params.organizationId,
                  areaId: params.areaId,
                },
              ],
              startDate: params.startDate,
              endDate: params.endDate,
            },
          },
        },
      },
    } as unknown;

    return new Promise((resolve, reject) => {
      if (params.taskId) {
        wfApi.submitForm(params.taskId, data);
      }
      mzService
        .postGatewayResponse(data)
        .then((responseData) => {
          resolve(responseData);
        })
        .catch((e) => {
          reject(e);
        });
    });
  },
};

export const mzGatewayMethods = mapActions('mz/gateway', []);
