import {
  ActionContext,
  ActionTree,
  GetterTree,
  Module,
  MutationTree,
} from 'vuex';
import { DictionaryApi } from '@openapi/registry-service';
import {
  DictionariesElement,
  ErvuMzMonitorServiceApi,
} from '@/recruits/openapi/ervu-service';
import { NsiServiceApi } from '@/recruits/openapi/nsi';
import { DeliveryDirectionsServiceApi } from '@/recruits/openapi/subpoena-service';
export type StringDictionary = { [key: string]: string };
type StringDictionaryFull = { [key: string]: { [key: string]: string } };

const NSI_SERVICE_API = new NsiServiceApi();
const ervuMzMonitorServiceApi = new ErvuMzMonitorServiceApi();
const deliveryDirectionsServiceApi = new DeliveryDirectionsServiceApi();
const dictionaryApi = new DictionaryApi();

export interface State {
  initialized: boolean;
  recruitStatusDictionary: DictionariesElement[];
  recruitStatusDictionaryLoading: boolean;
  subpoenaStatusDictionary: StringDictionary;
  subpoenaStatusDictionaryLoading: boolean;
  resolutionsDocumentsDictionary: StringDictionary;
  resolutionsDocumentsDictionaryLoading: boolean;
  commissionDocumentTypesDictionary: DictionariesElement[];
  militaryCommissarDocumentTypesDictionary: DictionariesElement[];
  militaryCommissarDecisionStatusDictionary: DictionariesElement[];
  personalDocumentTypesDictionary: DictionariesElement[];
  systemDocumentStatusesDictionary: DictionariesElement[];
  privateFileStatusDictionary: DictionariesElement[];
  militaryRegistrationUpdatingGoalsDictionary: StringDictionary;
  militaryRegistrationUpdatingGoalsDictionaryLoading: boolean;
  militaryRegistrationUpdatingGoalsDictionaryFull: StringDictionaryFull;
  militaryRegistrationUpdatingGoalsDictionaryFullLoading: boolean;
  militaryRegistrationUpdatingGoalCategoriesDictionary: StringDictionary;
  militaryRegistrationUpdatingGoalCategoriesDictionaryLoading: boolean;
  militaryRegistrationReasonsDictionary: DictionariesElement[];
  typeAdministrativePunishment: StringDictionary;
  typeAdministrativePunishmentLoading: boolean;
  typeCriminalPunishment: StringDictionary;
  typeCriminalPunishmentLoading: boolean;
  listChangeStatusDictionary: StringDictionary;
  listChangeStatusDictionaryLoading: boolean;
  causeNotConfirmDictionary: StringDictionary;
  causeNotConfirmDictionaryLoading: boolean;
  incidentsStatusesDictionary: StringDictionary;
  incidentsStatusesDictionaryLoading: boolean;
  sourcesRecruitDictionary: StringDictionary;
  sourcesRecruitDictionaryLoading: boolean;
  typeGepsDictionary: StringDictionary;
  typeGepsDictionaryLoading: boolean;
  statusGepsDictionary: StringDictionary;
  statusGepsDictionaryLoading: boolean;
  recruitErvuApplicationStatus: StringDictionary;
  recruitErvuApplicationStatusLoading: boolean;
  typesDulRf: StringDictionary;
  typesDulRfLoading: boolean;
  decisionTypeForGoodReason: StringDictionary;
  decisionTypeForGoodReasonLoading: boolean;
  decisionReason: StringDictionary;
  decisionReasonLoading: boolean;
  typesMzClass: StringDictionary;
  typesMzClassLoading: boolean;
  recruitStatusDescriptionLoading: boolean;
  deliveryDirections: StringDictionary;
  deliveryDirectionsFull: StringDictionary;
  deliveryDirectionsLoading: boolean;
  criminalPunishmentCausesList: DictionariesElement[];
  administrativePunishmentCausesList: DictionariesElement[];
  punishmentAppealList: DictionariesElement[];
  incidentsSourceMap: StringDictionary;
  incidentsSource: DictionariesElement[];
  archivationReasonsList: DictionariesElement[];
  incidentTypes: StringDictionary;
  incidentTypesReasons: StringDictionary;
  addressTypeDictionary: StringDictionary;
  addressTypeDictionaryLoading: boolean;
}

