import {
  mapState,
  mapGetters,
  mapActions,
  GetterTree,
  MutationTree,
  ActionTree,
  ActionMethod,
  ActionContext,
  Mapper,
} from 'vuex';
import { EnvelopesApi, PageOfEnvelope } from '@openapi/notifier-registry';
import axios, {  AxiosResponse } from 'axios';

interface State {
  envelopes: {
    loading: boolean;
    records: [];
    rowsNumber: number;
    countNew: number;
  };
}

const state: State = {
  envelopes: {
    loading: false,
    records: [],
    rowsNumber: 0,
    countNew: 0,
  },
};

const envelopesService = new EnvelopesApi();

const getters: GetterTree<State, State> = {
  envelopesLoading(state) {
    return state.envelopes.loading;
  },
  envelopesRecords(state) {
    return state.envelopes.records;
  },
  envelopesRowsNumber(state) {
    return state.envelopes.rowsNumber;
  },
  envelopesCountNew(state) {
    return state.envelopes.countNew;
  },
};

const mutations: MutationTree<State> = {
  envelopesLoading(state, loading) {
    state.envelopes.loading = loading;
  },
  envelopesRecords(state, records) {
    state.envelopes.records = records;
  },
  envelopesRowsNumber(state, rowsNumber) {
    state.envelopes.rowsNumber = rowsNumber;
  },
  envelopesCountNew(state, countNew) {
    state.envelopes.countNew = countNew;
  },
};

const actions: ActionTree<State, State> = {
  dispatchEnvelopeCount(
    { commit }: ActionContext<State, State>,
    { opts = {} }: any
  ) {
    return envelopesService.countEnvelopes1(undefined, {}).then((response) => {
      commit('envelopesCountNew', response.data?._new || 0);
    });
  },
  dispatchEnvelopes(
    { commit }: ActionContext<State, State>,
    { opts = {} }: any
  ) {
    const { authorization, page, size, sort, userPage, options, email } = opts;
    return new Promise((resolve, reject) => {
      commit('envelopesLoading', true);

      // todo вернуть обвязку api как только поправим бэк https://jira.egovdev.ru/browse/PGSRUSH-345

      axios.get(`/service/notifier/registry/envelopes/paging?filter=recipient.address=${email}&page=0&size=100&sort=-createDate`)
        .then((response: AxiosResponse<PageOfEnvelope>) => {

          commit('envelopesLoading', false);

          commit('envelopesRowsNumber', response.data.totalRows);

          commit('envelopesRecords', response.data.items);

          return resolve();
        })
        .catch((e) => {
          return reject(e);
        });
    });
  },
};
const notifierRegistryComputed = {
  ...mapState<Mapper<State>>('notifierRegistry', {
    envelopesLoading: (state: State) => state.envelopes.loading,
    envelopesRecords: (state: State) => state.envelopes.records,
    envelopesRowsNumber: (state: State) => state.envelopes.rowsNumber,
  }),
  ...mapGetters('notifierRegistry', [
    'envelopesLoading',
    'envelopesRecords',
    'envelopesRowsNumber',
  ]),
};

const notifierRegistryMethods: { dispatchEnvelopes: ActionMethod } = {
  ...mapActions('notifierRegistry', ['dispatchEnvelopes']),
};

export {
  state,
  getters,
  mutations,
  actions,
  notifierRegistryComputed,
  notifierRegistryMethods,
};
