import { auth } from '@services/AuthService';
import { AxiosRequestConfig } from 'axios';
import { Configuration, ConfigurationParameters } from '@openapi/printer';

/*
 * Конфигурация сервиса
 */
const initParams = async (): Promise<ConfigurationParameters> => {
  return {
    basePath: `${location.origin}/service/printer`.replace(/\/+$/, ''),
    accessToken: await auth.getAccessToken(),
  };
};

export const axiosBlobOptions: AxiosRequestConfig = {
  responseType: 'blob',
  headers: {
    Accept: '*/*',
  },
};

export const configCreator = async (): Promise<Configuration | undefined> => {
  try {
    return new Configuration(await initParams());
  } catch (e) {
    console.error(`[services/printer/configCreator] - ${e.toString()}`);
  }
};