export const state: State = {
  initialized: false,
  recruitStatusDictionary: [],
  recruitStatusDictionaryLoading: false,
  subpoenaStatusDictionary: {},
  subpoenaStatusDictionaryLoading: false,
  resolutionsDocumentsDictionary: {},
  resolutionsDocumentsDictionaryLoading: false,
  commissionDocumentTypesDictionary: [],
  militaryCommissarDocumentTypesDictionary: [],
  militaryCommissarDecisionStatusDictionary: [],
  personalDocumentTypesDictionary: [],
  systemDocumentStatusesDictionary: [],
  privateFileStatusDictionary: [],
  militaryRegistrationUpdatingGoalsDictionary: {},
  militaryRegistrationUpdatingGoalsDictionaryLoading: false,
  militaryRegistrationUpdatingGoalsDictionaryFull: {},
  militaryRegistrationUpdatingGoalsDictionaryFullLoading: false,
  militaryRegistrationUpdatingGoalCategoriesDictionary: {},
  militaryRegistrationUpdatingGoalCategoriesDictionaryLoading: false,
  militaryRegistrationReasonsDictionary: [],
  typeAdministrativePunishment: {},
  typeAdministrativePunishmentLoading: false,
  typeCriminalPunishment: {},
  typeCriminalPunishmentLoading: false,
  listChangeStatusDictionary: {},
  listChangeStatusDictionaryLoading: false,
  causeNotConfirmDictionary: {},
  causeNotConfirmDictionaryLoading: false,
  incidentsStatusesDictionary: {},
  incidentsStatusesDictionaryLoading: false,
  sourcesRecruitDictionary: {},
  sourcesRecruitDictionaryLoading: false,
  typeGepsDictionary: {},
  typeGepsDictionaryLoading: false,
  statusGepsDictionary: {},
  statusGepsDictionaryLoading: false,
  recruitErvuApplicationStatus: {},
  recruitErvuApplicationStatusLoading: false,
  typesDulRf: {},
  typesDulRfLoading: false,
  decisionTypeForGoodReason: {},
  decisionTypeForGoodReasonLoading: false,
  decisionReason: {},
  decisionReasonLoading: false,
  typesMzClass: {},
  typesMzClassLoading: false,
  recruitStatusDescriptionLoading: false,
  deliveryDirections: {},
  deliveryDirectionsFull: {},
  deliveryDirectionsLoading: false,
  criminalPunishmentCausesList: [],
  administrativePunishmentCausesList: [],
  punishmentAppealList: [],
  incidentsSourceMap: {},
  incidentsSource: [],
  archivationReasonsList: [],
  incidentTypes: {},
  incidentTypesReasons: {},
  addressTypeDictionary: {},
  addressTypeDictionaryLoading: false,
};

export const getters: GetterTree<State, any> = {
  initialized(state: State) {
    return state.initialized;
  },
  loading(state: State): boolean {
    return (
      state.recruitStatusDictionaryLoading ||
      state.subpoenaStatusDictionaryLoading
    );
  },
  recruitStatusDictionary(state: State) {
    return state.recruitStatusDictionary;
  },
  recruitStatusStringDictionary(state: State) {
    return state.recruitStatusDictionary.reduce(
      (dictionary: StringDictionary, status) => {
        dictionary[status.code] = status.value || '';
        return dictionary;
      },
      {}
    );
  },
  recruitStatusJournalEventDictionary(state: State) {
    return state.recruitStatusDictionary.reduce(
      (dictionary: StringDictionary, status) => {
        const event = status?.extraInfo?.journal_event || '';
        if (event) dictionary[status.code] = event;
        return dictionary;
      },
      {}
    );
  },
  recruitStatusDictionaryLoading(state: State) {
    return state.recruitStatusDictionaryLoading;
  },
  subpoenaStatusDictionary(state: State) {
    return state.subpoenaStatusDictionary;
  },
  subpoenaStatusDictionaryLoading(state: State) {
    return state.subpoenaStatusDictionaryLoading;
  },
  resolutionsDocumentsDictionary(state: State) {
    return state.resolutionsDocumentsDictionary;
  },
  resolutionsDocumentsDictionaryLoading(state: State) {
    return state.resolutionsDocumentsDictionaryLoading;
  },
  commissionDocumentTypesDictionary(state: State) {
    return state.commissionDocumentTypesDictionary;
  },
  militaryCommissarDocumentTypesDictionary(state: State) {
    return state.militaryCommissarDocumentTypesDictionary;
  },
  militaryCommissarDecisionStatusDictionary(state: State) {
    return state.militaryCommissarDecisionStatusDictionary;
  },
  personalDocumentTypesDictionary(state: State) {
    return state.personalDocumentTypesDictionary;
  },
  systemDocumentStatusesDictionary(state: State) {
    return state.systemDocumentStatusesDictionary;
  },
  privateFileStatusDictionary(state: State) {
    return state.privateFileStatusDictionary;
  },
  militaryRegistrationUpdatingGoalsDictionary(state: State) {
    return state.militaryRegistrationUpdatingGoalsDictionary;
  },
  militaryRegistrationUpdatingGoalsDictionaryLoading(state: State) {
    return state.militaryRegistrationUpdatingGoalsDictionaryLoading;
  },
  militaryRegistrationUpdatingGoalsDictionaryFull(state: State) {
    return state.militaryRegistrationUpdatingGoalsDictionaryFull;
  },
  militaryRegistrationUpdatingGoalsDictionaryFullLoading(state: State) {
    return state.militaryRegistrationUpdatingGoalsDictionaryFullLoading;
  },
  militaryRegistrationUpdatingGoalCategoriesDictionary(state: State) {
    return state.militaryRegistrationUpdatingGoalCategoriesDictionary;
  },
  militaryRegistrationUpdatingGoalCategoriesDictionaryLoading(state: State) {
    return state.militaryRegistrationUpdatingGoalCategoriesDictionaryLoading;
  },
  militaryRegistrationReasonsDictionary(state: State) {
    return state.militaryRegistrationReasonsDictionary;
  },
  typeAdministrativePunishment(state: State) {
    return state.typeAdministrativePunishment;
  },
  typeAdministrativePunishmentLoading(state: State) {
    return state.typeAdministrativePunishmentLoading;
  },
  typeCriminalPunishment(state: State) {
    return state.typeCriminalPunishment;
  },
  typeCriminalPunishmentLoading(state: State) {
    return state.typeCriminalPunishmentLoading;
  },
  listChangeStatusDictionary(state: State) {
    return state.listChangeStatusDictionary;
  },
  listChangeStatusDictionaryLoading(state: State) {
    return state.listChangeStatusDictionaryLoading;
  },
  causeNotConfirmDictionary(state: State) {
    return state.causeNotConfirmDictionary;
  },
  causeNotConfirmDictionaryLoading(state: State) {
    return state.causeNotConfirmDictionaryLoading;
  },
  incidentsStatusesDictionary(state: State) {
    return state.incidentsStatusesDictionary;
  },
  incidentsStatusesDictionaryLoading(state: State) {
    return state.incidentsStatusesDictionaryLoading;
  },
  sourcesRecruitDictionary(state: State) {
    return state.sourcesRecruitDictionary;
  },
  sourcesRecruitDictionaryLoading(state: State) {
    return state.sourcesRecruitDictionaryLoading;
  },
  typeGepsDictionary(state: State) {
    return state.typeGepsDictionary;
  },
  typeGepsDictionaryLoading(state: State) {
    return state.typeGepsDictionaryLoading;
  },
  statusGepsDictionary(state: State) {
    return state.statusGepsDictionary;
  },
  statusGepsDictionaryLoading(state: State) {
    return state.statusGepsDictionaryLoading;
  },
  recruitErvuApplicationStatus(state: State) {
    return state.recruitErvuApplicationStatus;
  },
  recruitErvuApplicationStatusLoading(state: State) {
    return state.recruitErvuApplicationStatusLoading;
  },
  typesDulRf(state: State) {
    return state.typesDulRf;
  },
  typesDulRfLoading(state: State) {
    return state.typesDulRfLoading;
  },
  decisionTypeForGoodReason(state: State) {
    return state.decisionTypeForGoodReason;
  },
  decisionTypeForGoodReasonLoading(state: State) {
    return state.decisionTypeForGoodReasonLoading;
  },
  decisionReason(state: State) {
    return state.decisionReason;
  },
  decisionReasonLoading(state: State) {
    return state.decisionReasonLoading;
  },
  typesMzClass(state: State) {
    return state.typesMzClass;
  },
  typesMzClassLoading(state: State) {
    return state.typesMzClassLoading;
  },
  recruitStatusDescription(state: State) {
    return state.recruitStatusDictionary.reduce(
      (dictionary: StringDictionary, status) => {
        if (status.description) {
          dictionary[status.code] = status.description;
        }
        return dictionary;
      },
      {}
    );
  },
  recruitStatusDescriptionLoading(state: State) {
    return state.recruitStatusDescriptionLoading;
  },
  getDeliveryDirections(state: State) {
    return state.deliveryDirections;
  },
  getDeliveryDirectionsFull(state: State) {
    return state.deliveryDirectionsFull;
  },
  getDeliveryDirectionsLoading(state: State) {
    return state.deliveryDirectionsLoading;
  },
  criminalPunishmentCausesList(state: State) {
    return state.criminalPunishmentCausesList;
  },
  administrativePunishmentCausesList(state: State) {
    return state.administrativePunishmentCausesList;
  },
  punishmentAppealList(state: State) {
    return state.punishmentAppealList;
  },
  incidentsSourceMap(state: State) {
    return state.incidentsSourceMap;
  },
  incidentsSource(state: State) {
    return state.incidentsSource;
  },
  archivationReasonsList(state: State) {
    return state.archivationReasonsList;
  },
  incidentTypes(state: State) {
    return state.incidentTypes;
  },
  incidentTypesReasons(state: State) {
    return state.incidentTypesReasons;
  },
  addressTypeDictionary(state: State) {
    return state.addressTypeDictionary;
  },
};

