import Vue from 'vue'
import api_client_methods from "../../shared/api _client_methods";

const MediaClient = new Vue({
    methods: {
        ...api_client_methods.methods,
        fetchItems(mediaTypes, page, searchTerm) {
            var url = this.replaceUrlParameters('/browse', {});
            let params = {
                mediaTypes: mediaTypes.join(','),
                page: page,
            }
            if (searchTerm !== undefined) {
                params.searchTerm = searchTerm;
            }
            url = this.setQueryParameters(url, params);

            return this.get(url);
        },
        upload(formData) {
            var url = this.replaceUrlParameters('/upload', {});

            return this.post(url, formData);
        },
        deleteMedia(type, id) {
            var url = this.replaceUrlParameters('/delete/{type}/{id}', {
                type: type,
                id: id,
            });

            return this.delete(url)
        },
        mediaPreview(type, id) {
            var url = this.replaceUrlParameters('/form/preview/{type}/{id}', {type: type, id: id});

            return this.get(url);
        },
        validateUploads(type, files) {
            var url = this.replaceUrlParameters('/validate/{type}', {type: type,});
            let filesData = [];
            files.forEach((file) => filesData.push({
                name: file.name,
                size: file.size,
                mimeType: file.type,
            }));
            return this.post(url, {files: filesData});
        }
    },
})

export default MediaClient;