import axios from 'axios';
import { type PostSetTempData } from '../photos-api';

import { API } from '../../../../types';
import { API_URL, APP_VERSION } from 'src/constants';
import { Platform } from 'react-native';

export const postSetUploadTemp = async (data: PostSetTempData) => {
  const url = API_URL + '/' + API.UPLOAD_TEMP;
  const formData = new FormData();
  formData.append('token', data.token);
  formData.append('file', data.file as unknown as Blob);

  const config = {
    headers: {
      Platform: Platform.OS,
      'App-Version': APP_VERSION
    },
    onUploadProgress: (progressEvent: ProgressEvent) => {
      if (progressEvent.lengthComputable) {
        const progress = progressEvent.loaded / progressEvent.total;
        console.log('Upload progress:', progress);
      }
    }
  };

  try {
    const response = await axios.postForm(url, formData, config as any);
    return response.data;
  } catch (error) {
    console.error('Error', error);
  }
};
