import { notifyError } from '@/utils/notify';

const objToMap = (o: Object) => new Map(Object.entries(o));

const mapInteragenciesToRows = (interagencies: Object): Array<Object> => {
  const map = objToMap(interagencies);
  const rows = [];
  for (const element of map) {
    rows.push({
      file: null,
      expand: false,
      response: element[1],
      label1: 'Наименование',
      label2: 'Описание',
      codes: [],
      code: {},
      responseTitle: '',
      responseText: element[1].mockResponse.responseMsg
    });
  }
  return rows;
};

const getErrorMessage = (error: any) => {
  return (
    (error.response.data.errorCode || error.response.data.message || error) + ''
  );
};

const requestNotifyError = (error: any) => {
  const errorMessage = getErrorMessage(error);
  notifyError(errorMessage);
};

const consoleNotifyError = (errorMessage: any) => {
  console.error(errorMessage);
  notifyError('' + errorMessage);
};

const MIN_STEP = 1;

export {
  mapInteragenciesToRows,
  consoleNotifyError,
  requestNotifyError,
  MIN_STEP,
};
