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

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

export const axiosPutOptions: AxiosRequestConfig = {
  headers: {
    'Content-Type': 'application/octet-stream',
  },
};

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

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