export const mutations: MutationTree<State> = {
  setInitialized(state: State, value: boolean) {
    state.initialized = value;
  },
  setRecruitStatusDictionary(state: State, dictionary: DictionariesElement[]) {
    state.recruitStatusDictionary = dictionary;
  },
  setRecruitStatusDictionaryLoading(state: State, loading: boolean) {
    state.recruitStatusDictionaryLoading = loading;
  },
  setSubpoenaStatusDictionary(state: State, dictionary: StringDictionary) {
    state.subpoenaStatusDictionary = dictionary;
  },
  setSubpoenaStatusDictionaryLoading(state: State, loading: boolean) {
    state.subpoenaStatusDictionaryLoading = loading;
  },
  setResolutionsDocumentsDictionary(
    state: State,
    dictionary: StringDictionary
  ) {
    state.resolutionsDocumentsDictionary = dictionary;
  },
  setResolutionsDocumentsDictionaryLoading(state: State, loading: boolean) {
    state.resolutionsDocumentsDictionaryLoading = loading;
  },
  setCommissionDocumentTypesDictionary(
    state: State,
    dictionary: DictionariesElement[]
  ) {
    state.commissionDocumentTypesDictionary = dictionary;
  },
  setMilitaryCommissarDocumentTypesDictionary(
    state: State,
    dictionary: DictionariesElement[]
  ) {
    state.militaryCommissarDocumentTypesDictionary = dictionary;
  },
  setMilitaryCommissarDecisionStatusDictionary(
    state: State,
    dictionary: DictionariesElement[]
  ) {
    state.militaryCommissarDecisionStatusDictionary = dictionary;
  },
  setPersonalDocumentTypesDictionary(
    state: State,
    dictionary: DictionariesElement[]
  ) {
    state.personalDocumentTypesDictionary = dictionary;
  },
  setSystemDocumentStatusesDictionary(
    state: State,
    dictionary: DictionariesElement[]
  ) {
    state.systemDocumentStatusesDictionary = dictionary;
  },
  setPrivateFileStatusDictionary(
    state: State,
    dictionary: DictionariesElement[]
  ) {
    state.privateFileStatusDictionary = dictionary;
  },
  setMilitaryRegistrationUpdatingGoalsDictionary(
    state: State,
    dictionary: StringDictionary
  ) {
    state.militaryRegistrationUpdatingGoalsDictionary = dictionary;
  },
  setMilitaryRegistrationUpdatingGoalsDictionaryLoading(
    state: State,
    loading: boolean
  ) {
    state.militaryRegistrationUpdatingGoalsDictionaryLoading = loading;
  },
  setMilitaryRegistrationUpdatingGoalsDictionaryFull(
    state: State,
    dictionary: StringDictionaryFull
  ) {
    state.militaryRegistrationUpdatingGoalsDictionaryFull = dictionary;
  },
  setMilitaryRegistrationUpdatingGoalsDictionaryFullLoading(
    state: State,
    loading: boolean
  ) {
    state.militaryRegistrationUpdatingGoalsDictionaryFullLoading = loading;
  },
  setMilitaryRegistrationUpdatingGoalCategoriesDictionary(
    state: State,
    dictionary: StringDictionary
  ) {
    state.militaryRegistrationUpdatingGoalCategoriesDictionary = dictionary;
  },
  setMilitaryRegistrationUpdatingGoalCategoriesDictionaryLoading(
    state: State,
    loading: boolean
  ) {
    state.militaryRegistrationUpdatingGoalCategoriesDictionaryLoading = loading;
  },
  setMilitaryRegistrationReasonsDictionary(
    state: State,
    dictionary: DictionariesElement[]
  ) {
    state.militaryRegistrationReasonsDictionary = dictionary;
  },
  setTypeAdministrativePunishment(state: State, dictionary: StringDictionary) {
    state.typeAdministrativePunishment = dictionary;
  },
  setTypeAdministrativePunishmentLoading(state: State, loading: boolean) {
    state.typeAdministrativePunishmentLoading = loading;
  },
  setTypeCriminalPunishment(state: State, dictionary: StringDictionary) {
    state.typeCriminalPunishment = dictionary;
  },
  setTypeCriminalPunishmentLoading(state: State, loading: boolean) {
    state.typeCriminalPunishmentLoading = loading;
  },
  setListChangeStatusDictionary(state: State, dictionary: StringDictionary) {
    state.listChangeStatusDictionary = dictionary;
  },
  setListChangeStatusDictionaryLoading(state: State, loading: boolean) {
    state.listChangeStatusDictionaryLoading = loading;
  },
  setCauseNotConfirmDictionary(state: State, dictionary: StringDictionary) {
    state.causeNotConfirmDictionary = dictionary;
  },
  setCauseNotConfirmDictionaryLoading(state: State, loading: boolean) {
    state.causeNotConfirmDictionaryLoading = loading;
  },
  setIncidentsStatusesDictionary(state: State, dictionary: StringDictionary) {
    state.incidentsStatusesDictionary = dictionary;
  },
  setIncidentsStatusesDictionaryLoading(state: State, loading: boolean) {
    state.incidentsStatusesDictionaryLoading = loading;
  },
  setSourcesRecruitDictionary(state: State, dictionary: StringDictionary) {
    state.sourcesRecruitDictionary = dictionary;
  },
  setSourcesRecruitDictionaryLoading(state: State, loading: boolean) {
    state.sourcesRecruitDictionaryLoading = loading;
  },
  setTypeGepsDictionary(state: State, dictionary: StringDictionary) {
    state.typeGepsDictionary = dictionary;
  },
  setTypeGepsDictionaryLoading(state: State, loading: boolean) {
    state.typeGepsDictionaryLoading = loading;
  },
  setStatusGepsDictionary(state: State, dictionary: StringDictionary) {
    state.statusGepsDictionary = dictionary;
  },
  setStatusGepsDictionaryLoading(state: State, loading: boolean) {
    state.statusGepsDictionaryLoading = loading;
  },
  setRecruitErvuApplicationStatus(state: State, dictionary: StringDictionary) {
    state.recruitErvuApplicationStatus = dictionary;
  },
  setRecruitErvuApplicationStatusLoading(state: State, loading: boolean) {
    state.recruitErvuApplicationStatusLoading = loading;
  },
  setTypesDulRf(state: State, dictionary: StringDictionary) {
    state.typesDulRf = dictionary;
  },
  setTypesDulRfLoading(state: State, loading: boolean) {
    state.typesDulRfLoading = loading;
  },
  setDecisionTypeForGoodReason(state: State, dictionary: StringDictionary) {
    state.decisionTypeForGoodReason = dictionary;
  },
  setDecisionTypeForGoodReasonLoading(state: State, loading: boolean) {
    state.decisionTypeForGoodReasonLoading = loading;
  },
  setDecisionReason(state: State, dictionary: StringDictionary) {
    state.decisionReason = dictionary;
  },
  setDecisionReasonLoading(state: State, loading: boolean) {
    state.decisionReasonLoading = loading;
  },
  setTypesMzClass(state: State, dictionary: StringDictionary) {
    state.typesMzClass = dictionary;
  },
  setTypesMzClassLoading(state: State, loading: boolean) {
    state.typesMzClassLoading = loading;
  },
  setRecruitStatusDescriptionLoading(state: State, loading: boolean) {
    state.recruitStatusDescriptionLoading = loading;
  },
  setDeliveryDirections(state: State, dictionary: StringDictionary) {
    state.deliveryDirections = dictionary;
  },
  setDeliveryDirectionsFull(state: State, dictionary: StringDictionary) {
    state.deliveryDirectionsFull = dictionary;
  },
  setDeliveryDirectionsLoading(state: State, loading: boolean) {
    state.deliveryDirectionsLoading = loading;
  },
  setCriminalPunishmentCausesList(state: State, value: DictionariesElement[]) {
    state.criminalPunishmentCausesList = value;
  },
  setAdministrativePunishmentCausesList(
    state: State,
    value: DictionariesElement[]
  ) {
    state.administrativePunishmentCausesList = value;
  },
  setPunishmentAppealList(state: State, value: DictionariesElement[]) {
    state.punishmentAppealList = value;
  },
  setIncidentsSourceMap(state: State, value: StringDictionary) {
    state.incidentsSourceMap = value;
  },
  setIncidentsSource(state: State, value: DictionariesElement[]) {
    state.incidentsSource = value;
  },
  setArchivationReasonsList(state: State, value: DictionariesElement[]) {
    state.archivationReasonsList = value;
  },
  setIncidentTypes(state: State, value: StringDictionary) {
    state.incidentTypes = value;
  },
  setIncidentTypesReasons(state: State, value: StringDictionary) {
    state.incidentTypesReasons = value;
  },
  setAddressTypeDictionary(state: State, value: StringDictionary) {
    state.addressTypeDictionary = value;
  },
  setAddressTypeDictionaryLoading(state: State, value: boolean) {
    state.addressTypeDictionaryLoading = value;
  },
};

