import { GenericService } from '../GenericService'
import mediaFileService from './MediaFileService'
class MediaService extends GenericService {
    constructor() {
        super('/api/admin/media')
    }

    uploadMedia = (file) =>
        new Promise((resolve, reject) => {
            mediaFileService
                .getImageUploadUrl({ type: file.type, fileName: file.name })
                .then(async (res) => {
                    console.log('RES URL: ', res, file)
                    fetch(res.url, {
                        method: 'PUT',
                        headers: {
                            'Content-Type': file.type,
                        },
                        body: file,
                    })
                        .then((response) => {
                            console.log('Res:', response, res.key)
                            resolve(res.key)
                        })
                        .catch((err) => {
                            console.log('Err in putting file on s3: ', err)
                            reject(err)
                        })
                })
                .catch((err) => {
                    console.log('ERR in getting url: ', err)
                    reject(err)
                })
        })
}

const mediaService = new MediaService()
export default mediaService
