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

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

export function getAxiosPutOptions(
  contentType = 'application/octet-stream'
): AxiosRequestConfig {
  return {
    headers: {
      'Content-Type': contentType,
    },
  };
}

export function getAxiosGetBlobOptions(
  responseType: ResponseType = 'blob'
): AxiosRequestConfig {
  return {
    responseType,
    headers: {
      Accept: '*/*',
    },
  };
}

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