export const actions: ActionTree<State, State> = {
  initialize({ commit, dispatch }: ActionContext<State, State>) {
    dispatch('loadRecruitStatusDictionary');
    dispatch('loadSubpoenaStatusDictionary');
    dispatch('loadListChangeStatusDictionary');
    dispatch('loadMilitaryRegistrationUpdatingGoalsDictionary');
    dispatch('loadMilitaryRegistrationUpdatingGoalCategoriesDictionary');
    dispatch('loadMilitaryCommissarDecisionStatus');
    dispatch('loadTypeAdministrativePunishment');
    dispatch('loadTypeCriminalPunishment');
    dispatch('loadResolutionsDocumentsDictionary');
    dispatch('loadCommissionDocumentTypesDictionary');
    dispatch('loadCauseNotConfirmDictionary');
    dispatch('loadIncidentsStatusesDictionary');
    dispatch('loadSourcesRecruitDictionary');
    dispatch('loadTypeGepsDictionary');
    dispatch('loadStatusGepsDictionary');
    dispatch('loadRecruitErvuApplicationStatus');
    dispatch('loadTypesDulRf');
    dispatch('loadDecisionTypeForGoodReason');
    dispatch('loadDecisionReason');
    dispatch('loadTypesMzClass');
    dispatch('loadDeliveryTypeSubpoena');
    dispatch('loadCriminalPunishmentCausesList');
    dispatch('loadAdministrativePunishmentCausesList');
    dispatch('loadPunishmentAppealList');
    dispatch('loadIncidentsSource');
    dispatch('loadArchivationReasonsList');
    dispatch('loadIncidentTypes');
    dispatch('loadAddressTypeDictionary');
    commit('setInitialized', true);
  },
  loadRecruitStatusDictionary({ commit }: ActionContext<State, State>) {
    commit('setRecruitStatusDictionaryLoading', true);
    commit('setRecruitStatusDescriptionLoading', true);
    const DICTIONARY_CODE = 'ervuPersonRegistryRecruitErvuStatus';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        commit('setRecruitStatusDictionary', response?.data || []);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      })
      .finally(() => {
        commit('setRecruitStatusDictionaryLoading', false);
        commit('setRecruitStatusDescriptionLoading', false);
      });
  },
  loadResolutionsDocumentsDictionary({ commit }: ActionContext<State, State>) {
    commit('setResolutionsDocumentsDictionaryLoading', true);
    const DICTIONARY_CODE =
      'ervuDecisionDocumentServiceTypeResolutionsDocuments';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        const result: {
          [key: string]: string;
        } = {};
        if (Array.isArray(response.data) && response.data.length) {
          response.data.forEach(({ code, description }) => {
            result[code] = description || '';
          });
        }
        commit('setResolutionsDocumentsDictionary', result);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      })
      .finally(() => {
        commit('setResolutionsDocumentsDictionaryLoading', false);
      });
  },
  loadCommissionDocumentTypesDictionary({
    commit,
  }: ActionContext<State, State>) {
    const DICTIONARY_CODE = 'ervuDecisionDocumentServiceForeignDecisionType';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        commit('setCommissionDocumentTypesDictionary', response?.data || []);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      });
  },

  loadMilitaryCommissarDocumentTypesDictionary({
    commit,
  }: ActionContext<State, State>) {
    const DICTIONARY_CODE = 'ervuDecisionDocumentServiceDecisionType';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        commit(
          'setMilitaryCommissarDocumentTypesDictionary',
          response?.data || []
        );
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      });
  },
  loadMilitaryCommissarDecisionStatus({ commit }: ActionContext<State, State>) {
    const DICTIONARY_CODE = 'ervuDecisionDocumentServiceDecisionStatus';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        commit(
          'setMilitaryCommissarDecisionStatusDictionary',
          response?.data || []
        );
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      });
  },
  loadPersonalDocumentTypesDictionary({ commit }: ActionContext<State, State>) {
    const DICTIONARY_CODE = 'ervuPersonRegistryPersonalDocumentTypes';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        commit('setPersonalDocumentTypesDictionary', response?.data || []);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      });
  },
  loadSystemDocumentStatusesDictionary({
    commit,
  }: ActionContext<State, State>) {
    const DICTIONARY_CODE = 'ervuPersonRegistrySystemDocumentStatus';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        commit('setSystemDocumentStatusesDictionary', response?.data || []);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      });
  },
  loadPrivateFileStatusDictionary({ commit }: ActionContext<State, State>) {
    const DICTIONARY_CODE = 'ervuPersonRegistryRecruitPrivateFileStatus';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        commit('setPrivateFileStatusDictionary', response?.data || []);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      });
  },
  loadSubpoenaStatusDictionary({ commit }: ActionContext<State, State>) {
    commit('setSubpoenaStatusDictionaryLoading', true);
    const DICTIONARY_CODE = 'subpoena_ervu_status';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        const result: { [key: string]: string } = {};
        if (Array.isArray(response.data) && response.data.length) {
          response.data.forEach(({ code, value }) => {
            result[code] = value || '';
          });
        }
        commit('setSubpoenaStatusDictionary', result);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      })
      .finally(() => {
        commit('setSubpoenaStatusDictionaryLoading', false);
      });
  },
  loadMilitaryRegistrationUpdatingGoalsDictionary({
    commit,
  }: ActionContext<State, State>) {
    commit('setMilitaryRegistrationUpdatingGoalsDictionaryLoading', true);
    commit('setMilitaryRegistrationUpdatingGoalsDictionaryFullLoading', true);
    const DICTIONARY_CODE = 'MilitaryRegistrationUpdatingGoals';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        const result: { [key: string]: string } = {};
        const resultFull: { [key: string]: { [key: string]: string } } = {};
        if (Array.isArray(response.data) && response.data.length) {
          response.data.forEach(({ code, value, extraInfo }) => {
            result[code] = value || '';
            resultFull[code] = {
              value: value || '',
              type: extraInfo?.type || '',
            };
          });
        }

        commit('setMilitaryRegistrationUpdatingGoalsDictionary', result);
        commit(
          'setMilitaryRegistrationUpdatingGoalsDictionaryFull',
          resultFull
        );
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      })
      .finally(() => {
        commit('setMilitaryRegistrationUpdatingGoalsDictionaryLoading', false);
        commit(
          'setMilitaryRegistrationUpdatingGoalsDictionaryFullLoading',
          false
        );
      });
  },
  loadMilitaryRegistrationUpdatingGoalCategoriesDictionary({
    commit,
  }: ActionContext<State, State>) {
    commit(
      'setMilitaryRegistrationUpdatingGoalCategoriesDictionaryLoading',
      true
    );
    const DICTIONARY_CODE = 'MilitaryRegistrationUpdatingGoalCategories';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        const result: { [key: string]: string } = {};
        if (Array.isArray(response.data) && response.data.length) {
          response.data.forEach(({ code, value }) => {
            result[code] = value || '';
          });
        }
        commit(
          'setMilitaryRegistrationUpdatingGoalCategoriesDictionary',
          result
        );
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      })
      .finally(() => {
        commit(
          'setMilitaryRegistrationUpdatingGoalCategoriesDictionaryLoading',
          false
        );
      });
  },
  loadMilitaryRegistrationReasonsDictionary({
    commit,
  }: ActionContext<State, State>) {
    const DICTIONARY_CODE = 'ervuPersonRegistryMilitaryRegistrationReason';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        commit(
          'setMilitaryRegistrationReasonsDictionary',
          response?.data || []
        );
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      });
  },
  loadTypeAdministrativePunishment({ commit }: ActionContext<State, State>) {
    commit('setTypeAdministrativePunishmentLoading', true);
    const DICTIONARY_CODE =
      'ervuDecisionDocumentServiceTypeAdministrativePunishment';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        const result: { [key: string]: string } = {};
        if (Array.isArray(response.data) && response.data.length) {
          response.data.forEach(({ code, value }) => {
            result[code] = value || '';
          });
        }
        commit('setTypeAdministrativePunishment', result);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      })
      .finally(() => {
        commit('setTypeAdministrativePunishmentLoading', false);
      });
  },
  loadTypeCriminalPunishment({ commit }: ActionContext<State, State>) {
    commit('setTypeCriminalPunishmentLoading', true);
    const DICTIONARY_CODE = 'ervuDecisionDocumentServiceTypeCriminalPunishment';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        const result: { [key: string]: string } = {};
        if (Array.isArray(response.data) && response.data.length) {
          response.data.forEach(({ code, value }) => {
            result[code] = value || '';
          });
        }
        commit('setTypeCriminalPunishment', result);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      })
      .finally(() => {
        commit('setTypeCriminalPunishmentLoading', false);
      });
  },
  loadCriminalPunishmentCausesList({ commit }: ActionContext<State, State>) {
    const DICTIONARY_CODE =
      'ervuDecisionDocumentServiceCauseCriminalPunishment';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        commit('setCriminalPunishmentCausesList', response?.data || []);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      });
  },
  loadAdministrativePunishmentCausesList({
    commit,
  }: ActionContext<State, State>) {
    const DICTIONARY_CODE =
      'ervuDecisionDocumentServiceCauseAdministrativePunishment';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        commit('setAdministrativePunishmentCausesList', response?.data || []);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      });
  },
  loadPunishmentAppealList({ commit }: ActionContext<State, State>) {
    const DICTIONARY_CODE = 'ervuAppealDocumentServicePunishmentAppealResult';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        commit('setPunishmentAppealList', response?.data || []);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      });
  },
  loadArchivationReasonsList({ commit }: ActionContext<State, State>) {
    const DICTIONARY_CODE = 'ervuPersonRegistryRecruitArchivationReason';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        commit('setArchivationReasonsList', response?.data || []);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      });
  },
  loadListChangeStatusDictionary({ commit }: ActionContext<State, State>) {
    commit('setListChangeStatusDictionaryLoading', true);
    const DICTIONARY_CODE = 'listChangeStatus';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        const result: { [key: string]: string } = {};
        if (Array.isArray(response.data) && response.data.length) {
          response.data.forEach(({ code, value }) => {
            result[code] = value || '';
          });
        }
        commit('setListChangeStatusDictionary', result);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      })
      .finally(() => {
        commit('setListChangeStatusDictionaryLoading', false);
      });
  },
  loadCauseNotConfirmDictionary({ commit }: ActionContext<State, State>) {
    commit('setCauseNotConfirmDictionaryLoading', true);
    const DICTIONARY_CODE = 'ervuPersonRegistryCauseNotConfirm';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        const result: { [key: string]: string } = {};
        if (Array.isArray(response.data) && response.data.length) {
          response.data.forEach(({ code, value }) => {
            result[code] = value || '';
          });
        }
        commit('setCauseNotConfirmDictionary', result);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      })
      .finally(() => {
        commit('setCauseNotConfirmDictionaryLoading', false);
      });
  },
  loadIncidentsStatusesDictionary({ commit }: ActionContext<State, State>) {
    commit('setIncidentsStatusesDictionaryLoading', true);
    const DICTIONARY_CODE = 'incidents_statuses';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        const result: { [key: string]: string } = {};
        if (Array.isArray(response.data) && response.data.length) {
          response.data.forEach(({ code, value }) => {
            result[code] = value || '';
          });
        }
        commit('setIncidentsStatusesDictionary', result);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      })
      .finally(() => {
        commit('setIncidentsStatusesDictionaryLoading', false);
      });
  },
  loadRecruitErvuApplicationStatus({ commit }: ActionContext<State, State>) {
    commit('setRecruitErvuApplicationStatusLoading', true);
    const DICTIONARY_CODE = 'ervuPersonRegistryRecruitErvuApplicationStatus';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        const result: { [key: string]: string } = {};
        if (Array.isArray(response.data) && response.data.length) {
          response.data.forEach(({ code, value }) => {
            result[code] = value || '';
          });
        }
        commit('setRecruitErvuApplicationStatus', result);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      })
      .finally(() => {
        commit('setRecruitErvuApplicationStatusLoading', false);
      });
  },
  loadSourcesRecruitDictionary({ commit }: ActionContext<State, State>) {
    commit('setSourcesRecruitDictionaryLoading', true);
    const DICTIONARY_CODE = 'ervuPersonRegistrySourcesRecruit';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        const result: { [key: string]: string } = {};
        if (Array.isArray(response.data) && response.data.length) {
          response.data.forEach(({ code, value }) => {
            result[code] = value || '';
          });
        }
        commit('setSourcesRecruitDictionary', result);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      })
      .finally(() => {
        commit('setSourcesRecruitDictionaryLoading', false);
      });
  },
  loadTypeGepsDictionary({ commit }: ActionContext<State, State>) {
    commit('setTypeGepsDictionaryLoading', true);
    const DICTIONARY_CODE = 'personalTemplatePrefillNotificationsTypes';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        const result: { [key: string]: string } = {};
        if (Array.isArray(response.data) && response.data.length) {
          response.data.forEach(({ code, value }) => {
            result[code] = value || '';
          });
        }
        commit('setTypeGepsDictionary', result);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      })
      .finally(() => {
        commit('setTypeGepsDictionaryLoading', false);
      });
  },
  loadStatusGepsDictionary({ commit }: ActionContext<State, State>) {
    commit('setStatusGepsDictionaryLoading', true);
    const DICTIONARY_CODE = 'personalTemplatePrefillStatusGeps';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        const result: { [key: string]: string } = {};
        if (Array.isArray(response.data) && response.data.length) {
          response.data.forEach(({ code, value }) => {
            result[code] = value || '';
          });
        }
        commit('setStatusGepsDictionary', result);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      })
      .finally(() => {
        commit('setStatusGepsDictionaryLoading', false);
      });
  },
  loadTypesDulRf({ commit }: ActionContext<State, State>) {
    commit('setTypesDulRfLoading', true);
    const DICTIONARY_CODE = 'ervuPersonRegistryTypeDulRf';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        const result: { [key: string]: string } = {};
        if (Array.isArray(response.data) && response.data.length) {
          response.data.forEach(({ code, value }) => {
            result[code] = value || '';
          });
        }
        commit('setTypesDulRf', result);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      })
      .finally(() => {
        commit('setTypesDulRfLoading', false);
      });
  },
  loadDecisionTypeForGoodReason({ commit }: ActionContext<State, State>) {
    commit('setDecisionTypeForGoodReasonLoading', true);
    const DICTIONARY_CODE = 'decisionTypeForGoodReason';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        const result: { [key: string]: string } = {};
        if (
          Array.isArray(response.data) &&
          response.data.length
        ) {
          response.data.forEach(({ code, value }) => {
          result[code] = value || '';
          });
        }
        commit('setDecisionTypeForGoodReason', result);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      })
      .finally(() => {
        commit('setDecisionTypeForGoodReasonLoading', false);
      });
  },
  loadDecisionReason({ commit }: ActionContext<State, State>) {
    commit('setDecisionReasonLoading', true);
    const DICTIONARY_CODE = 'ervuPersonRegistryDecisionReason';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        const result: { [key: string]: string } = {};
        if (Array.isArray(response.data) && response.data.length) {
          response.data.forEach(({ code, value }) => {
            result[code] = value || '';
          });
        }
        commit('setDecisionReason', result);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - '${DICTIONARY_CODE}'`,
          error
        );
      })
      .finally(() => {
        commit('setDecisionReasonLoading', false);
      });
  },
  loadTypesMzClass({ commit }: ActionContext<State, State>) {
    commit('setTypesMzClassLoading', true);
    ervuMzMonitorServiceApi
      .getMzDictionary()
      .then((response) => {
        const result: { [key: string]: string } = {};
        if (Array.isArray(response.data) && response.data.length) {
          response.data.forEach(({ code, value }) => {
            result[code] = value || '';
          });
        }
        commit('setTypesMzClass', result);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - MzDictionary`,
          error
        );
      })
      .finally(() => {
        commit('setTypesMzClassLoading', false);
      });
  },
  loadDeliveryTypeSubpoena({ commit }: ActionContext<State, State>) {
    commit('setDeliveryDirectionsLoading', true);
    // допишем парамсы
    deliveryDirectionsServiceApi
      .getDeliveryTypeDirections({ params: { toFront: 'true' } })
      .then((response) => {
        const result: { [key: string]: string } = {};
        const resultAll: { [key: string]: string } = {};
        if (Array.isArray(response.data) && response.data.length) {
          response.data.forEach(({ code, name: value, hidden }) => {
            /* надо разбить словарь на несколько, формат его не очень вяжется с типом key:value */
            if (!hidden) result[code] = value || '';
            resultAll[code] = value || '';
          });
        }
        commit('setDeliveryDirections', result);
        commit('setDeliveryDirectionsFull', resultAll);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - DeliveryDirections`,
          error
        );
      })
      .finally(() => {
        commit('setDeliveryDirectionsLoading', false);
      });
  },
  loadIncidentTypes({ commit }: ActionContext<State, State>) {
    const DICTIONARY_CODE = 'type_of_incident';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        const result: { [key: string]: string } = {};
        const incidentTypeReasons: { [key: string]: string } = {};
        if (Array.isArray(response.data) && response.data.length) {
          response.data.forEach(({ code, value, extraInfo }) => {
            /* надо разбить словарь на несколько, формат его не очень вяжется с типом key:value */
            result[code] = value || '';
            incidentTypeReasons[extraInfo.type] = value || '';
          });
        }
        commit('setIncidentTypes', result);
        commit('setIncidentTypesReasons', incidentTypeReasons);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - DeliveryDirections`,
          error
        );
      });
  },
  loadIncidentsSource({ commit }: ActionContext<State, State>) {
    commit('setDeliveryDirectionsLoading', true);
    const DICTIONARY_CODE = 'incident_source';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        const map: { [key: string]: string } = {};
        if (Array.isArray(response.data) && response.data.length) {
          response.data.forEach(({ code, value }) => {
            /* надо разбить словарь на несколько, формат его не очень вяжется с типом key:value */
            map[code] = value || '';
          });
        }
        commit('setIncidentsSourceMap', map);
        commit('setIncidentsSource', response.data);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - DeliveryDirections`,
          error
        );
      })
      .finally(() => {
        commit('setDeliveryDirectionsLoading', false);
      });
  },
  loadAddressTypeDictionary({ commit }: ActionContext<State, State>) {
    commit('setAddressTypeDictionaryLoading', true);
    const DICTIONARY_CODE = 'addressTypes';
    NSI_SERVICE_API.getDictionaryV1(DICTIONARY_CODE)
      .then((response) => {
        const map: { [key: string]: string } = {};
        if (
          Array.isArray(response.data) &&
          response.data.length
        ) {
          response.data.forEach(({ code, value }) => {
            map[code] = value || '';
          });
        }
        commit('setAddressTypeDictionary', map);
      })
      .catch((error) => {
        console.error(
          `Ошибка при загрузки словарика статусов - AddressTypeDictionary`,
          error
        );
      })
      .finally(() => {
        commit('setAddressTypeDictionaryLoading', false);
      });
  },
};

export default function (): Module<State, State> {
  return {
    state,
    getters,
    mutations,
    actions,
  };
}
