import { DefaultApi, PresignedUrlResponse } from '@openapi/s3urlsigner';
import { configCreator } from './config';
import {
  S3BaseParams,
  S3GetParams,
  S3PutParams,
} from '@/recruits/interfaces/s3UrlService';

class S3UrlSignerService {
  public async fetchSignedUrlForDelete({
    url,
    region = undefined,
    options = {},
  }: S3BaseParams) {
    try {
      const config = await configCreator();
      const { data } = await new DefaultApi(config).getSignedDELETE(
        url,
        region,
        options
      );
      return data;
    } catch (e) {
      console.error(
        `[services/S3UrlSignerService/fetchSignedUrlForDelete] - ${e}`
      );
      throw e;
    }
  }

  public async fetchSignedUrlForGet({
    url,
    fileName = undefined,
    region = undefined,
    options = {},
  }: S3GetParams) {
    try {
      const config = await configCreator();
      const { data } = await new DefaultApi(config).getSignedGET(
        url,
        fileName,
        region,
        options
      );
      return data;
    } catch (e) {
      console.error(
        `[services/S3UrlSignerService/fetchSignedUrlForGet] - ${e}`
      );
      throw e;
    }
  }

  public async fetchSignedUrlForPut({
    path,
    bucket,
    orgId,
    region,
    options = {},
  }: S3PutParams) {
    try {
      const config = await configCreator();
      const { data } = await new DefaultApi(config).getSignedPUTv3(
        path,
        bucket,
        orgId,
        region,
        options
      );
      return data;
    } catch (e) {
      console.error(
        `[services/S3UrlSignerService/fetchSignedUrlForPut] - ${e}`
      );
      throw e;
    }
  }
}

export default new S3UrlSignerService();
