import { ActionContext, ActionTree, mapActions } from 'vuex';
import mzService from '../../../services/mz/index';

export const state = {};

export const getters = {};

export const mutations = {};

export const actions: ActionTree<any, any> = {
  getHistoryInfo({ commit }: ActionContext<any, any>, id: string) {
    return new Promise((resolve, reject) => {
      if (!id) {
        return reject('[mz][history][getHistoryInfo] Идентификатор обязателен');
      }

      mzService
        .getHistoryById(id)
        .then((data) => {
          resolve(data);
        })
        .catch((e) => {
          reject(e);
        });
    });
  },
  getCurrentXml(
    { commit }: ActionContext<any, any>,
    opts: Record<string, string>
  ) {
    const { type, id }: Record<string, string> = opts;
    return new Promise((resolve, reject) => {
      if (!id) {
        return reject('[mz][history][getCurrentXml] Идентификатор обязателен');
      }

      mzService
        .getCurrentXml(type, id)
        .then((data) => {
          resolve(data);
        })
        .catch((e) => {
          reject(e);
        });
    });
  },
};

export const mzHistoryMethods = mapActions('mz/history', [
  'getHistoryInfo',
  'getCurrentXml',
]);
