import { axiosInstance } from '../config/api';

export const fileUpload = (data, type = null, companyId = null) => {
  let searchParams = new URLSearchParams();

  if (type) {
    searchParams.append('type', type);
  }
  if (companyId) {
    searchParams.append('companyId', companyId);
  }

  const url = `/upload?${searchParams.toString()}`;

  return new Promise((resolve, reject) => {
    axiosInstance
      .post(url, data, {
        headers: {
          'Content-Type': 'multipart/form-data',
        },
      })
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const getPresignedUrl = (data) => {
  const url = `/s3`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .post(url, data)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const uploadToS3 = (file, putUrl) =>{
  const url = putUrl;
  return new Promise((resolve, reject) => {
    axiosInstance
      .put(url, file, {
        headers: {
          'Content-Type': 'multipart/form-data',
        },
      })
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
}